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
Wilztan Andrew
Python Web Development Techdegree Student 4,979 Pointshow to create a looping string inside a function ?
func getLoopNumber(loop:String)-> String{
var a
for ngeloop in loop{
a = print(ngeloop)
}
return a
}
whats wrong with this code?
Wilztan Andrew
Python Web Development Techdegree Student 4,979 Pointsso when i call the function name it will give me a result of the string for example in my struct ,i have a string of ["jacko","rehab"] then when i call the function name it will return to me jacko rehab
Martin Wildfeuer
Courses Plus Student 11,071 PointsSo that is what the code below would do. It's not a function but a computed property. With a a computed property, you can use simple dot notation, without the need of (), name.fullName. That is: no function, no loop, same result. Paste it in a playground and give it a try :)
Wilztan Andrew
Python Web Development Techdegree Student 4,979 Pointsyeah i know too but i want to use function and in inside the function, there is a loop for the string var
Wilztan Andrew
Python Web Development Techdegree Student 4,979 Pointsyeah i know too but i want to use function and in inside the function, there is a loop for the string var
Wilztan Andrew
Python Web Development Techdegree Student 4,979 Pointsfor the simple. can u create a looping array insde a function? or not?
Martin Wildfeuer
Courses Plus Student 11,071 PointsOf course you can. But first of all, your name property is not a string. It is an array of strings. So, the below code might do what you are looking for, beware of the trailing whitespace.
struct Person {
var name = ["Jacko", "Rehab"]
func getLoopName() -> String {
var loopName = ""
for element in name {
loopName += element + " "
}
return loopName
}
}
let person = Person()
person.getLoopName()
Other than that, please look at the other examples I gave you. For what you want to achieve, this is the worst approach. Cheers.
Wilztan Andrew
Python Web Development Techdegree Student 4,979 Pointsthank you, i understand now
1 Answer
Martin Wildfeuer
Courses Plus Student 11,071 PointsPasting this code to a Playground will give you two errors right away:
func getLoopNumber(loop:String)-> String{
// var a: You have to provide a type annotation if you don't assign a value
// right away, as Swift can't infer anything
var a
// as of Swift 2, Strings do not longer conform to the SequenceType
// protocol, thus you can't loop over a String directly.
for ngeloop in loop {
// You are trying to assign a print statement to a variable, even if that worked,
// a would be overwritten with every iteration, so a would return the very last
// character only
a = print(ngeloop)
}
return a
}
So, this does not really make sense. Depending on what you are trying to achieve,
you can use the characters property of a String, which conforms to the SequenceType
protocol and thus can be iterated over. You don't have to create a function for this,
it's already there!
let hello = "Hello there!"
for character in hello.characters {
print(character)
}
Hope that helps :)
Wilztan Andrew
Python Web Development Techdegree Student 4,979 Pointshmm i just want to achieve a looping struct inside a function , but it always give me error can you help me?
Martin Wildfeuer
Courses Plus Student 11,071 PointsOk, I am not sure if I understand this. Please describe in more detail what you expect as input - processing - output.
Wilztan Andrew
Python Web Development Techdegree Student 4,979 Pointsfor ex : i have struct name{ var name:String = [ "jacko", "rehab" ]
//i want to them inside a function func getLoopName()->String{ //i want to use for in loop to return the name .... } }
can you help me fill the code with for in loop?
Martin Wildfeuer
Courses Plus Student 11,071 PointsSorry, I think I still don't get it. Do you want to return a String that joins the name string array?
struct Name {
var name: [String] = ["jacko", "rehab"]
var fullName: String {
return name.joinWithSeparator(" ")
}
}
let name = Name()
let fullName = name.fullName // "jacko rehab"
Btw., I would not use a String array for first and last name, but a property for each individually. How would you tell first and last name apart in an array? I mean, sure, the order, but is that guaranteed?
struct Person {
var firstName = "jacko"
var lastName = "rehab"
var fullName: String {
return "\(firstName) \(lastName)"
}
}
let person = Person()
let fullName = person.fullName // "jacko rehab"
Wilztan Andrew
Python Web Development Techdegree Student 4,979 PointsWilztan Andrew
Python Web Development Techdegree Student 4,979 Pointsno, i just want to create a looping inside a function and my expected result is jacko Rehab