Loop reference in iOS ARC

Source: Internet
Author: User

Strong: applies to OC objects. It acts the same as retain in non-ARC objects. Its Modified member variables are strongly pointer-type weak: applies to OC objects and works the same as assign in non-ARC objects, modified member variables are weak pointer type assging: applicable to non-OC object types. When the OC object is referenced cyclically, one end is of the strong type, and the other is of the weak type. The sample code is as follows: copy the code to copy the code/******************************* Teacher. h file ***********************************/# import <Foundation/Foundation. h> @ class Student; @ interface Teacher: NSObject @ property (nonatomic, strong) Student * student; @ property (nonatomic, strong) NSString * teacherName; @ end/******************************* Teacher. m file **********************************/# import "Teacher. h "# import" Student. h "@ implementation Teacher-(void) dealloc {NSLog (@" the Teacher object named % @ has been destroyed ", _ teacherName );} @ end/******************************** Student. h file ***********************************/# import <Foundation/Foundation. h> @ class Teacher; @ interface Student: NSObject @ property (nonatomic, weak) Teacher * teahcher; @ property (nonatomic, strong) NSStirng * studentName; @ end/******************************** Student. m file **********************************/# import "Student. h "# import" Teacher. h "@ implementation Student-(void) dealloc {NSLog (@" Student object named % @ has been destroyed ", _ stuName );} @ end/******************************* main. m file **********************************/# import <Foundation/Foundation. h> # import "Teacher. h "# import" Student. h "int main (int argc, const char * argv []) {Teacher * teacher = [[Teacher alloc] init]; teacher. teacherName = @ "instructor Zhang"; Student * student = [[Student alloc] init]; student. stuName = @ "Li "; // The Teacher attribute in the Student Class Object is weak reference student. teahcher = teacher; // The Student attribute in the Teacher Class Object is strongly referenced by teacher. student = student; return 0 ;}

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.