Why should the iOS agent modified with weak (to a)?

In development we often use a proxy, or write your own agent, and agent properties with weak (assign) modified, seen some developers use strong (retain), but did not find anything wrong, do not know weak (assign) and strong (retain) modified difference

Function realization on the line, consider so much why ~ ~ ~ I can only ha ha ha!

  • Weak: indicates that the object is not responsible for keeping the object delegate, and that the destruction of the object delegate is externally controlled
@property (nonatomic, weak) id< HSDogDelegate> delegate;
  • Strong: the object strongly references delegate, which cannot destroy delegate objects and can cause Retain Cycles
@property (nonatomic, strong) id< HSDogDelegate> delegate;

Perhaps you do not quite understand, it does not matter, the following first example, see the results, and then analysis!

Give an example

  • HSDog class

HSDog.h:

@protocol HSDogDelegate < NSObject> @end @interface HSDog: NSObject, @property (nonatomic, weak) id< HSDogDelegate> delegate; @end

HSPerson.m:

#import, "HSDog.h", @implementation, HSDog - (void) dealloc {NSLog (@ HSDog---- destruction)} @end
  • HSPerson class

HSPerson.h:

@interface HSPerson: NSObject @end

HSPerson.m:

#import "HSPerson.h" #import "HSDog.h" @interface HSPerson (<); HSDogDelegate> dog*/ @property; / * * strong reference (nonatomic, strong) HSDog *dog; @end @implementation HSPerson (instancetype init) {self = [super init]; if (self) self.dog = [[HSDog {/ / instantiate the dog / / dog alloc] init]; delegate self reference. Self retainCount, depending on the modification of delegate, weak:retainCount constant, strong:retainCount + 1 self.dog.delegate = self return self;};} - {(void) dealloc NSLog (@ HSPerson---- destroyed ");} @end
  • Implemented in ViewController:
#import "ViewController.h" #import "HSPerson.h" @interface ViewController (@end @implementation ViewController) - (void) viewDidLoad {[super viewDidLoad]; / / self person to instantiate person, weak references, person retainCount HSPerson [[HSPerson alloc] init] constant *person = @end;}

Result:

  • Weak modifier agent
@property (nonatomic, weak) id< HSDogDelegate> delegate;

Run -> print:

HSPerson---- destroy HSDog---- destroy
  • Strong modifier agent
@property (nonatomic, strong) id< HSDogDelegate> delegate;

-> print:
…. HSPerson and HSDog did not print, not call the dealloc method, the two objects were destroyed in
and this is what we often say the release of memory leak, memory is not released!

Analysis:

  • Using strong
Why should the iOS agent modified with weak (to a)?

Person strong reference to dog

@property (nonatomic, strong) HSDog *dog; person

Self.dog.delegate also makes a strong reference to person, making person + retainCount 1

@property (nonatomic, strong) id< HSDogDelegate> delegate;

When the viewController is not person reference, dog.delegate of person is a strong reference, person retainCount is 1, so the person will not be released, although dog will not release, this is the cause of memory leaks cause circular references!

  • Using weak
Why should the iOS agent modified with weak (to a)?

Person strong reference to dog

@property (nonatomic, strong) HSDog *dog; person

Self.dog.delegate only weakly references to person and does not make person’s retainCount + 1

@property (nonatomic, weak) id< HSDogDelegate> delegate;

So when viewController does not refer to the person, person’s retainCount is 0, or person is released, then dog is released

The article is synchronized with the WeChat public number: hans_iOS, you can send it directly in the public number