Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
复制后缺少属性(iOS)_Ios_Properties_Automatic Ref Counting - Fatal编程技术网

复制后缺少属性(iOS)

复制后缺少属性(iOS),ios,properties,automatic-ref-counting,Ios,Properties,Automatic Ref Counting,我的项目启用了ARC,我有两个类似的模型: 类别: 产品: 为了避免引用循环,并在ios4.x上部署,我将Child的父属性设置为assign,但不是weak 从JSON数据中,我可以得到一个包含我的类别和产品的树。当我用这棵树来列举时,一切都很完美。我有一个递归方法来搜索名称与关键字匹配的产品。方法如下: - (NSArray *)productsWithKeyword:(NSString *)keyword { NSMutableArray *filteredProducts = [

我的项目启用了ARC,我有两个类似的模型:

类别:

产品:

为了避免引用循环,并在ios4.x上部署,我将Child的父属性设置为assign,但不是weak

从JSON数据中,我可以得到一个包含我的类别和产品的树。当我用这棵树来列举时,一切都很完美。我有一个递归方法来搜索名称与关键字匹配的产品。方法如下:

- (NSArray *)productsWithKeyword:(NSString *)keyword
{
    NSMutableArray *filteredProducts = [NSMutableArray array];
    for (Category *cat in self.children) {
        NSArray *subResult = [cat productsWithKeyword:keyword];
        [filteredProducts addObjectsFromArray:subResult];
    }

    return [filteredProducts copy];
}
问题是当我使用返回的数组时,我可以得到正确的category属性。但从第二次开始,category属性从调试器中捕获为0


那么,有什么问题?问题的解决方法是什么?

为了避免引用循环,您希望使一个对象对另一个对象具有弱引用,而不是使两个对象彼此具有弱引用。我认为在这种情况下,产品不拥有类别,类别也不拥有产品,所以ARC会释放这两个对象,假设没有人声称它们


修复:使其中一个对象成为另一个对象的父对象

我还尝试将父属性和类别属性设置为强属性。但是问题仍然存在。天哪,这是我自己的愚蠢问题。根树数组保留了它们。
@interface Child : NSObject
@property (strong, nonatomic) NSString *name;
@property (assign, nonatomic) Category *category;
@end
- (NSArray *)productsWithKeyword:(NSString *)keyword
{
    NSMutableArray *filteredProducts = [NSMutableArray array];
    for (Category *cat in self.children) {
        NSArray *subResult = [cat productsWithKeyword:keyword];
        [filteredProducts addObjectsFromArray:subResult];
    }

    return [filteredProducts copy];
}