@properties(MRC)的显式获取器/设置器 [英] Explicit getters/setters for @properties (MRC)

查看:79
本文介绍了@properties(MRC)的显式获取器/设置器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2012年中,当ARC取代MRC成为通用惯例而使后者几乎不再需要学习时,我已经开始使用Objective-C语言进行编程.

I've started programming on Objective-C language in the middle of 2012 in the time when ARC replaced MRC as a general practice making the latter almost unnecessary to learn.

现在,我正在尝试了解MRC的一些基础知识,以加深我对Objective-C中的内存管理的了解.

Now I am trying to understand some basics of MRC to deepen my knowledge of Memory Management in Objective-C.

我现在感兴趣的是如何手动为明确声明的@properties编写getters/setters.

The thing I am interested in now, is how to write getters/setters for declared @properties explicitly, by hands.

这一次,我发现的唯一明智的示例来自Apple的高级内存管理编程指南":

By this time the only sane example I found is from "Advanced Memory Management Programming Guide" by Apple:

@interface Counter : NSObject {
    NSNumber *_count;
}
@property (nonatomic, retain) NSNumber *count;
@end;

- (NSNumber *)count {
    return _count;
}

- (void)setCount:(NSNumber *)newCount {
    [newCount retain];

    [_count release];

    _count = newCount;
}

我的猜测是,要使(非原子的,复制的)内容相同,我应该写一些类似的东西:

My guess is that to make the same for (nonatomic, copy) I should write something like:

- (NSNumber *)count {
    return _count;
}

- (void)setCount:(NSNumber *)newCount {
    [_count release];

    _count = [newCount copy];
}

所以问题是关于其他组合,我不确定:

So the question is about the rest of combinations I am not sure about:

我要感谢能够向我展示如何为使用手动引用计数(MRC)的以下@property声明编写显式getter/setter的示例:

I would thankful for someone who could show me examples of how explicit getters/setters should be written for the following @property declarations given Manual Reference Counting (MRC) is used:

1. @property (nonatomic, retain) NSNumber *count;
2. @property (nonatomic, copy) NSNumber *count;
3. @property (atomic, retain) NSNumber *count;
4. @property (assign) NSNumber *count;
5. What else is used often under MRC? (please share, if any other combinations exist)

推荐答案

@Gabriele答案正确时,我想编写自己的答案,其中包含:

While @Gabriele answer is correct, I want to write my own answer containing:

  1. 我对链接主题的研究:可变ivar的不可变属性使用MRC
  2. @robmayoff的评论
  3. objc运行时
  1. My research in linked topic: Immutable property for mutable ivar using MRC
  2. Comment by @robmayoff
  3. exploration of objc runtime


1) @property (nonatomic, retain) NSNumber *count;


1) @property (nonatomic, retain) NSNumber *count;

- (NSNumber *)count {
    return _count;
}

- (void)setCount:(NSNumber *)count {
    if (count != _count) {
        id oldValue = _count;
        _count = [count retain];
        [oldValue release];
    }
}

2) @属性(非原子,副本)NSNumber * count;

- (NSNumber *)count {
    return _count;
}

- (void)setCount:(NSNumber *)count {
    id oldValue = _count;
    _count = [count copy]; // retains (+1)
    [oldValue release];
}

注意:由于(副本)会生成副本(objc运行时源也具有这种行为),因此无需在if (count != _count)中进行检查.

Note: no need in if (count != _count) check since (copy) produces copies (objc runtime source also behaves this way).

3) @属性(原子,保留)NSNumber * count;

- (NSNumber *)count {
    NSNumber *count;
    @synchronized(self) {
        count = [_count retain]; // +1
    }
    return [count autorelease]; // delayed -1
}

- (void)setCount:(NSNumber *)count {
    id oldValue;
    @synchronized(self) {
        oldValue = _count;
        _count = [count retain];
    }
    [oldValue release];
}

4) @属性(分配)NSNumber * count;

- (NSNumber *)count {
    NSNumber *count;
    @synchronized(self) {
        count = _count;
    }
    return count;
}

- (void)setCount:(NSNumber *)count {
    @synchronized(self) {
        _count = count;
    }
}


P.S.最近,我对这种过去的手动参考计数"进行了一些研究,让我与您分享以下一些链接,这些链接是我发现与该主题最相关的:


P.S. Recently I did some research for this back-to-the-past Manual Reference Counting, let me share with you the following links which I found to be the best on this topic:

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