NSMutable array - assign and save objects

I need information on how to assign, save objects.

For example, if we have two view controllers and it is necessary to transfer the array data from viewcontrlr 1 to viewContrl 2, how can we send an object from view 1 to view 2 and release it as 1 and save it as 2.

The simple = operator simply assigns an address that again indicates a view of 1 object. Which is the best way, so we can free obj in view 1 and keep the new object in view 2 when passing from view 1.

+3
source share
2 answers

Create an NSMutableArray in view controller 2 and declare a save property for it.

@interface VC2 : UIViewController
{
   NSMutableArray *mutableArrayInVC2
} 
@property (nonatomic, retain) NSMutableArray *mutableArrayInVC2

:

viewController2Instance.mutableArrayInVC2 = mutableArrayInVC1

:

[mutableArrayInVC1 release];

[ ]

mutableArrayInVC2 mutableArrayInVC1, " " , :

-(void)setMutableArrayInVC2:(NSMutableArray *)arrayValue
{
    [arrayValue retain]; // This is your mutableArrayInVC1
    [mutableArrayInVC2 release]; // This is nil the first time you access it which is cool - we can send messages to nil in ObjC
    mutableArrayInVC2 = arrayValue; // So basically you end up doing and assignment but only after retaining the object so it is pointing to the same memory address BUT it is now 'owned' by your VC2 instance.
}

, !

+2

, . , Cocoa Mac OS X, IMO Cocoa Obj-C . , Obj-C/Cocoa, , .

0

Source: https://habr.com/ru/post/1793478/


All Articles