Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

Plz help me

I want to create a function in which shows every element in an array each time the user click the button in which it shows at the screen app. Every time my user click this button I want to show him another information from my array. I would like to show him all the information of my array without interrupting the app.

Is this function ok??? What should i put at return???

func randomFact () -> String { let maxPrints = 40 var currentPrint = 0

    while (currentPrint < maxPrints) {
        for element in factsArray {
            print("\(element)")
        }
        currentPrint += 1

} return // what should i put.Is this function ok???? }

7 Answers

Hey Frenklin -

Based on your function signature you are supposed to be returning a String from the function, although I am not sure if that is what you really intend to do. If in fact you are just looking to print all the elements from the factsArray you could just return void from your function:

func randomFact() {
    ... all your code here
}

It also seems like you are printing out every fact from the array 40 times? (currentPrint = 0 and maxPrints = 40). Hope that helps. Let me know if I can help clarify a little more.

what string should i return to make my function works???

I have tried to put void but it results error in my other files of my program when i use this function.The Xcode say it expect an String.So what should i do??

I am not exactly sure what it is you are trying to do. I would recommend the change that I showed in the code example above. Remove the -> String from the function definition line and Xcode will not expect a string to be returned from the function.

In other words your function should look like this:

func randomFact() {
    let maxPrints = 40 var currentPrint = 0

    while (currentPrint < maxPrints) {
        for element in factsArray {
            print("\(element)")
        }
        currentPrint += 1
}

If you are looking to print out the facts from the array 40 times. If not and you only want to print out the facts once you can use:

func randomFact() {
        for element in factsArray {
            print("\(element)")
        }
}

I have tried to compile as you said but when i use this function in my other files of my program it says : "Wainting for a String not for int"

I am not sure what the other files in your program look like, so I unfortunately cannot help out with that. If you want to post here I can take a look. Thanks

@IBAction func shohFunFact() { view.backgroundColor = colorWheel.randomColor()

    funFactLabel.text? = factBook.randomFact()    //here XCODE it says cannot assign value of type '( )' to type 'STRING'//

}

}

Okay. You need to return just one fact from the array? You can just grab a random fact by pulling one from the array by index number.

func randomFact() {
        return factsArray[Int((arc4random_uniform(factsArray.count))] 
}

It keeps saying (3) errors

func randomFact () { let maxPrints = 40 var currentPrint = 0

    while (currentPrint < maxPrints) {
        for element in factsArray {
            print("\(element)")
        }
        currentPrint += 1

} return factsArray[Int((arc4random_uniform(factsArray.count))] // 1) expected ')' in exprresion list // 2) expected ']' in exprresion list // 3) expected expression in a list of expression }

You are doing too much in the function. You are trying to print out the entire array 40 times and your return is outside of your function block. It needs to be before the closing }

This is the entire file.Please help what should i do to make me user every time he clicks the button i can show to him another element from my array and when user reaches the end of my array i want to start again from the beginning

import Foundation

struct FactBook { var factsArray = [ "Ckemi", "Ky eshte nje aplikacion i bere ne gjuhen programuese Swift.", "Gjuha programuese Swift eshte nje gjuhe moderne,fleksibel dhe e sigurt.", "Ajo u krijua nga kompania Apple ne vitin 2014 ", "Ky aplikacion ka si qellim te nxise dhe te promovoje kulturen e qyteteve te Shqiperise", "Ky projekt eshte konceptuar te vihet ne pune nga Bashkite dhe zonat e tyre perkatese", "Shpresoj te mari feedback e duhur", "Faleminderit"

]


func randomFact ()  {
    let maxPrints = 3
    var currentPrint = 0

    while (currentPrint < maxPrints) {
        for element in factsArray {
            print("\(element)")
        }
        currentPrint += 1

} } }

remove

func randomFact ()  {
    let maxPrints = 3
    var currentPrint = 0

    while (currentPrint < maxPrints) {
        for element in factsArray {
            print("\(element)")
        }
        currentPrint += 1
} } }

and replace with

func randomFact() -> String {
        return factsArray[Int((arc4random_uniform(factsArray.count))] 
}

remove

func randomFact ()  {
    let maxPrints = 3
    var currentPrint = 0

    while (currentPrint < maxPrints) {
        for element in factsArray {
            print("\(element)")
        }
        currentPrint += 1
} } }

and replace with

func randomFact() -> String {
        return factsArray[Int((arc4random_uniform(factsArray.count))] 
}

func randomFact() -> String { return factsArray[Int((arc4random_uniform(factsArray.count))] )

It is saying cannot convert value of type 'Int' to expected argument type Uint32