Objective-c-ARC (Automatic Reference counting) automatic reference technical details

Source: Internet
Author: User
ARC characteristics and judgment criteria
/ *
 ARC's criterion: as long as there is no strong pointer to the object, the object will be released


 1.ARC Features
 1> Calling release, retain, retainCount is not allowed
 2> Dealloc can be overridden, but calling [super dealloc] is not allowed
 3> @ property's parameters
  * strong: member variable is a strong pointer (for OC object type)
  * weak: member variable is a weak pointer (for OC object type)
  * assign: applicable to non-OC object types
 4> The previous retain was changed to strong

 There are 2 types of pointers:
 1> Strong pointer: By default, all pointers are strong pointers __strong
 2> Weak pointer: __weak

 * /

int main ()
{
    Dog * d = [[Dog alloc] init];

    Person * p = [[Person alloc] init];
    p.dog = d;

    d = nil;

    NSLog (@ "% @", p.dog);

    return 0;
}

void test ()
{
    // Wrong writing (meaningless writing)
    __weak Person * p = [[Person alloc] init];


    NSLog (@ "% @", p);

    NSLog (@ "------------");
}
@class Dog;

@interface Person: NSObject

@property (nonatomic, strong) Dog * dog;


@property (nonatomic, strong) NSString * name;

@property (nonatomic, assign) int age;

@end
@implementation Person

-(void) dealloc
{
    NSLog (@ "Person is dealloc");

    // [super dealloc];
}

@end
@interface Dog: NSObject

@end
@implementation Dog
-(void) dealloc
{
    NSLog (@ "Dog is dealloc");
}
@end
ARC circular reference issue

/ **
 * When circularly referenced at both ends, the solution:
 1> ARC
 Use strong on one end and weak on the other

 2> Non-ARC
 Use retain on one end and assign on the other end
 * /
int main ()
{
    Person * p = [[Person alloc] init];


    Dog * d = [[Dog alloc] init];
    p.dog = d;
    d.person = p;

    return 0;
}
@class Dog;

@interface Person: NSObject

@property (nonatomic, strong) Dog * dog;

@end
@implementation Person

-(void) dealloc
{
    NSLog (@ "Person--dealloc");
}

@end
@class Person;

@interface Dog: NSObject

@property (nonatomic, weak) Person * person;

@end
@implementation Dog
-(void) dealloc
{
    NSLog (@ "Dog--dealloc");
}
@end
Objective-C-ARC (Automatic Reference Counting)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.