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;
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.