What's the difference between & Array<Int>?
let numberList = Array(1...10) // type == Array<Int> let numberList2 = [1,2,3,4,5,6,7,8,9,10] // type == [Int]
The code above assigns the commented types to each constant. I don’t recall running into an explanation for this in the documentation.
Is it simply a case of one using the constructor, and the other using literal notation, and as a result the differing types are produced?
If so, are there any differences to using one over the other, once they’ve been declared? (i.e. using the constructor allows you to use class initializers etc. but post initialization, does one offer any benefits over the other?)
They seem to both use the same Array API once initialized. So I’m assuming this is all purely syntactical?
Lastly, is there any way to mimic the constructors functionality via the literal notation? e.g.
let arr = (1...10) // doesn't create [1,2,3,4,5,6,7,8,9,10]
Can anyone tell me why the following code produces the result it does?:
let numberList2 = [1...10] // == ["1..<11"]
I reason that the above is an array containing a range. The type shown in a Playground reports that the type is: [Range], as expected. However, why is the value seemingly reported as an array containing a string representing a half-open range up to 11, rather than a closed range up to 10?
Apologies for the disjointed post. Thanks in advance for any help.
- How do you create an immutable array in Swift?
- How to return first 5 objects of Array in Swift?
- Append Firebase data to an array in Swift
- Concatenate Swift Array of Int to create a new Int
- Get notified when element added/removed to array
- How do I check the number of values in an array that is a UDF parameter?
Solutions Collect From Internet About “What's the difference between & Array<Int>?”
From The Swift Programming Language: Collection Types
Array Type Shorthand Syntax
The type of a Swift array is written in full as
Array<SomeType>, where SomeType is the type of values the array is allowed to store. You can also write the type of an array in shorthand form as
[SomeType]. Although the two forms are functionally identical, – the shorthand form is preferred and is used throughout this guide when referring to the type of an array.
That is, both comments are correct – and both represent the same type.
- How to open settings app programatically?
- library linked but Xcode says “file not found.”
- Sticky search bar and section header behavior similar to native Contacts app
- NSData on custom class?
- When constructing a Swift UnsafeMutablePointer manually, is destroy/dealloc obligatory after alloc?
- Detect Video playing full screen in Portrait or landscape
- Making a post to API with RKParams and mapping the response with RKObjectMapping using RestKit
- xCode 6 beta 7: storyboard adds extra space on right and left sides
- How can I monitor scroll position while scrolling in Safari on iOS?
- PurgeIdleCellConnections causes application to freeze then crash
- Why use AFNetworking instead of dispatch_async?
- Swift pattern matching with enum and Optional tuple associated values
- Communicating between iOS and Android with Bluetooth LE
- Xcode 8 and Swift 3 NSCalendar