和 getter 方法
属性的声明:(写在.h中)
格式: @property 数据类型 属性名
属性的实现:(写在.m中)
格式: @synthesize 属性名:
@property NSString *gender;
person.gender = @"妖";
NSLog(@"%@",person.gender);//点语法
NSLog(@"%@",[person
gender]);//setter、getter方法
Teacher
: NSObject
NSString
*name,*color;
@property
NSInteger
age;
score;
name,color,age,score;
和getter方法
//@synthesize
name = _name;
- (NSString
*)description
+++++++++description
中不能打印self+++++++++++
// return [NSString
stringWithFormat:@"%@-%ld-%.2lf-%@-%@",
_name,_age,_score,_color,self];
(1)ERROR!!!
return
[NSString
stringWithFormat:@"%@-%ld-%.2lf-%@",
_name,_age,_score,self.color];//这样是可以的
2.实现可以省略实例变量的声明(系统自动帮你生成属性名 加下划线的实例变量
如:_name)
score color
//定义一个Teacher对象
Teacher
*tea =
[[Teacher
alloc]init];
tea.name
=
@"小仓";
tea.age
=
18;
tea.score
=
85.3;
tea.color
=
@"纯白种人";
NSLog(@"%@-%ld-%.2lf-%@",tea.name,tea.age,tea.score,tea.color);
NSLog(@"%@",tea);
主要用来控制setter 和 getter 的方法的实现细节
1.访问控制组
atomic:原子性,修饰的属性同一时刻只能使用setter 或getter,(setter和getter
是互斥的),安全但效率低;
atomic
使用场景:在多线程下为保护数据安全可以使用atomic
nonatomic :非原子性,允许同时使用setter
或getter方法,不安全但效率高,推荐使用nonatomic
2.内存管理组
assign 默认值
基本数据类型使用assign修饰,代理属性使用assign修饰
retain 对象类型一般使用retain
copy
字符串一般用copy修饰,如果要使用copy修饰属性,这个属性必须遵循NSCoping协议
3.读写控制组
readWrite : 可读可写,自动帮你生成setter 和getter 方法
默认值
readOnly :只读,系统只帮你生成getter方法
4.重命名组
setter = setter新名字
getter = getter 新名字
gender
自定义初始化方法
遍历构造器方法
重写description
方法
Boy :
NSObject
//属性修饰符可以写多个,每一个修饰符之间用逗号隔开
//系统默认readwrite
,平时可以不写
@property
(nonatomic,copy,readwrite,setter=setAAA:,getter=AAA)NSString
*name;
@property
(atomic,copy,readwrite)NSString
*gender;
(nonatomic,assign,readonly)NSInteger
age;
(nonatomic,assign,readwrite,getter=isFalg)BOOL
flag;
使用属性可以不写实现部分
Boy
*boy =
[Boy
boyWithName:@"zhangsan"
gender:@"yao"
age:18];
NSLog(@"%@",boy);
readOnly 只读不能写
//
boy.age = 18; error!!
NSLog(@"%ld",boy.age);
//name属性的setter和getter方法起了别名
[boy
setAAA:@"Lisi"];
NSLog(@"%@",[boy
AAA]);
boy.name
=
@"Wanger";
NSLog(@"%@",boy.AAA);
boy.name
=
@"Mazi";
NSLog(@"%@",boy.name);
name,sex,age,height,写属性的属性,自定义初始化方法,便利构造器方法,重写description方法
Girl :
NSObject
@property
(nonatomic,copy)NSString
*name;
@property(nonatomic,copy)
NSString
*sex;
@property
(nonatomic,assign)NSInteger
age;
@property
(nonatomic,assign)CGFloat
height;
//-
(void)sayHi; error!!!
- (id)initWithGirlFriend
: (NSString
*)name sex :
(NSString
*)sex age :
(NSInteger)age
height : (CGFloat)height;
+ (id)girlWithGirlFriend
: (NSString
*)name sex :
(NSString
*)sex age :
(NSInteger)age
height : (CGFloat)height;
Girl
//当属性生成的setter和getter方法的实现,不满足你的需求时,可以重写setter和getter方法
_name;,就是实效的
解决问题的方法
1.只重写setter和getter 方法中的一个
2.自己添加@synthesize name=
_name;
name=
_name;
- (void)setName:(NSString
*)name
{
NSLog(@"你好小姐");
//
_name = name;
//
self.name = name;
//self.name == [self setNane : name];
//
NSLog(@"%@",self.name);
//self.name == [self name];
_name
=
name;
(NSString
*)name{
//
return self.name;
error!!! [self name];
(2)ERROR!!!
return
_name;
}
++++++方法内部是不能用方法调用自己++++++
//-
(void)sayHi{
//
NSLog(@"小仓");
//
[self sayHi];
( 3)
ERROR!!!
(id)initWithGirlFriend
: (NSString
*)name sex :
(NSString
*)sex age :
(NSInteger)age
height : (CGFloat)height{
if
(self
=
[super
init])
{
_name
=
name;
_sex
=
sex;
_age=
age;
_height
=
height;
}
return
self;
}
+ (id)girlWithGirlFriend
: (NSString
*)name sex :
(NSString
*)sex age :
(NSInteger)age
height : (CGFloat)height{
return
[[Girl
alloc]initWithGirlFriend:name
sex:sex
age:age
height:height];
(NSString
*)description
{
return
[NSString
stringWithFormat:@"%@-%@-%ld-%.2lf",
_name,_sex,_age,_height];
键值编码 例题中的调用部分
(void)setValue:(id)value
forUndefinedKey:(NSString
*)key{
NSLog(@"没有在Girl类中找到此key值");
}
- (id)valueForUndefinedKey:(NSString
*)key{
return
nil;
*g
= [Girl
girlWithGirlFriend:@"如花"
sex:@"yao"
age:28
height:175.6];
NSLog(@"%@",g);
g.name
=
@"翠花";
NSLog(@"%@",g.name);
Key-Value-Coding 键值编码
Girl;
@interface
Man :
NSObject
{
NSString
*_name;
}
@property(nonatomic,retain)Girl
*girlFriend;
//@property
(nonatomic,copy)NSString *name; //做例子实例变量
@property
(nonatomic,assign)NSInteger
age;
- (id)initWithName
: (Girl
*)girlFriend
name : (NSString
*)name
age : (NSInteger)age;
+ (id)manWithName
: (Girl
*)girlFriend
name : (NSString
*)name
age : (NSInteger)age;
"Man.h"
#import "Girl.h"
Man
(id)initWithName
: (Girl
*)girlFriend
name : (NSString
*)name
age : (NSInteger)age{
if
(self
=
[super
init])
{
_girlFriend
=
girlFriend;
_age
=
age;
_name
=
name;
}
return
self;
(id)manWithName
: (Girl
*)girlFriend
name : (NSString
*)name
age : (NSInteger)age{
return
[[Man
alloc]initWithName:girlFriend
name:name
age:age];
}
- (NSString
*)description
{
return
[NSString
stringWithFormat:@"%@-%@-%ld",
_girlFriend,_name,_age];
(void)setValue:(id)value
forUndefinedKey:(NSString
*)key
//容易打错
NSLog(@"哈哈,没有找到对应key值");
}
(id)valueForUndefinedKey:(NSString
*)key{
return
nil;
}
Man
*man =
[[Man
alloc]init];
[man setValue:@"妖"
forKey:@"name"];
NSLog(@"%@",[man
valueForKey:@"name"]);
[man
setValue:g
forKey:@"girlFriend"];
NSLog(@"%@",[man
valueForKey:@"girlFriend"]);
//想通过man这个对象给girl
这对象的名字赋值@"棉花"
[[man valueForKey:@"girlFriend"]
setValue:@"棉花"
forKey:@"name"];
//通过KVC把棉花打印出来
NSLog(@"%@",[[man
valueForKey:@"girlFriend"]valueForKey:@"name"]);
//KVC通过路径赋值取值(即使没有是实现setter
和 getter 方法也可以通过KVC赋值和取值)
[man
setValue:@"棉花糖"
forKeyPath:@"girlFriend.name"];
NSLog(@"%@",[[man
valueForKey:@"girlFriend"]valueForKey:@"name"]);
欢迎分享本文,未经许可,不得转载,转载请注明出处!