In Swift, let is used to declare constant and var is used to declare variable. In Objective-C, there are array and mutable array, also dictionary and mutable dictionary. Instinctively, you may think constant array is not mutable and the values inside the array is not editable. The truth is a constant array is not mutable but you may edit the values inside. However, constant dictionary is not the same as array. A constant dictionary is not mutable or editable.
I don't know why constant array is that special :(
Update:
Start from Beta 3, let array is now completely immutable, and var array is completely mutable.
Link: Swift Language Changes in Xcode 6 beta 3
Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts
05 July, 2014
22 June, 2014
Array type of parameter in function
Two ways to pass an array into a function:
1. Passing in a sequence
func sumOf(numbers: Int...) -> Int {
...
}
sumOf(42, 597, 12)
2. Passing in a Array object
func sumOf(numbers: [Int]) -> Int {
...
}
let numbers = [42, 597, 12]
sumOf(numbers)
In the 1st example, array is passed as a sequence of Int. In the 2nd example, an object of Array is required, otherwise, there will be an error. You may see that no matter you use Int... or [Int], numbers is an array inside the function.
1. Passing in a sequence
func sumOf(numbers: Int...) -> Int {
...
}
sumOf(42, 597, 12)
2. Passing in a Array object
func sumOf(numbers: [Int]) -> Int {
...
}
let numbers = [42, 597, 12]
sumOf(numbers)
In the 1st example, array is passed as a sequence of Int. In the 2nd example, an object of Array is required, otherwise, there will be an error. You may see that no matter you use Int... or [Int], numbers is an array inside the function.
Subscribe to:
Posts (Atom)
