Objective c @性质与性质

Objective c @性质与性质,objective-c,properties,synthesis,Objective C,Properties,Synthesis,我对目标C很陌生(现在两天了)。当读到@synthesis时,它似乎与我对@property的理解重叠(我以为我理解了)。。。所以,一些细节需要在我的脑海中整理。。。它烦死我了 如果我对@property和@synthesis的区别有错误,请纠正我: 如果您在@界面中声明了一个@属性,那么您正在告诉世界,用户可以使用该属性的标准getter和setter。此外,XCode将为您制作通用的getter和setter。。。。但是,@property声明在多大程度上会发生这种情况?(即,这是否意味着“

我对目标C很陌生(现在两天了)。当读到
@synthesis
时,它似乎与我对
@property
的理解重叠(我以为我理解了)。。。所以,一些细节需要在我的脑海中整理。。。它烦死我了

如果我对
@property
@synthesis
的区别有错误,请纠正我:

如果您在
@界面
中声明了一个
@属性
,那么您正在告诉世界,用户可以使用该属性的标准getter和setter。此外,XCode将为您制作通用的getter和setter。。。。但是,@property声明在多大程度上会发生这种情况?(即,这是否意味着“完全”…就像在
@界面
中未看到的声明,以及在
@界面
中未看到的代码一样

-或-


@property
是否只负责
@接口中的未看到的代码声明
@synthesis
负责
@实现
部分中的未看到的代码实现?)
@property
使用您提供的原子性和setter语义在类上声明属性


使用Xcode 4.4,可以使用autosynthesis,您可以从属性中获得支持ivar,而无需在
@synthesis
中声明。此ivar的形式为
\u propertyName
,其中属性名为
propertyName

。首先,请注意,最新版本的Xcode不再需要@synthesis。你可以(也应该)省略它。也就是说,这就是这些碎片的作用

@property
是访问器的声明。这只是一个声明。以下各项之间的差别很小:

@property (nonatomic, readwrite, strong) NSString *something;
vs

主要区别在于,使用
@property
声明这些方法可以让编译器自动为您生成(合成)实现。没有要求您让编译器为您做这件事。您可以完全自由地手动执行
某些内容
设置某些内容:
,这是很常见的。但是,如果您不手动实现它们,编译器将自动为您创建一个名为
\u something
的ivar,并为getter和setter创建一个合理的实现

在旧版本的Xcode中,必须使用
@synthesis
关键字显式请求自动生成。但这已不再需要。今天,使用
@synthesis
的唯一原因是如果您希望ivar具有非标准名称(千万不要这样做)

这里的一个关键点是,方法
something
setSomething:
只是方法。他们没有什么神奇之处。它们不是特殊的“属性方法”,它们只是按照惯例访问一个状态的方法。这段状态通常存储在ivar中,但不需要存储

更清楚的是:
object.something
并不意味着“从
object
返回名为
\u something
的ivar”,而是意味着“返回
[object something]
的结果,不管它做什么。”返回ivar的值是很常见的


您应该使用
@property
声明声明所有状态(内部和外部),并且应该避免直接声明IVAR。您还应该始终通过属性的访问器(
self.something
)访问属性,但
init
dealoc
方法除外。在
init
dealloc
中,您应该直接使用ivar(
\u something
)。

Objective-C@属性和@synthesis

@属性

  • 生成get/set方法
  • 今天(从使用LLVM v4.0的Xcode v4.4版开始)
    @property
    在内部额外使用了
    @synthesis
    • @合成propertyName=\u propertyName
@synthesis

  • 生成新的iVar或与现有iVar的链接
  • 使用适当的iVar生成get/set方法的实现

@属性

@interface SomeClass : NSObject
@property NSString *foo;
@end

//generated code
@interface SomeClass : NSObject
- (NSString *)foo;
- (void)setFoo:(NSString)newFoo;
@end
@synthesis
模式

@synthesize <property_name> = <variable_name>;

//Using
//1. Specify a variable. New variable(variableName) will be generated/linked with existing
@synthesize propertyName = variableName

//if variableName is not exist it generates: 
//NSString *variableName;

//read access
NSString *temp = variableName;

//2. Default. New variable(propertyName - the same name as a property) will be generated/linked with existing
@synthesize propertyName
//is the same as
//@synthesize propertyName = propertyName

//if propertyName is not exist it generates:  
//NSString *propertyName;

//read access
NSString *temp = propertyName;

//if you specify not-existing <property_name>  you  get
//Property implementation must have its declaration in interface '<class_name>' or one of its extensions
但今天您可以使用下一个语法

@interface SomeClass : NSObject
//1. create property
@property NSString *foo;
@end
接下来,将为这两种情况生成相同的代码

@interface SomeClass : NSObject
{
    //variable
    NSString *_foo; 
}

//getter/setter
- (void)setFoo:(NSString *)newFoo;
- (NSString *)foo;
@end

@implementation SomeClass
- (void)setFoo:(NSString *)newFoo
{
    _foo = newFoo;
}
- (NSString *)foo
{
    return _foo;
}
@end

如果您有一个
@属性
,您很可能需要
@synthesis
,请这样想。这是因为yes
@property
告诉世界需要一个通用的
getter
setter
,但是
@synthesis
实际上是在创建
getter
setter
,如果您愿意,您可以覆盖它并创建自己的
getter
setter
,因为生成的是非常基本的。看看Paul Hegarty的iTunesU
iPad和iPhone应用程序开发
我就是这么想的,它还没有失败。非常感谢大力水手。。。iTunesU太棒了!!!实际上我正在上第四课或保罗的2011系列。我只是在备份,阅读XCode 4学习目标C以及本系列的内容。。。但这对我来说还不是很清楚。这已经一针见血了,简短而简单+1是的。。。我知道您可以使用不同的ivar(您的类应该使用它,尤其是getter和setter)来“支持您的属性var”。。。非常感谢您的回复。。。我已经被有帮助的回复的速度弄得不知所措。。。(这是我加入几分钟后的第一篇帖子)!-谢谢你花时间写这篇文章,艾伦!罗布,非常感谢你的时间和回复。在iTunes U上观看去年(2011年)的iOS视频。。。并试图跟上XCode m的变化
@interface SomeClass : NSObject
//1. create property
@property NSString *foo;
@end
@interface SomeClass : NSObject
{
    //variable
    NSString *_foo; 
}

//getter/setter
- (void)setFoo:(NSString *)newFoo;
- (NSString *)foo;
@end

@implementation SomeClass
- (void)setFoo:(NSString *)newFoo
{
    _foo = newFoo;
}
- (NSString *)foo
{
    return _foo;
}
@end