var numbers = [Int]() vs var numbers = Array<Int>()

Discussion in 'iOS Programming' started by patent10021, Apr 6, 2016.

  1. patent10021 macrumors 68020

    patent10021

    Joined:
    Apr 23, 2004
    #1
    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
    
     
  2. Dookieman, Apr 6, 2016
    Last edited: Apr 7, 2016

    Dookieman macrumors 6502

    Joined:
    Oct 12, 2009
    #2
    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.
     
  3. patent10021, Apr 6, 2016
    Last edited: Apr 6, 2016

    patent10021 thread starter macrumors 68020

    patent10021

    Joined:
    Apr 23, 2004
    #3
    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.
    --- Post Merged, Apr 6, 2016 ---
    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.
    --- Post Merged, Apr 6, 2016 ---
    From tuts+
     
  4. Dookieman macrumors 6502

    Joined:
    Oct 12, 2009
    #4
    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<>.
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    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.
     
  6. AdonisSMU macrumors 603

    Joined:
    Oct 23, 2010
    #6
    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.
     
  7. abhibeckert macrumors regular

    abhibeckert

    Joined:
    Jun 2, 2007
    Location:
    Cairns, Australia
    #7
    I use the following syntax personally and have never ran into any issues.

    Code:
    var numbers: [Int] = []
    
     
  8. AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #8
    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>();
     
  9. AdonisSMU macrumors 603

    Joined:
    Oct 23, 2010
    #9
    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"]
    ]
    
     
  10. Dookieman macrumors 6502

    Joined:
    Oct 12, 2009
    #10
    var dictionary = [String : AnyObject]

    This lets the dictionary hold any kind of object, not just Arrays.
     
  11. AdonisSMU macrumors 603

    Joined:
    Oct 23, 2010
    #11
    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)
    
     
  12. Dookieman macrumors 6502

    Joined:
    Oct 12, 2009
    #12

    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")
    
     
  13. AdonisSMU macrumors 603

    Joined:
    Oct 23, 2010
    #13
    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...
     
  14. Dookieman macrumors 6502

    Joined:
    Oct 12, 2009
    #14
    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.
     
  15. AdonisSMU macrumors 603

    Joined:
    Oct 23, 2010
    #15
    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...
     
  16. Dookieman macrumors 6502

    Joined:
    Oct 12, 2009
    #16
    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.
     
  17. AdonisSMU, Apr 11, 2016
    Last edited: Apr 12, 2016

    AdonisSMU macrumors 603

    Joined:
    Oct 23, 2010
    #17
    Ohhh duhhhh you are so right!!!

    Edit: this won't work either.
     
  18. AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #18
    I do this:

    Code:
    var dict = [String : [String]]();
    
     

Share This Page