您的位置:首页 > 移动开发 > IOS开发

ios协议里的属性

2015-08-01 10:57 423 查看
http://blog.csdn.net/yuanchunzi/article/details/47104907

在CSDN上看到有人提的问题,觉得很有价值,先看代码:
@protocol CAMediaTiming

/* The begin time of the object, in relation to its parent object, if
* applicable. Defaults to 0. */

@property CFTimeInterval beginTime;

/* The basic duration of the object. Defaults to 0. */

@property CFTimeInterval duration;

/* The rate of the layer. Used to scale parent time to local time, e.g.
* if rate is 2, local time progresses twice as fast as parent time.
* Defaults to 1. */

@property float speed;

/* Additional offset in active local time. i.e. to convert from parent
* time tp to active local time t: t = (tp - begin) * speed + offset.
* One use of this is to "pause" a layer by setting `speed' to zero and
* `offset' to a suitable value. Defaults to 0. */

@property CFTimeInterval timeOffset;

/* The repeat count of the object. May be fractional. Defaults to 0. */

@property float repeatCount;

/* The repeat duration of the object. Defaults to 0. */

@property CFTimeInterval repeatDuration;

/* When true, the object plays backwards after playing forwards. Defaults
* to NO. */

@property BOOL autoreverses;

/* Defines how the timed object behaves outside its active duration.
* Local time may be clamped to either end of the active duration, or
* the element may be removed from the presentation. The legal values
* are `backwards', `forwards', `both' and `removed'. Defaults to
* `removed'. */

@property(copy) NSString *fillMode;

@end


问题:

上面那个协议时苹果自带的,为什么里面定义了成员变量呢?


网友回答:

这只是在头文件中声明,编译器是不会自动生成实例变量的,也就是说你可以用一个对象去实现这个协议,但是你不自己存储的话实例变量的话,访问同样会崩溃: self.fillModel = @“test”; // unrecognized selector


追问

你好,也就是说我需要这样:
@interface MyClass : NSObject < CAMediaTiming >
@property CFTimeInterval beginTime;
@end


这样才可以么? 也就是@protocol能声明,不能定义变量么?


网友回答

协议声明出来的是方法,虽然看起来是属性,其实只有 getter、setter 这两个方法,内部是没有实例变量的,并且你不能去重定义它,只能在 getter、setter 里处理 ​
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: