0

for example:

let myArray:[] = []

or

let myArray = []
1
  • you don't need semi-colons anymore Commented Feb 21, 2015 at 0:46

3 Answers 3

3

The first one is not possible (it will complain about expected element type) If you don't know the element type you can use AnyObject but if you know the type of it you have to put it there (Int,Double,String, AnyObject, etc...).

var myArrayOfDoubles:[Double] = []

The second one (when omitting the type) is OK ONLY if you initialize it with some values but If you try it with an empty array you won't be able to append anything (at least using Playground) saying that NSArray does not have a member named append.

var myArrayOfInts = [1,2,3,4,5]          //   [Int]
var myArrayOfAnyObject:[AnyObject] = []  //   [AnyObject]

And finally last but not least you have to define it as var because if you define it using let it will stay empty forever :)

Sign up to request clarification or add additional context in comments.

1 Comment

At least this is a good habit you should always use let and if Xcode complains you just change it to var :)
0

Yes, you can, it can be Any or Anyobject... example:

var noTypeArray = [Any]()

If you want to fill the array eventually it may be var, no let, there's no point otherwise.

Comments

0

No. If you don't know what will go in use this:

var myArray = [Any]()

Apple has introduced a Type-GeStaPo and you must type anything that's not up the tree at count three.

To add to this use

myArray.append("this")
myArray.append(1)
myArray.append(myObject)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.