iphone – initWithArray seems to give a reference to source array rather then a new array – Education Career Blog

In my code I use initWithArray like so :

vendorArray = NSMutableArray alloc initWithArray:resultsCore.availableVendors;

But changes I make to the vendorArray also seem to be made to restultsCore.availableVendors.

This was not what I had desired.

I want populate vendorArray with restultsCore.availableVendors but then leave it untouched by the changes I make to vendorArray.

Is there away to get this functionality without using an for and if statements to copy the data object by object into vendorArray from restultsCore.availableVendors ?

Many Thanks


EDIT Below is my edit.

Vendors is an object i made

@interface Vendor : NSObject 
NSString        *vendorID;
NSMutableArray  *availableCars; 
BOOL            atAirport;

@property (nonatomic, copy) NSString *vendorID;
@property (nonatomic, retain) NSMutableArray *availableCars;
@property (nonatomic, assign) BOOL atAirport;

- (id)initFromVehVendorAvailsDictionary:(NSDictionary *)vehVendorAvails;


As you can see it has an array.

This array is another object I made called Car.

@interface Car : NSObject 

NSMutableArray *vehicleCharges; // These are extras like breakdown assist etc
NSString *rateQualifier;
BOOL needCCInfo;
NSMutableArray *fees; // This array contains 3 dictionarys
NSMutableArray *pricedCoverages;

@property (nonatomic, retain) NSMutableArray *vehicleCharges;
@property (nonatomic, copy) NSString *rateQualifier;
@property (nonatomic, assign) BOOL needCCInfo;
@property (nonatomic, retain) NSMutableArray *fees;
@property (nonatomic, retain) NSMutableArray *pricedCoverages;

- (id) initFromVehicleDictionary:(NSDictionary *)vehicleDictionary;


Car also has 3 arrays of my other objects. Its all very object oriented -.-

I’ve been reading up on NSCopying and saw a tutorial. But I still feel really lost as what to do here.

Do I need to implement the NSCopying protocol for my Vendors class, Car class and the other 3 classes that are contained in the arrays by car.

Vendors is the object contained in the array i want to copy. So is this the only class i need to implement the NSCopying tutorial for?

Many thanks, sorry for the long question


vendorArray = NSMutableArray alloc initWithArray:resultsCore.availableVendors copyItems:YES;

Don’t forget to implement the NSCopying protocol in the classes for the objects to be copied.

Leave a Comment