Objective-C内存管理第三弹:@property的使用
来源:互联网 发布:儿童文学知乎 编辑:程序博客网 时间:2024/05/22 00:43
//main.m
#import <Foundation/Foundation.h>#import "Person.h"#import "Room.h"int main(int argc, const char * argv[]) { Person * p = [[Person alloc] init]; Room * r = [[Room alloc] init]; [p setSex:YES]; BOOL sex = p.isMan; NSLog(@"%d", sex); p.room = r; [r release]; p.room = r; Room * r2 = [[Room alloc] init]; p.room = r2; [r2 release]; r2 = nil; [p release]; p = nil; return 0;}
//Person.h
/* 内存管理关键字: retain 就在setter和getter方法中加入一些内存管理的代码 当属性为一个普通的OC对象的时候,使用retain assign 直接赋值,不生成内存管理代码 当属性为基本数据类型的时候,使用assign copy复制对象,NSString对象一般使用copy 线程安全的关键字: nonatomic:非原子性,不会生成线程安全的代码,速度快,iOS通常都是这种。 atomic:原子性,会生成线程安全的代码 控制权限: readwrite:可读可写,生成getter与setter方法是声明与实现 readonly:只读,只生成getter方法 修改方法名称: getter 修改生成的getter方法的名称 一般情况下,BOOL类型的属性的getter方法是以is开头的。 setter 修改生成的setter方法的名称 一般不去修改它 */@interface Person : NSObject@property (nonatomic, assign, getter=isMan,setter=setSex:) BOOL man;@property (nonatomic, copy) NSString * name;@property (nonatomic, assign) int age;@property (nonatomic, retain) Room * room;@end
//Person.m
#import "Person.h"@implementation Person- (void)dealloc{// [_room release];// _room = nil; //简写:setter方法中先release再retain self.room = nil; NSLog(@"%s",__func__); [super dealloc];}@end
//Room.h
#import <Foundation/Foundation.h>@interface Room : NSObject@end
//Room.m
#import "Room.h"@implementation Room- (void)dealloc{ NSLog(@"%s",__func__); [super dealloc];}@end
1 0
- Objective-C内存管理第三弹:@property的使用
- Objective-C内存管理第七弹:ARC中@property的使用
- 【objective-c】@property的使用
- Objective - C基础: 第五天 - 4.@property - 内存管理
- Objective-C内存管理(四)@property相关参数详解
- Objective-C的内存管理
- objective-c的内存管理
- Objective C的内存管理
- Objective-C的内存管理
- Objective-C内存管理教程和原理剖析(三)@property (retain)和@synthesize的默认实现
- Objective-C内存管理教程和原理剖析(三)@property (retain)和@synthesize的默认实现
- Objective-C内存管理教程和原理剖析(三)@property (retain)和@synthesize的默认实现
- Objective-c 的 内存管理和@property strong,weak,assign,copy,retain等关键字介绍
- objective-c @property 使用
- Objective-C 中 @property的使用
- objective-c中对象所有权的内存管理(关于set,get方法),以及如何使用@property来进行简易操作(九)
- objective-c中对象所有权的内存管理(关于set,get方法),以及如何使用@property来进行简易操作(九)
- objective-c中对象所有权的内存管理(关于set,get方法),以及如何使用@property来进行简易操作
- UI01-UIView(示例代码)
- C++ 策略模式
- mysql密码mac
- java实现树(链式存储)
- 清空联系人数据
- Objective-C内存管理第三弹:@property的使用
- 一些和图像图形有关的资源
- C程序练习
- 利用Socket建立网络连接的步骤
- 如何使用ShoeBox和PhotoShop制作出漂亮的Fnt字体
- 几个私有代码库托管网站
- 深入学习JavaScript对象(转+学)
- android canvas.draw 注意
- online_judge_1086