Containers
Objective-C has the same core containers that Swift does with the two exceptions that they are named slightly differently and all the containers in Objective-C are reference types because of the basic requirement that all Objective-C types must be reference types.
Arrays
In Objective-C, arrays are called NSArray
. Let's look at the initialization of an array in both Swift and Objective-C side-by-side:
var array = [Int]() NSArray *array = [NSArray alloc]; array = [array init];
We defined a variable called array
that is a reference to the NSArray
type. We then assign it to a newly allocated instance of NSArray
. The square bracket notation in Objective-C allows us to call methods on a type or on an instance. Each separate call is always contained within a single set of square brackets. In this case, we first call the alloc
method on the NSArray
class. This returns a newly allocated variable that is of the NSArray
type.
In contrast to Swift, Objective-C requires a two-step process to initialize...