Junior — Middle
Kas saame nimetada erinevaid meetodeid kogumitega töötamiseks ja nende omadusi?
sobes.tech AI
Vastus AI-lt
In iOS, the most commonly used collection types and methods are:
-
Array — an ordered list of elements. Allows adding, removing, and modifying elements by index.
- Methods:
append(),insert(),remove(at:),sort(),filter(),map().
- Methods:
-
Set — an unordered collection of unique elements.
- Fast search and existence check.
- Methods:
insert(),remove(),contains(), set operations (union,intersection).
-
Dictionary — a collection of key-value pairs.
- Fast access to values via key.
- Methods:
updateValue(),removeValue(forKey:), iteration over keys and values.
Features:
- Array maintains order, allows duplicates.
- Set does not guarantee order, does not allow duplicates.
- Dictionary stores data in pairs, keys are unique.
Example of using Array:
var numbers = [1, 2, 3]
numbers.append(4) // [1, 2, 3, 4]
let filtered = numbers.filter { $0 % 2 == 0 } // [2, 4]
Example of using Set:
var fruits: Set = ["apple", "banana"]
fruits.insert("orange")
fruits.contains("banana") // true
These collections cover most data storage and processing tasks in iOS.