iOS ARC下循環參考的問題

來源:互聯網
上載者:User

strong:適用於OC對象,作用和非ARC中的retain作用相同,它修飾的成員變數為強指標類型weak:適用於OC對象,作用和非ARC中的assign作用相同,修飾的成員變數為弱指標類型assging:適用於非OC物件類型 在OC對象循環參考的時候一端為strong類型,另一段為weak類型 範例程式碼如下:複製代碼複製代碼/****************************** Teacher.h檔案 ***********************************/#import <Foundation/Foundation.h>@class Student;@interface Teacher : NSObject@property (nonatomic,strong) Student *student;@property (nonatomic,strong) NSString *teacherName;@end /****************************** Teacher.m檔案 ***********************************/#import "Teacher.h"#import "Student.h"@implementation Teacher- (void)dealloc{    NSLog(@"叫%@的Teacher對象被銷毀了",_teacherName);}@end /****************************** Student.h檔案 ***********************************/#import <Foundation/Foundation.h>@class Teacher;@interface Student : NSObject@property (nonatomic,weak) Teacher *teahcher;@property (nonatomic,strong) NSStirng *studentName;@end  /****************************** Student.m檔案 ***********************************/#import "Student.h"#import "Teacher.h"@implementation Student- (void)dealloc{    NSLog(@"叫%@的Student對象被銷毀了",_stuName);}@end /****************************** main.m檔案 ***********************************/#import <Foundation/Foundation.h>#import "Teacher.h"#import "Student.h"int main(int argc, const char * argv[]){        Teacher *teacher = [[Teacher alloc] init];    teacher.teacherName  = @"張老師";        Student *student = [[Student alloc] init];    student.stuName = @"李同學";      // Student類對象中的Teacher屬性為弱引用    student.teahcher = teacher;        // Teacher類對象中的Student屬性為強引用    teacher.student = student;     return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.