简体   繁体   English

Xcode 4.5中@property和ivar的区别

[英]Difference between @property and ivar in Xcode 4.5

Previously I have always seen example of use of properties and iVars like this... 以前我一直看到使用属性和iVars这样的例子......

Inside SomeClass.h 在SomeClass.h里面

@interface SomeClass : NSObject {
    NSString *_someString;
}

@property NSString *someString;

@end

Then in SomeClass.m 然后在SomeClass.m中

#import "SomeClass.h"

@implementation SomeClass

@synthesize someString = _someString;

@end

More recently (in the WWDC 2012 videos) I've heard that we no longer need @synthesize and it's recommended to just use the @property without it's associated iVar. 最近(在WWDC 2012视频)我听说,我们不再需要@synthesize和它的建议只使用@property没有它相关的伊娃。

So the above would change to... 所以上面会变成......

SomeClass.h SomeClass.h

@interface SomeClass : NSObject

@property NSString *someString;

@end

SomeClass.m SomeClass.m

#import "SomeClass.h"

@implementation SomeClass

@end

This is just using the @property and no ivar. 这只是使用@property而没有ivar。 This makes sense and I have been using it. 这是有道理的,我一直在使用它。

However, I have also seen examples of... 但是,我也看过......的例子

SomeClass.h SomeClass.h

@interface SomeClass : NSObject

@end

SomeClass.m SomeClass.m

#import "SomeClass.h"

@interface SomeClass () {
    NSString *someString;
}

@end

@implementation SomeClass

@end

In this they just have a private iVar and no @property . 在这一点,他们只是有一个私人iVar也没有@property

So what's the difference? 那有什么区别? I understand that the @property gives the accessor methods too but you don't have to override the accessor methods. 我知道@property提供了访问器方法,但您不必重写访问器方法。 You can just use default accessors. 您可以使用默认访问者。

So when would you use @property and not an ivar and when would you use just an ivar and not a @property ? 那么你什么时候使用@property而不是ivar,什么时候才能使用ivar而不是@property And why wouldn't you just get rid of ivars completely and just use @properties ? 为什么你不只是摆脱实例变量的完整,只是使用@properties If they need to be private then just use them inside the interface extension in the .m . 如果它们需要是私有的,那么只需在.m中的接口扩展中使用它们。

Quick edit in response to an answer about memory management. 快速编辑以响应有关内存管理的答案。 I'm using ARC and for me it would seem that I have more control over memory management with using strong and weak @properties than I do with using iVars. 我使用ARC,对我来说似乎是我有过的内存管理,使用更多的控制strongweak @properties比我用的ivars。

I hope the question is clear enough. 我希望这个问题足够清楚。

Thanks 谢谢

In general you can always use properties. 通常,您始终可以使用属性。 If you have "assign" property you can use ivar beause you don't need any memory management in your getters/setters. 如果您有“assign”属性,则可以使用ivar,因为您的getter / setter中不需要任何内存管理。 But if you should retain objects it is just inconvinient to use ivars because you have to manually retain/release them while properties do that for you. 但是如果你应该保留对象,那么使用ivars是不方便的,因为当属性为你做这件事时你必须手动保留/释放它们。

Also If you use ivars you don't have control over setting/getting values. 此外,如果您使用ivars,则无法控制设置/获取值。 In general it is a bad practive to use public fields instead of getters or setters. 一般来说,使用公共字段而不是getter或setter是一个不好的方法。 For example it is not ok if you can set negative value for field that store person's age. 例如,如果您可以为存储人员年龄的字段设置负值,那就不行了。

And you can't use KVO with ivars 而你不能使用伊娃的KVO

I think its depends on fact that you have access to public ivars with " -> " syntax. 我认为这取决于您可以使用“ -> ”语法访问公共ivars。 Maybe previously, if you declare a readonly property without @private ivar you can access to it ivar with " -> " syntax. 也许以前,如果你声明一个没有@private ivar的readonly属性,你可以使用“ -> ”语法访问它。 (maybe its breaks encapsulation). (也许它打破封装)。

But now if you declare something like this 但现在如果你宣布这样的话

{
@private NSArray *a;
@protected NSArray *b;
@public NSArray *c;
}
@property (nonatomic, retain) NSArray *d; 

You can access with " -> " only to " c " ivar, just try this code. 您只能使用“ -> ”访问“ c ”ivar,只需尝试此代码即可。 with a,b and d it will be warning or error. 用a,b和d它将是警告或错误。

If it somehow helps, I will be glad. 如果它有所帮助,我会很高兴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM