oc点语法的使用
(摘自http://www.cnblogs.com/wendingding/p/3705658.html)
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
// insert code here...
Person *person = [[Person alloc] init];
//[person setAge:10];
person.age = 10;//点语法,等效与[person setAge:10];
//这里并不是给person的属性赋值,而是调用person的setAge方法
//int age = [person age];
int age = person.age;//等效与int age = [person age]
NSLog(@"age is %i", age);
[person release];
}
return 0;
}
又如人遛狗中 Person.h为
#import "Dog.h"
@interface Person:NSObject
{
Dog *_dog;
}
//原-(void)setDog:(Dog *)aDog;
//-(Dog *) dog;
@property(retain) Dog *dog;
@end
Person.m中
#import "Person.h"
@implementation Person
@synthesize dog=_dog;
/*
-(void)setDog:(Dog *)aDog
{
if(aDog!=_dog){
[_dog release];
_dog =[aDog retain];
}
}
-(Dog *)dog
{
return _dog;
}
*/
-(void)dealloc
{
//[_dog release];
//_dog=nil;
self.dog = nil;
//[self setDog:nil];
[super dealloc];
}
@end
oc点语法的本质
点语法的本质是方法的调用,而不是访问成员变量,当使用点语法时,编译器会自动展开成相应的方法。和Java中的那种使用对象变量直接访问public类型的变量的点是不一样的。
在OC中使用点语法有两个条件:
1、必须是属性
2、有对应的get/set方法
Stu.age=10;展开为:[stu setAge:10];
int a=stu.age;展开为:[stu age];
OC中的get方法和Java中不太一样,OC中只要一个方法有返回值都可以认为是get方法
#import <Foundation/Foundation.h>
@interface Person : NSObject{
//点语法只能适用于set/get方法,如果没有对应的set/get方法的话,是不能使用.语法的
//这里的get方法和其他语言不一样,只要有返回值就是get方法
NSString *_name;
NSInteger _age;
}
- (void)setName:(NSString *)name;
- (void)setAge:(NSInteger)age;
- (NSString *)name;
- (NSInteger) age;
//这里要注意,showInfo也是一个get方法
- (NSString *)showInfo;
//只定义,不实现,所以在调用[person test]的时候,编译时候不会报错
//但是运行的时候就会报错了,因为编译的时候只会判断方法有没有定义,而运行的时候是需要判断这个方法有没有实现
- (void)test;
@end
#import "Person.h"
@implementation Person
- (void)setName:(NSString *)name{
_name = name;
NSLog(@"setName is Execute");
}
- (void)setAge:(NSInteger)age{
//这里要注意,如果我们定义的全局变量是age而不是_age的话
//这里不能用age = age进行赋值
//但是也不能使用self.age = age,因为self.age等价于[self setAge:age]这样就会出现死循环了
//所以我们在定义全局变量的时候一般都会加上一个下划线以便区分
_age = age;
NSLog(@"setAge is Execute");
}
- (NSString *)name{
NSLog(@"getName is Execute");
return _name;
}
- (NSInteger) age{
NSLog(@"getAge is Execute");
return _age;
}
- (NSString *)showInfo{
NSLog(@"showInfo is Execute");
return _name;
}
@end
oc点语法属性
(摘自http://www.cnblogs.com/X-Code/archive/2013/01/17/2865165.html)
oc当中通过@property和 @synthesize 配对使用来自动生成变量的set和get方法
@interface Person:NSObject
{
int _age;
int _height;
}
@property int age;
//1、相当于下面两句,可写多个变量如@property int age,height
//2、只能写在@interface中
//- (void)setAge:(int)age;
//- (int)age;
@end
@implementation
{
@synthesize age=_age;
//1、相当于下面,可写多个变量如@synthesize age=_age,height=_height;
//2、上面的int _age,可不写,当访问_age不存在时,会自动生成@private类型的_age变量
//3、xcode4.4后@synthesize也可省略,只写@property即可,此时成员变量也是私有的,若想成员变量是保护的,要加上@protect int _age;
//4、若写为@synthesize age;会默认访问age
//5、只能写在@implementation中
//6、若手动实现了setter方法,编译器只会生成getter方法...
//-(void)setAge:(int) age
//{
//_age= age;
//}
//-(int)age
//{
//return _age;
//}
}
@end
- @property有以下几种属性
readwrite (缺省),readonly 只读(只有get方法,禁用set方法);
atomic :开启多线程变量保护,会消耗一定的资源(非原子性,保证多线程安全),nonatomic:禁止多线程变量保护,提高性能 ;
assign(缺省):直接赋值,适用于基本数据类型(非对象类型),retain:赋值时做内存优化,适用于对象类型,copy:复制一个副本,适用于特殊的对象类型(常用于NSstring)(有NSCoping协议的才可以用copy)
- assign retain copy的setter方法的内部实现
assign:
@property float price;
内部实现:
- (void)setPrice:(float)price
{
_price = price;
}
-(float)price
{
return _price;
}
retain:
@property (retain, nonatomic) NSString *company;
内部实现:
- (void)setCompany:(NSString *)company{
if(_company != company){
[_company release];
[company retain];
_company = company;
}
}
copy:
@property (copy, readwrite, nonatomic) NSString *company;
内部实现:
- (void) setCompany:(NSString *)company{
if(_company != company){
[_company release];
[company copy];
_company = company;
}
}

489

被折叠的 条评论
为什么被折叠?



