Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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 :)