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
Frenklin Hasani
1,754 PointsPlz 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
Ryan Huber
13,021 PointsHey 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.
Frenklin Hasani
1,754 PointsI 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"
Ryan Huber
13,021 PointsI 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
Frenklin Hasani
1,754 Points@IBAction func shohFunFact() { view.backgroundColor = colorWheel.randomColor()
funFactLabel.text? = factBook.randomFact() //here XCODE it says cannot assign value of type '( )' to type 'STRING'//
}
}
Ryan Huber
13,021 PointsOkay. 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))]
}
Frenklin Hasani
1,754 PointsIt 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 }
Ryan Huber
13,021 PointsYou 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 }
Frenklin Hasani
1,754 PointsThis 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
} } }
Ryan Huber
13,021 Pointsremove
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))]
}
Ryan Huber
13,021 Pointsremove
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))]
}
Frenklin Hasani
1,754 Pointsfunc randomFact() -> String { return factsArray[Int((arc4random_uniform(factsArray.count))] )
It is saying cannot convert value of type 'Int' to expected argument type Uint32
Frenklin Hasani
1,754 PointsFrenklin Hasani
1,754 Pointswhat 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??
Ryan Huber
13,021 PointsRyan Huber
13,021 PointsI 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:
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: