一、@property 关键字
注意:@property 关键字是编译器特性,让xcode可以自动生成getter和setter的声明。
@property int age;
编译时遇到这一行,则自动扩展成下面两句:
- (void)setAge:(int)age;
- (int)age;
二、@synthesize关键字
@synthesize关键字帮助生成成员变量的setter和getter方法的实现。@synthesize age = _age;
相当于生成以下代码:
- (void)setAge:(int)age
{
_age = age;
}
- (int)age
{
return _age;
}
三、@property @synthesize 的使用
声明部分
//
// Person.h
// 06-@property和@synthesize
//
// Created by rui on 4/3/15.
// Copyright (c) 2015 itcast. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
int _age;
double _height;
}
// 自动生成_age成员变量的setter和getter声明
@property int age;
//- (void)setAge:(int)age;
//- (int)age;
// 自动生成_height成员变量的setter和getter声明
@property double height;
//- (void)setHeight:(double)height;
//- (double)height;
@end
实现部分:
//
// Person.m
// 06-@property和@synthesize
//
// Created by rui on 4/3/15.
// Copyright (c) 2015 itcast. All rights reserved.
//
#import "Person.h"
@implementation Person
@synthesize age = _age;
@synthesize height = _height;
//- (void)setAge:(int)age
//{
// _age = age;
//}
//
//- (int)age
//{
// return _age;
//}
//
//- (void)setHeight:(double)height
//{
// _height = height;
//}
//
//- (double)height
//{
// return _height;
//}
@end
测试程序:
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
Person *p = [Person new];
p.age = 23;
NSLog(@"Person's age is %d", p.age);
return 0;
}
编译链接之后,运行程序:
2015-04-03 15:37:21.554 06-@property和@synthesize[1454:127007] Person's age is 23
从XCode4.4b版本以后,采用如下新方法:
Person类的声明和实现:
#import <Foundation/Foundation.h>
@interface Car : NSObject
//{
// double _speed;<pre name="code" class="objc">//}
@property double speed;@property int wheels;@end
<pre name="code" class="objc">#import "Car.h"
@implementation Car
// 会访问—_speed成员变量,如果_speed不存在,那么就自动生成_speed成员变量,并且属性还是@private
//@synthesize speed = _speed;
//@synthesize wheels = _wheels;
@end
// int _wheels;//}
四、使用注意
(1)在老式的代码中,@property只能写在@interface @end中,@synthesize只能写在@implementation @end中,自从xcode 4.4后,@property就独揽了@property和@synthesize的功能。
(2)@property int age;这句话完成了3个功能:1)生成_age成员变量的get和set方法的声明;2)生成_age成员变量set和get方法的实现;3)生成一个_age的成员变量。
注意:这种方式生成的成员变量是private的。
(3)可以通过在{}中加上int _age;显示的声明_age为protected的。
(4)原则:get和set方法同变量一样,如果你自己定义了,那么就使用你已经定义的,如果没有定义,那么就自动生成一个。
五、id
在OC中,id类型是一个独特的数据类型。在概念上,类似于Java的Object类,可以装换为任何数据类型。换句话说,id类型的变量可以存放任何数据类型的对象。在内部处理上,这种类型被定义为指向对象的指针,实际上是一个指向这种对象的实例变量的指针。例如,下面定义了一个id类型的变量和返回一个id类型的方法:-(id) newObject : (int) type;
id 和void *并非完全一样,下面是id在objc.h中的定义:
typedef struct objc_object
{
Class isa;
} *id;
从上面看出,id 是指向struct objc_object的一个指针。也就是说,id 是一个指向任何一个继承了Object类的对象。需要注意的是id 是一个指针,所以在使用id的时候不需要加星号,比如:
id foo = nil;
上述语句定义了一个nil指针,这个指针指向NSObject 的任意一个类,而”“id *foo = nil;”则定义了一个指针,这个指针指向另一个指针,被指向的这个指针指向NSObject的一个类。
在OC中,id 取代了int类型称为默认的数据类型,关键字nil被定义为空对象,也就是值为0的对象。
下面距离说明:
声明和实现一个Person对象
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property int age;
@end
// 实现Person类
#import "Person.h"
@implementation Person
@end
测试id类型:
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
// 万能指针,能曹总任何OC对象
id d = [Person new];
// [d setAge:23];
[d setAge:24];
NSLog(@"Person's age is %d", [d age]);
return 0;
}
运行测试程序:
2015-04-03 16:37:15.837 01-id[1649:147711] Person's age is 24