Reputation: 536
Im seeking some understanding on the code below:
NSMutableArray<DEVICEID*>* device_a = @[
[device_IDENTIFY command],
[device_STATUS command],
[device__POWER command]].mutableCopy;
Is this code creating an array of objects, where on each initialization of each element in the array calls the constructor for each object?
Upvotes: 1
Views: 491
Reputation: 16160
According to Apple Docs
The NSMutableArray class declares the programmatic interface to objects that manage a modifiable array of objects. This class adds insertion and deletion operations to the basic array-handling behavior inherited from NSArray.
In the above code, I assume that command
is a method which declared in an extension file of something(device_IDENTIFY
's type) that returns an Object of type DEVICEID*
.
ie.,
DEVICEID* identity = [device_IDENTIFY command];
DEVICEID* status= [device_STATUS command];
DEVICEID* identity = [device_POWER command];
NSArray *array = @[identity, status, identity]; //Created array with 3 DEVICEID* objects.
NSMutableArray<DEVICEID*>* device_a = [array mutableCopy]; //created mutable copy from An immutable one.
Upvotes: 0
Reputation: 53000
Is this code creating an array of objects,
Yes.
where on each initialization of each element in the array calls the constructor for each object?
It is not clear what you are asking here but maybe if the execution of the statement is explained you will have your answer.
When your statement is executed the steps are as follows:
[device_IDENTIFY command]
, [device_STATUS command]
and [device__POWER command]
; are evaluated. Each will return a reference to an object.@[ ... ]
is evaluated. This is Objective-C syntax to create an immutable array from the enclosed expressions. The result of this evaluation is a reference to a three element array, of type NSArray
, containing the object references from step 1..mutableCopy
method of NSArray
is called on the array from step 2. This method returns a reference to a new mutable array, of type NSMutableArray
.device_a
.The type of device_a
is declared as NSMutableArray <DEVICEID *> *
, this is an example of lightweight generics which were introduced into Objective-C to improve interfacing with Swift. A standard Objective-C mutable array, NSMutableArray
, stores references to objects of any type. The <DEVICEID *>
part of device_a
's type declares that this array should only hold references to objects of type DEVICEID
(or any of its subclasses), and Objective-C will perform compile time checks to enforce this in most (the "lightweight" part) cases.
HTH
Upvotes: 1
Reputation: 3060
A NSMutableArray
is the array in Objective-c which can be modified at the rune time. obviously an array can hold any kind of object it is like . NSMutableArray<T>
holds the generic object , sometimes we need to specify the array before allocating , which type of objects it should hold.
NSMutableArray<DEVICEID*>* device_a;
The above code is used to specify, this will hold a pointer of deviceId object.
@[[device_IDENTIFY command], [device_STATUS command], [device__POWER command]]
this will create and NSArray
which is Immutable
which can't be modified at run time so at last we mutable copy of NSArray
the last line mutableCopy
create a mutable copy .
I hope this will help you .
Upvotes: 1