Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

patent10021

macrumors 68040
Original poster
Apr 23, 2004
3,517
795
What is the difference?

Code:
var numbers = [Int]() // Empty array of integers.
var numbers = Array<Int>() // Empty array of integers using a generic type.

numbers.append(1) // [1]
numbers.append(2) // [1, 2]
numbers.append(3) // [1, 2, 3]

let firstNumberAgain = numbersAgain[0] // 1
 

Dookieman

macrumors 6502
Oct 12, 2009
390
67
Nothing. Both specify an array that holds type Int. One is the short hand version of creating an array versus the long form.

In my experience the Swift compiler has some problems when using the short convenience Array's in some specific instances. Actually, Swift arrays in general are annoying to deal with as they aren't nearly as full featured as their NS counterparts.
 
Last edited:

patent10021

macrumors 68040
Original poster
Apr 23, 2004
3,517
795
Nothing. Both specifying that an array that holds type Int. One is the short hand version of creating an array versus the long form.

In my experience the Swift compiler has some problems when using the short convenience Array's in some specific instances. Actually, Swift arrays in general are annoying to deal with as they aren't nearly as full featured as their NS counterparts.
I'd say Swift is simply super type-safe.

Thanks for your answer. But still people seem to be using longer form <> generics all over the place so there must be some reason.

Something still doesn't sit right with me. There has to be a reason why <> is widely recognized as a generic. Swift 2 even got a big update with Generics in that Swift 2 added the ability to subclass generic classes.
[doublepost=1460003121][/doublepost]Isn't the key that we can create our own types and choose the type at time of declaration? We cannot do that with the stdlib.
[doublepost=1460003231][/doublepost]From tuts+
Generics allow you to declare a variable which, on execution, may be assigned to a set of types defined by us.

In Swift, an array can hold data of any type. If we need an array of integers, strings, or floats, we can create one with the Swift standard library. The type that the array should hold is defined when it's declared. Arrays are a common example of generics in use. If you were to implement your own collection you'd definitely want to use generics.
 
Last edited:

Dookieman

macrumors 6502
Oct 12, 2009
390
67
In theory you should be able to use both interchangeably but I've come across a few cases where the compiler didn't like using the shorthand version of the Swift array. I had to switch over to the longhand array declaration to get it to work, which shouldn't be necessary. I'm betting other people have come across similar problems and the "[]" type of declaration and mitigated the problem with using Array<>.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,896
8,708
A sea of green
Something still doesn't sit right with me. There has to be a reason why <> is widely recognized as a generic. Swift 2 even got a big update with Generics in that Swift 2 added the ability to subclass generic classes.
One possible reason is that other languages use the < > syntax for generic types, so it's already widely known among programmers. It shouldn't be a surprise when humans do something in a familiar way vs. an equivalent yet unfamiliar way.
 
  • Like
Reactions: mildocjr and dejo

AdonisSMU

macrumors 604
Oct 23, 2010
7,305
3,065
I think [Int]() is more terse but clearly people have issues with it. Hopefully Apple and the rest of the Open Source Community will resolve it.
 

AxoNeuron

macrumors 65816
Apr 22, 2012
1,251
855
The Left Coast
I should really just pick one style of instantiating an array because as it stands I literally use all three ways. I'm not even slightly consistent:

var nums = [Int]();
var nums : [Int] = [];
var nums = Array <Int>();
 

AdonisSMU

macrumors 604
Oct 23, 2010
7,305
3,065
I should really just pick one style of instantiating an array because as it stands I literally use all three ways. I'm not even slightly consistent:

var nums = [Int]();
var nums : [Int] = [];
var nums = Array <Int>();
What style do you use for a dictionary that holds an array?

here is some sample data
Code:
var dict = [
    "key1": "Name",
    "key2": ["Boo!", "Foo", "Bar", "Far"]
]
 

Dookieman

macrumors 6502
Oct 12, 2009
390
67
What style do you use for a dictionary that holds an array?

here is some sample data
Code:
var dict = [
    "key1": "Name",
    "key2": ["Boo!", "Foo", "Bar", "Far"]
]

var dictionary = [String : AnyObject]

This lets the dictionary hold any kind of object, not just Arrays.
 

AdonisSMU

macrumors 604
Oct 23, 2010
7,305
3,065
var dictionary = [String : AnyObject]

This lets the dictionary hold any kind of object, not just Arrays.
The problem is when I try to add items to the arrays I get an error.

for example.... the below throws an error.
Code:
dictionary.keyArray.append(item)
 

Dookieman

macrumors 6502
Oct 12, 2009
390
67
The problem is when I try to add items to the arrays I get an error.

for example.... the below throws an error.
Code:
dictionary.keyArray.append(item)


I'm not sure how you are creating your dictionary, but I think it's how you are accessing your array. You need to get the object from it's key, then append the new element to the array.

e.g.

Code:
var dictionary = [String : [String]]()
let testArray = ["Hello", "World", "Look"]
dictionary["array1"] = testArray
dictionary["array1"]?.append("Appending")
 

AdonisSMU

macrumors 604
Oct 23, 2010
7,305
3,065
I'm not sure how you are creating your dictionary, but I think it's how you are accessing your array. You need to get the object from it's key, then append the new element to the array.

e.g.

Code:
var dictionary = [String : [String]]()
let testArray = ["Hello", "World", "Look"]
dictionary["array1"] = testArray
dictionary["array1"]?.append("Appending")
Maybe its the fact that I'm not using it like an optional. *scratches head* I tried the dictionary["array1"] syntax as well and the error I got back was AnyObject does not have append method...
 

Dookieman

macrumors 6502
Oct 12, 2009
390
67
Maybe its the fact that I'm not using it like an optional. *scratches head* I tried the dictionary["array1"] syntax as well and the error I got back was AnyObject does not have append method...

That is correct. AnyObject doesn't have an "append" method. In my example, the dictionary holds [String : Array<String>] (An array of strings).

The problem when using AnyObject in this case is that the compiler doesn't know what is actually being held at key "array1". If you specify it is holding an array, then you can use the Array methods. To get around this, you need to grab and assign the array to a variable and make your additions on the new array, then reassign the array with new data to the "array1" key.
 
  • Like
Reactions: AdonisSMU

AdonisSMU

macrumors 604
Oct 23, 2010
7,305
3,065
That is correct. AnyObject doesn't have an "append" method. In my example, the dictionary holds [String : Array<String>] (An array of strings).

The problem when using AnyObject in this case is that the compiler doesn't know what is actually being held at key "array1". If you specify it is holding an array, then you can use the Array methods.
Which makes sense to me but that means you cant use type specific methods in the context of a dictionary which seems weird to me. In the Javascript world its very natural to have a key value pair with a numbers or strings or arrays or even other objects... SWIFT forces each key value pair to be of the same type... I mean can get around this limitation with using a struct or a class but sometimes those options seem so heavy handed to me.... *scratches head*

EDIT:
To get around this, you need to grab and assign the array to a variable and make your additions on the new array, then reassign the array with new data to the "array1" key.

Good idea!

I could see this being a problem with well really any other type besides Arrays as well.... So lets say you have some float functions or some string functions you want to use with a dictionary... then I'd fully expect AnyObject to throw an error...
 

Dookieman

macrumors 6502
Oct 12, 2009
390
67
Which makes sense to me but that means you cant use type specific methods in the context of a dictionary which seems weird to me. In the Javascript world its very natural to have a key value pair with a numbers or strings or arrays or even other objects... SWIFT forces each key value pair to be of the same type... I mean can get around this limitation with using a struct or a class but sometimes those options seem so heavy handed to me.... *scratches head*

EDIT:


Good idea!

I could see this being a problem with well really any other type besides Arrays as well.... So lets say you have some float functions or some string functions you want to use with a dictionary... then I'd fully expect AnyObject to throw an error...

You don't necessarily have to use AnyObject, it was just a quick example. If you want a dictionary of Array you can specify that when setting up your dictionary.

Code:
let dictionartyWithArrays = [String : [<Whatever Type you want>]]()

If you use this you can call array methods the objects without needing to grab it.
 

AdonisSMU

macrumors 604
Oct 23, 2010
7,305
3,065
You don't necessarily have to use AnyObject, it was just a quick example. If you want a dictionary of Array you can specify that when setting up your dictionary.

Code:
let dictionartyWithArrays = [String : [<Whatever Type you want>]]()

If you use this you can call array methods the objects without needing to grab it.
Ohhh duhhhh you are so right!!!

Edit: this won't work either.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.