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

iOS Swift 2.0 Collections and Control Flow Introduction to Collections Manipulating an Array

sai jayanth kashyap
sai jayanth kashyap
3,293 Points

having problem with append command

when i tried to apply append command with an array in my playground, it shows an error saying cannot invoke append command with an argument list of type "string". This Is what i have tried in my playground. var name = "sai,jayanth,kashyap,naresh,pavan,thony,chaitanya,anand" var rollNo = "1,2,3,4,5,6,7" name += ",leela" name.append("sai kiran")

2 Answers

Luca Argenziano
Luca Argenziano
16,416 Points

Hi Sai, I think I can help you. You said you wanted to try the append method on an array, but actually your "name" variable is only a string, not an array. A string, since it's only a variable, has some properties but no methods available. To make your "name" an array, you must declare it as follows:

  var names: [string] = ["sai", "jayanth", "kashyap", "naresh", "pavan"];

This time you can call every methods available for arrays, like:

  names.count;
  names.append("Luca");

Note also that since an array is a collection of variables, it's common practice to use a plural name to declare it. So you could say: "I want to add a name to my names array".

Hope it can help you ;)

This is the Swift language. Your link contains a document for the Python language, which is completely different.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Woops! hehehe I'm tired. Boy, talk about confusing your languages! But still what you need is some sort of string concatenation. According to Apple Developers documentation on Swift you can use the append method for adding a character but to add another string you should use concatenation.

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html

Obviously, I removed my previous answer :)