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