ios协议里的属性
2015-08-01 10:57
423 查看
http://blog.csdn.net/yuanchunzi/article/details/47104907
在CSDN上看到有人提的问题,觉得很有价值,先看代码:
上面那个协议时苹果自带的,为什么里面定义了成员变量呢?
这只是在头文件中声明,编译器是不会自动生成实例变量的,也就是说你可以用一个对象去实现这个协议,但是你不自己存储的话实例变量的话,访问同样会崩溃: self.fillModel = @“test”; // unrecognized selector
你好,也就是说我需要这样:
这样才可以么? 也就是@protocol能声明,不能定义变量么?
协议声明出来的是方法,虽然看起来是属性,其实只有 getter、setter 这两个方法,内部是没有实例变量的,并且你不能去重定义它,只能在 getter、setter 里处理
在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 里处理
相关文章推荐
- IOS 实现多种分类 添加删除排序功能(LXReorderableCollectionViewFlowLayout)
- 双系统IOS\windows7 换成Windows10后果
- iOS 宏
- 如何用代码开启“个人热点”功能?如何用代码把iOS设备设置成一个无线路由?
- iOS设计模式 - 装饰
- ios 当键盘出现遮挡视图 视图上移Bug
- iOS
- iOS开发 - Quartz2D绘制小黄人
- iOS开发 - Quartz2D绘图
- IOS中延时执行的几种方式的比较和汇总
- ios开发之给Xcode安装颜色主题及自动注释插件
- Foundation框架提供的一些基本对象的用法之数字对象
- 【转】iOS延迟加载
- 【iOS】利用Runtime特性做监控
- iOS学习(如何添加背景音乐,以及音效)
- iOS设计模式 - 观察者
- xcode6 ios ios simulator Home键
- [IOS] Class Memory Analysis
- iOS Quartz2D - 画圆和圆弧
- iOS 9之Advanced Touch Input(高级触摸输入)