What's the main difference between the Array , Set and Dictionary collection
type?
What's the main difference between the Array , Set and Dictionary collection
type?
In Swift, Arrays, Sets, and Dictionaries are the primary collection types, each serving distinct purposes and having unique characteristics. Here’s a detailed comparison of these three collection types:
var arrayExample: [Int] = [1][2][3][4][5]
[1][3][4].var arrayExample: [Int] = [1, 2, 3, 4, 5]
arrayExample.append(6) // Adds 6 to the end of the array
arrayExample.remove(at: 0) // Removes the first element
Set
type, e.g., var setExample: Set<Int> = [1][2][3][4][5]
[1][3][4].var setExample: Set<Int> = [1, 2, 3, 4, 5]
setExample.insert(6) // Adds 6 to the set
setExample.remove(1) // Removes 1 from the set
var dictionaryExample: [String: Int] = ["one": 1, "two": 2, "three": 3]
[1][4][6].var dictionaryExample: [String: Int] = ["one": 1, "two": 2, "three": 3]
dictionaryExample["four"] = 4 // Adds a new key-value pair
dictionaryExample["one"] = nil // Removes the key-value pair with key "one"
middle
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào