本文介绍了计算数组属性的内存语义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 这是一个允许用户标记内容的应用。标签只是字符串。 TagHolder 对象的数组包含应用程序中使用的所有标签的列表, 外部接口调用两个方法, selectedTags 和 setSelectedTags:,它返回并接受字符串数组。 现在,我的问题是: $ $ b 为该属性声明的正确的内存管理语义是什么? 我想到的代码模式是this代码未测试,所以请忍受打字错误): @interface TagInfo:NSObject @property )NSString * tag; @property(nonatomic)BOOL selected; @end @interface SomeClass:NSObject @property(memorytype,nonatomic)NSArray * selectedTags; @end @implementation TagHolder - (NSArray *)selectedTags { // tagInfoArray是所有TagInfo对象的数组 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 应该 memorytype be?显然不强或弱,但我认为它可以是 assign , copy 或甚至 unsafe_unretained ,但哪一个对于具有对象值的计算属性最正确? 我通常使用ARC,在具有手动保留计数的环境中,问题是相同的。解决方案 memorytype 仅当您 @synthesize 您的属性访问器时才有意义。由于你为getter和setter提供了自己的实现,所以在 @property 之后放在括号中的东西将被忽略;我通常把 readonly 或 readwrite 在那里,只是为了提醒自己这些属性可用的访问类型。 / p> 您的代码是正确的,它将工作,不会创建或不使用ARC的内存问题。 This is for an app that allows users to tag things. Tags are just strings.An array of TagHolder objects holds a list of all tags in use in the app, with a boolean telling if the tag is selected, but this is an implementation detail.The external interface calls for two methods, selectedTags, and setSelectedTags: which return and accept an arrays of strings.I would like these two methods to work as accessors for a declared property selectedTags.Now, my question is:What would be the correct memory management semantics to declare for that property?The code pattern that I have in mind is this (code not tested, so please bear with typos):@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]; }}@endWhat should memorytype be? Obviously not strong or weak, but I think it could be any one of assign, copy or even unsafe_unretained, but which one is the most correct for a computed property with an object value?I normally use ARC, but I guess the question is the same in an environment with manual retain count. 解决方案 memorytype is significant only when you @synthesize your property accessors. Since you are providing your own implementation for both the getter and the setter, the things you put in parentheses after @property are ignored; I usually put readonly or readwrite there, just to remind myself of what kind of access is available on these properties.Your code is correct, it will work without creating memory issues with or without ARC. 这篇关于计算数组属性的内存语义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-19 13:28