美文网首页将来跳槽用
OC基础学习9:属性

OC基础学习9:属性

作者: Andy_Ron | 来源:发表于2017-09-12 10:52 被阅读19次

1 使用属性

  • 接口代码简化
    没有使用属性:
#import "Tire.h"

@interface AllWeatherRadial : Tire
{
    // 轮胎在潮湿和积雪的道路上的性能
    float rainHandling;
    float snowHandling;
}
- (void) setRainHandling:(float)rainHandling;
- (float) rainHandling;
- (void) setSnowHandling:(float)snowHandling;
- (float) snowHandling;
@end

使用属性:

@interface AllWeatherRadial : Tire
{
    // 轮胎在潮湿和积雪的道路上的性能
    float rainHandling;
    float snowHandling;
}

@property float rainHandling;
@property float snowHandling;

@end

@ property预编译指令的作用是自动声明属性的setter和getter方法。

  • 简化实现代码
    之前:
#import "AllWeatherRadial.h"

@implementation AllWeatherRadial
-(NSString *)description
{
//    return (@"I am a tire for rain or shine.");
    NSString *desc;
    desc = [[NSString alloc] initWithFormat:@"AllWeatherRadial: %.1f / %.1f / %.1f / %.1f.", self.pressure, self.treadDepth, self.rainHandling, self.snowHandling];
    return desc;
}
- (void)setRainHandling:(float)r
{
    rainHandling = r;
}
- (float)rainHandling
{
    return rainHandling;
}
- (void)setSnowHandling:(float)s
{
    snowHandling = s;
}
- (float)snowHandling
{
    return snowHandling;
}

- (id) initWithPressure:(float)p treadDepth:(float)td
{
    if (self = [super initWithPressure:p treadDepth:td]) {
        rainHandling = 23.7;
        snowHandling = 58.1;
    }
    return self;
}
@end

使用属性后的实现代码:

#import "AllWeatherRadial.h"

@implementation AllWeatherRadial

@synthesize rainHandling;
@synthesize snowHandling;

-(NSString *)description
{
    NSString *desc;
    desc = [[NSString alloc] initWithFormat:@"AllWeatherRadial: %.1f / %.1f / %.1f / %.1f.", self.pressure, self.treadDepth, self.rainHandling, self.snowHandling];
    return desc;
}

- (id) initWithPressure:(float)p treadDepth:(float)td
{
    if (self = [super initWithPressure:p treadDepth:td]) {
        rainHandling = 23.7;
        snowHandling = 58.1;
    }
    return self;
}
@end

@synthesize预编译指令表示通知编译器生成访问方法。当遇到@synthesize xxx;这行代码时,编译器将添加实现-setXxx:和-xxx方法的预编译代码。

该@synthesize预编译指令不同于代码生成。你永远看不到实现-setXxx:和-xxx的代码,但是这些方法确实存在并可以被调用。这种技术使苹果公司可以更加灵活地改变Objective-C中生成访问方法的方式,并获得更安全的实现和更高的的性能。在Xcode4.5以后的版本中,可以不必使用@synthesize了。

  • 所有的属性都是基于变量的,在@synthesize时,编译器会自动创建与属性名称相同的实例变量。

    实例变量声明位置:

    • 头文件:有子类,想从子类直接通过属性访问变量。
    • 实现文件:变量只属于当前类。
  • 点表达式
    之前的
    [tire setPressure:23+i];
    可相应变为:
    tire.rainHandling = 23+i;

2 属性扩展

  • 实例变量和属性不是同一个名称
@interface Car : NSObject
{
    NSMutableArray *tires;
    Engine *engine;
    NSString *appellation;
}
@property (copy) NSString *name;
@property (retain) Engine *engine;

修改@synthesize指令为: @synthesize name = appellation;
编译器仍将创建-setName:-name方法,但在其实现代码中用的确实appellation实例变量。

  • 只读属性
    readwrite readonly
    属性为只读时,编译器只会生成一个getter方法。

  • 属性不支持那些需要接收额外参数的方法。如car对象中tire对象的代码。

相关文章

  • OC基础学习9:属性

    1 使用属性 接口代码简化没有使用属性: 使用属性: @ property预编译指令的作用是自动声明属性的sett...

  • OC运行时

    我个人对OC的理解就是:动态获取OC Class的属性和方法,从而动态改变Class方法和属性。之前学习Java的...

  • OC基础-category(4)关联对象添加成员变量

    OC基础 - 类添加成员变量属性 = ivar + setter(声明,实现) + getter(声明,实现)类扩...

  • OC基础-@property 属性(七)

    @property 的本质本质 @property = ivar + getter + setter;(成员变量 ...

  • OC学习(3)-属性

    不同关键字的使用场景 使用assign:对基础数据类型(NSInteger)和C数据类型(int,float,do...

  • 四、Objective-C 属性声明和点语法

    Objective-C学习索引 一、属性是什么 属性(property):一个对象的属性或特性,OC中可以简单理解...

  • @property指示符copy相关知识

    近期空闲时间在重新加深OC基础相关方面的学习,这里记录一下属性指示符copy相关知识和引申出来的对象复制等知识。 ...

  • 属性

    存储属性其实Swift中的存储属性就是以前学习OC中的普通属性, 在结构体或者类中定义的属性, 默认就是存储属性*...

  • OC基础篇:内存管理、属性

    内存管理 为什么要了解内存管理? 了解内存管理之后,其实发现很多东西与我们编写代码关系不大,但是为什么还要了解它呢...

  • OC:属性

    目录 属性演变过程 属性定义 属性扩展 属性演变过程 上篇博文《OC学习备忘录:成员变量、方法》中演示了如何声明成...

网友评论

    本文标题:OC基础学习9:属性

    本文链接:https://www.haomeiwen.com/subject/gmztsxtx.html