Kas on võimalik kasutada erinevaid andmetüüpe ühes massiivis?
sobes.tech AI
Vastus AI-lt
In iOS, speaking about the Swift language, arrays (Array) are type-safe and contain elements of the same type. It is not possible to directly create an array with elements of different types, such as [Int, String, Double].
However, if you need to store different types in one array, you can use the Any or AnyObject type:
let mixedArray: [Any] = [42, "text", 3.14, true]
In this case, the array contains elements of different types, but working with them requires type casting, which reduces safety and convenience.
In Objective-C, arrays (NSArray) can contain objects of different classes, as they are collections of objects, but primitive types need to be wrapped in objects (NSNumber, NSString, etc.).
Therefore, in Swift, arrays are strictly typed, but with Any, you can store different types, although it is not recommended without necessity.