I\'ve been playing around with arrays of generic classes with different types. It\'s easiest to explain my problem with some sample code:
// Obviously a very
If you try this modified example in a playground, it will systematically crash:
// Obviously a very pointless protocol...
protocol MyProtocol {
var value: Int { get }
}
extension Int : MyProtocol { var value: Int { return self } }
//extension Double: MyProtocol { var value: Double { return self } }
class Container {
var values: [T]
init(_ values: T...) {
self.values = values
}
}
var containers: [Container] = []
Probably they are still working on this, and things might change in the future.
Anyway as of now, my explanation for this is that a protocol is not a concrete type. Thus you do not now how much space in ram something conforming to the protocol will take (for example an Int might not occupy the same amount of ram as a Double). Thus it might be quite a tricky problem the allocation of the array in ram.
Using an NSArray you are allocation an array of pointers (pointers to NSObjects) and they all occupy the same amount of ram. You can think of the NSArray as an array of the concrete type "pointer to NSObject". Thus no problem calculating ram allocation.
Consider that Array as well as Dictionary in Swift are Generic Struct, not objects containing pointers to objects as in Obj-C.
Hope this helps.