如何在@属性动画propertyname中给属性附初始值

如何在@property中给属性附初始值_百度知道Discuss - 廖雪峰的官方网站
在哪里可以看到@property的所有属性
Sign In to Make a Comment
You can sign in directly without register:
You need authorize to allow connect to your social passport for the first time.
WARNING: You are using an old browser that does not support HTML5.
Please choose a modern browser ( /
/ ) to get a good experience.今天看啥 热点:
iOS开发中由属性(property)引发的坑
copy修饰的NSMutableArray属性(property)初始化问题
对于属性:
@property (nonatomic, copy) NSMutableArray *someA
若初始化时使用self.someArray:
self.someArray = [[NSMutableArray alloc] initWithCapacity:200];
[self.someArray addObject:name];
APP Crash,其中关键 Error Info如下:
-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x7f9c89701c20
原因是,通过copy修饰的property,若通过self.someArray =来赋值初始化,则是通过系统合成setter方法实现,由于设置copy修饰词,则返回实际上是不可变数组(NSArray),当调用addObject 方法会报错。
初始化 或者 赋值 部分,做如下修改:
_someArray = [[NSMutableArray alloc] initWithCapacity:200];
则APP运行正常,原因是:_someArray是实例变量,实例变量并没有 copy 修饰,指向的仍是定义的 NSMutableArray 类型。所以即使后面通过 self.someArray 使用 addObject 方法仍然可行,因为初始化赋值阶段获取的是NSMutableArray类型对象
知道最佳解决方案么? 其实,就是把copy修饰词改为strong,因为可变数组对象是个容器,只要其元素中没有和self对象存在相互持有造成内存泄漏的,则不会出现任何问题。
@synthesize @dynamic 正确使用
在之前的文章 Objective-C 2.0 基础要点归纳 中,对属性(property)进行了分析,但没有对关键字 @property 和 @synthesize 以及@dynamic的作用进行对比解析,这部分将对之进行详细分析,但问题却是因 atomic 修饰词引出。
关键字的作用说明
@property 预编译命令(关键字)作用是声明属性的 getter和setter方法 @synthesize 生成属性访问器即 getter和setter方法 @dynamic 禁止编译器生成 getter/setter,通过自定义或者在运行时动态创建绑定:主要使用在Core Data中
atomic 是操作原子性,作为属性修饰词,则编译器生产的 getter/setter方法中存在 @synchronized(self)或者lock锁机制只允许原子性访问。
若只自定义一个属性访问器(setter/getter中一个),会出现warning:writable atomic property &name& cannot pair a synthesized getter with &
若两个都定义,则需要 @synthesize name = _ 两个都自定义的情况下,系统不会合成属性访问器,则此时需要显示调用 @synthesize来定义属性的实例变量名,以便使用属性的实例变量
实践代码:
TestAtomic 类
TestAtomic.h
@interface TestAtomic : NSObject
@property (atomic, copy) NSString *
TestAutomic.m
#import TestAtomic.h
@implementation TestAtomic
@synthesize name = _
- (NSString *)name
if (![_name isEqualToString:@]) {
- (void)setName:(NSString *)name
@synchronized(self) {
if (![_name isEqualToString:name]) {
TestB 类,继承TestAtomic类
#import TestAtomic.h
@interface TestB : TestAtomic
@property (atomic, copy) NSString *B
- (instancetype)initWithSuperN
#import TestB.h
@implementation TestB
- (instancetype)initWithSuperName
self = [super init];
if (self) {
[self setName:@this is A];
- (void)print
NSLog(@%@ --- %@, self.Bname, self.name);
Main 函数:
#include TestAtomic.h
#include TestB.h
int main(int argc, const char * argv[])
@autoreleasepool {
TestB *testB = [TestB new];
testB.name = @
//testB = [testB initWithSuperName];
testB.Bname = @this is B;
[testB print];
NSMutableArray问题
error: writable atomic property cannot pair a synthesized setter/getter
iphone 开发中属性 property 和 synthesize 权威的介绍
iOS中atomic的实现解析
@synthesize和@dynamic区别
相关搜索:
相关阅读:
相关频道:
IOS教程最近更新spring 使用注解装配的Bean怎么使用property-placeholder属性配置中的值 - 编程当前位置:& &&&spring 使用注解装配的Bean怎么使用property-placehspring 使用注解装配的Bean怎么使用property-placeholder属性配置中的值&&网友分享于:&&浏览:231次spring 使用注解装配的Bean如何使用property-placeholder属性配置中的值
很久没动笔了
spring 使用注解装配的Bean如何使用property-placeholder属性配置中的值
这个问题不大不小,以前偷懒凡是碰到需要引用属性文件中的类时就改用xml来配置。
今天看了下spring 3 的El,看来有更简单的解决方式,这个自己一直还不知道,赶不上时代了
在XML配置文件中配置一个util:properties
&context:property-placeholder location="/WEB-INF/config.properties"/&
&util:properties id="properties" location="/WEB-INF/config.properties"/&
然后就可以在类中使用注解
@Value("#{properties['jdbc.url']}")
&context:property-placeholder location="/WEB-INF/config.properties"/&@Value("${jdbc.url}")private S需要哪个版本,我怎么没生效
12345678910
12345678910
12345678910 上一篇:下一篇:文章评论相关解决方案 12345678910 Copyright & &&版权所有如何在@property中给属性附初始值_百度知道}

我要回帖

更多关于 property ref属性 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信