何时在类属性上使用self? [英] When to use self on class properties?

查看:108
本文介绍了何时在类属性上使用self?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类属性何时需要自我?例如:

self.MyProperty = @"hi there";

vs

MyProperty = @"hi there";

MyProperty是一个设置为(非原子,副本)的NSString.上面两个在内存管理上有什么区别吗?

当没有属性并且在头文件中声明了变量MyProperty时该怎么办?如果从未在类外部引用该属性,是否需要该属性?它对内存管理有影响吗?

解决方案

是的,内存和性能都有差异.

MyProperty = @"hi there";

这被认为是直接分配.几乎不影响内存或性能.当然,这并不是说这是最佳实践-这是一个不同的问题:)

@property(nonatomic, copy) NSString *MyProperty;
// ...
self.MyProperty = @"hi there";

此语句对内存和性能有重大影响.这基本上等于:

-(void)setMyProperty(NSString *)newValue {
    if (MyProperty != newValue) {
        [MyProperty release];
        MyProperty = [newValue copy];
    }
}

释放旧值,并将新值复制到MyProperty中.这是可以接受的,尤其是在您分配的字符串是可变的(例如,以后可能会更改)的情况下处理字符串时.

如果在您的示例中,您只是分配一个静态字符串(@"hi there"),则直接分配字符串值没有任何问题;效率更高,但是性能差异却很小.

您可以使用@property声明一个属性作为保留,复制或分配(默认为assign).然后,您可以使用@synthesize生成访问器"(getter/setter)方法.这是您在执行时生成的setter方法的外观:

// @property(nonatomic, assign)
-(void)setMyProperty(NSString *)newValue {
    MyProperty = newValue;
}

// @property(nonatomic, retain)
-(void)setMyProperty(NSString *)newValue {
    if (property != newValue) {
        [property release];
        property = [newValue retain];
    }

// @property(nonatomic, copy)
-(void)setMyProperty(NSString *)newValue {
    if (property != newValue) {
        [property release];
        property = [newValue copy];
    }
}

有关ObjectiveC的声明属性.

您可以在@implementation块中使用@synthesize和@dynamic指令来触发特定的编译器操作.请注意,任何给定的@property声明都不需要.

重要提示:如果您未为特定属性指定@synthesize或@dynamic,则必须为该属性提供getter和setter方法(对于只读属性,仅提供getter方法)."

换句话说,如果您声明一个属性但不合成该属性,则除非定义了"MyProperty"和"setMyProperty"方法,否则将无法使用[self MyProperty]或self.MyProperty.如果不声明属性,则只需一个实例变量即可.

注意:@dynamic不会生成访问器.如果您是通过加载代码或动态方法解析来动态(即神奇地)解析访问器方法,则确实可以使用它.

When is self needed for class properties? For example:

self.MyProperty = @"hi there";

vs

MyProperty = @"hi there";

MyProperty is an NSString set as (nonatomic, copy). Is there any difference in memory management for the above two?

What about when there is no property and the variable MyProperty is declared in the header file? Is a property needed if it is never referenced outside of the class? Does it make a difference to memory management?

解决方案

Yes, there is a difference for both memory and performance.

MyProperty = @"hi there";

This is considered a direct assignment. There is practically no memory or performance impact. Of course, that's not to say it's best practice - that's a different question :)

@property(nonatomic, copy) NSString *MyProperty;
// ...
self.MyProperty = @"hi there";

This statement has a significant impact on memory and performance. This is essentially equivalent to:

-(void)setMyProperty(NSString *)newValue {
    if (MyProperty != newValue) {
        [MyProperty release];
        MyProperty = [newValue copy];
    }
}

The old value is released and the new value is copied into MyProperty. This is acceptable and especially typical when dealing with strings when the string your assigning is mutable (ie, it could change later).

If, as in your example, you're simply assigning a static string (@"hi there"), there is nothing wrong with directly assigning the string value; it's more efficient however the difference in performance is trivial.

You can declare a property with @property as retain, copy, or assign (default is assign). You can then generate "accessor" (getter/setter) methods by using @synthesize. Here is what the setter methods look like that are generated when you do so:

// @property(nonatomic, assign)
-(void)setMyProperty(NSString *)newValue {
    MyProperty = newValue;
}

// @property(nonatomic, retain)
-(void)setMyProperty(NSString *)newValue {
    if (property != newValue) {
        [property release];
        property = [newValue retain];
    }

// @property(nonatomic, copy)
-(void)setMyProperty(NSString *)newValue {
    if (property != newValue) {
        [property release];
        property = [newValue copy];
    }
}

More information on ObjectiveC Declared Properties.

"You can use the @synthesize and @dynamic directives in @implementation blocks to trigger specific compiler actions. Note that neither is required for any given @property declaration.

Important: If you do not specify either @synthesize or @dynamic for a particular property, you must provide a getter and setter (or just a getter in the case of a readonly property) method implementation for that property."

In other words, if you declare a property but don't synthesize the property, you won't be able to use [self MyProperty] or self.MyProperty unless you define 'MyProperty' and 'setMyProperty' methods. If you don't declare a property then you simply have an instance variable.

Note: @dynamic doesn't generate the accessors. It's really used if you're dynamically (ie, magically) resolving accessor methods via loading code or dynamic method resolution.

这篇关于何时在类属性上使用self?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆