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 Basics (retired) Collections What is a Dictionary?

When to use "" , () , ("") , [] , [""]

I have difficulty understanding when to use wich: "" , () , ("") , [] , [""]

Anyone can it explain me please?

Thanks Greg

2 Answers

Renato Nobre
Renato Nobre
5,805 Points
  • "" -The quotation marks you use for Strings
  • [] -The braces you use for an Array or Dictionaries
  • () - The parentheses you use for putting parameters of functions

So if you got quotation marks inside a brace like:

var arrayForDemo: Array = ["This", "is","array","of","strings"] 

You have an Array of strings.

The ("") is used for taking strings parameters for your function

func fullName(firstName: String, lastName: String) -> String {
    return "\(firstName) \(lastName)"
}
fullName("Bob", "Tuesday")

As you can see, the code create a function that take two strings as parameters, when the function is called you use the (""), because you are calling strings.

Obs: The () empty is used when the function take no parameters.

Hope that helps, any further questions fell free to contact me! I highly recommend you read A Swift Tour on Swift Documentation.

Fred Sites
Fred Sites
11,151 Points

This is what I've seen consistently in Swift so far...

  • Strings (usually text) go in " "
  • Arrays (an unordered list of elements... can be strings, numbers etc) go in [ ]
  • String in an array would be ["example1", "example 2"]
  • Parameters and everything else (usually for a function) go in ( )
  • If passing a string to as a parameter it would look like (" ")