English 中文(简体)
计算数组属性的内存语义?
原标题:Memory semantics of a computed array property?

这是用于一个允许用户标记事物的应用程序。 标记只是字符串 。

TagHolder 对象的数组持有应用程序中使用的所有标记列表, 并有一个布林告知是否选中了标记, 但这是执行细节 。

外部界面需要两种方法,即 选择 Tags setSectiveTags: 返回并接受字符串数组的方法。

我希望这两种方法能够作为申报财产selected Tags 的存取者。

现在,我的问题是:

正确的内存管理语义是什么来申报该财产的?

我想到的代码模式是这样的(没有测试的代码, 请用打字机写下) :

@interface TagInfo : NSObject
@property (strong, nonatomic) NSString *tag;
@property (nonatomic) BOOL selected;
@end


@interface SomeClass : NSObject
@property (memorytype, nonatomic) NSArray *selectedTags;
@end

@implementation TagHolder

- (NSArray *)selectedTags
{
    // tagInfoArray is an array of all TagInfo objects
    NSPredicate *selPred = [NSPredicate predicateWithFormat: @"selected == YES"];
    NSArray *selectedTagInfoObjects = [[self tagInfoArray] filteredArrayUsingPredicate: selPred];

    NSArray *selectedTags = [selectedTagInfoObjects valueForKey: @"tag"];
    return selectedTags;
}

- (void)setSelectedTags: (NSArray *)selectedTags
{
    for (TagInfo *tagInfo in [self tagInfoArray]) {
        tagInfo.selected = [selectedTags containsObject: tagInfo.tag];
    }
}

@end

emorytype 应该是什么? 显然不是强或弱, 但我认为它可能是 指派 copy 或甚至 unsafe_unreserved 的任意一种, 但对于带有对象值的计算属性来说,哪种最正确?

我通常使用ARC, 但我猜这个问题在人工保留量的环境下是相同的。

最佳回答

memorytype 只有在以下情况下才有意义: your pression accessers. 由于您正在为获取者和设置者提供自己的执行工具, 忽略了 property 之后您放在括号中的东西; 我通常在此处放 仅读 readwrite , 只是为了提醒自己这些属性上可以使用哪种访问 。

您的代码是正确的, 它不会在 ARC 或没有 ARC 的情况下产生记忆问题 。

问题回答

暂无回答




相关问题
Windows Mobile 6 Emulator change storage?

How do i change the size of the Windows Mobile 6 Emulator. Its fixed at 32mb. I read this post: Increasing Windows Mobile 5 Emulator Storage But it only helps for the 5.0 version. Isnt there any way ...

CUDA Memory Allocation accessible for both host and device

I m trying to figure out a way to allocate a block of memory that is accessible by both the host (CPU) and device (GPU). Other than using cudaHostAlloc() function to allocate page-locked memory that ...

RAM memory reallocation - Windows and Linux

I am working on a project involving optimizing energy consumption within a system. Part of that project consists in allocating RAM memory based on locality, that is allocating memory segments for a ...

Should I send retain or autorelease before returning objects?

I thought I was doing the right thing here but I get several warnings from the Build and Analyze so now I m not so sure. My assumption is (a) that an object I get from a function (dateFromComponents: ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

doubts regarding Memory management in .net

I m learning about Memory management in C# from the book "Professional C#" The presence of the garbage collector means that you will usually not worry about objects that you no longer need; ...

Objective-C returning alloc d memory in a function == bad?

This is on the iPhone. So what if I have a function like - (SomeObject*)buildObject; Do I need to pass in a variable that I have already alloc d outside like - (void)assignObject(SomeObject** out);...

热门标签