oc-27-@property的参数

时间:2023-03-09 02:52:03
oc-27-@property的参数

oc-27-@property的参数

//01加强-10

@property .4前

) @property + 手动实现
) @property int age; + @synthesize age;//get和set方法的声明和实现都帮我们做了
) @property int age + @synthesizes age = _b;
@property .4增强 .h @property int age;
) 生成_age
) 生成_age的get和set方法的声明
) 实现_age的get和set方法 // 基本数据类型
int _age;
// set方法的写法
-(void)setAge:(int) age
{
_age = age;
} // 用assign修饰后,仍旧会生成以上标准的set方法
@property (assign) int age; // oc对象类型
@property (retain) Car *car ;
// 用retain修饰后,生成如下内存管理代码
-(void)setCar:(int) car
{
if(_car ! = car)
{
[_car release];
_car = [car retain];
}
} .内存管理相关参数
retain : release旧值,retain新值(用于OC对象)
assign : 直接赋值,不做任何内存管理(默认,用于非OC对象类型)
copy : release旧值,copy新值(一般用于NSString *) //使用@property增强型 生成get和set方法
@property(nonatomic,retain)Car *car;
//.m文件中实际上生成的是
- (void)setCar:(Car *)cat
{
if(_car != car)
{
[_car release];
_car = [car retain];
}
} .@property 参数(二)
、是否要生成set方法(若为只读属性,则不生成)
readonly:只读,只会生成get的声明和实现
readwrite:默认的,同时生成set和get的声明和实现
.多线程管理(苹果在一定程度上屏蔽了多线程操作)
nonatomic:高性能,一般使用这个
atomic:低性能,默认
atomic是Objc使用的一种线程保护技术,基本上来讲,是防止在写未完成的时候被另外一个线程读取,造成数据错误。而这种机制是耗费系统资源的,所以在iPhone这种小型设备上,如果没有使用多线程间的通讯编程,那么nonatomic是一个非常好的选择。
.set和get方法的名称
修改set和get方法的名称,主要用于布尔类型。因为返回布尔类型的方法名一般以is开头,修改 名称一般用在布尔类型中的getter。
控制set方法和get方法的名称
setter : 设置set方法的名称,一定有个冒号:
getter : 设置get方法的名称
@property(nonatomic,assign, setter=abc:,getter=haha)int age
Gamer.h

#import <Foundation/Foundation.h>
#import "House.h" @interface Gamer : NSObject
//{
// House *_house; // 房间
//}
//- (void)setHouse:(House *)house;
//- (House *)house; // @property的完整格式
// @property(参数,参数)成员变量类型 成员变量名称(去掉下划线); // retain:参数用于对象类型,能够帮我们生成set方法的内存管理代码.
// assign:参数,用于基本数据类型,不做内存管理代码. // atomic:对线程加锁,性能低,安全性高(默认).
// nonatomic:不对线程加锁,性能高,安全性低.手机端,建议使用nonatomic // readonly:只能操作,只给外界提供get方法,不提供set方法.
// readwirte:可读可写. // setter:改set方法的名字
// getter:改get方法的名字 @property (nonatomic,retain)House *house;//对象类型用retain
// 外界可以读写.如果,只让外界读取,不能写入时,
@property (atomic,assign,readonly)int age;//基本类型用assign
// 判断玩家是否是vip
//BOOL res = game.isVip;
//BOOL res1 = [game isVip];
@property(nonatomic,assign,getter=isVip,setter=noVip:)BOOL vip;
@end Gamer.m #import "Gamer.h" @implementation Gamer
- (void)dealloc
{
NSLog(@"玩家被释放");
// 当玩家自己被回收时,对房间进行一次release操作.
[_house release];
[super dealloc];
}
//@property已经做了内存管理,下面就不要内存管理了。
//- (void)setAge:(int)age
//{
// _age = age;
//}
//- (void)setHouse:(House *)house
//{
// if (_house != house) {
// //当玩家换房间时,需要对旧房间做一次release操作
// [_house release];
// // 玩家要进入房间,玩家就要对房间进行一次retain操作.
// _house = [house retain];
// }
//
//}
//- (House *)house
//{
// return _house;
//}
@end House.h #import <Foundation/Foundation.h> @interface House : NSObject
@property int no;
@end House.m #import "House.h" @implementation House
- (void)dealloc
{
NSLog(@"%d房间被释放了",_no); [super dealloc];
}
@end main.m #import <Foundation/Foundation.h>
#import "Gamer.h" int main(int argc, const char * argv[]) {
@autoreleasepool {
Gamer *game = [[Gamer alloc] init]; BOOL res = game.isVip;
BOOL res1 = [game isVip];
[game noVip:YES];
}
return ;
}
void demo1(){
Gamer *g = [[Gamer alloc] init];
g.age = ;
NSLog(@"%d",g.age); House *h = [[House alloc] init]; g.house = h; [g release];
[h release];
}
void demo()
{
// 创建1个玩家
Gamer *gamer = [[Gamer alloc] init]; // 玩家1 // 创建1个房间
House *house1 = [[House alloc] init]; // 房间1 1
house1.no = ; // 创建第二个房间
House *house2 = [[House alloc] init]; // 房间2 1
house2.no = ; // 玩家进入房间
gamer.house = house1; // 房间 2 gamer.house = house2; // 2号房间 2 [house1 release]; // 房间 1 // 房间1被释放
[house2 release]; // 房间2 1 // gamer.house = house1; // 操作僵尸对象
// gamer.house = house2; [gamer release]; }