Objective c 从实现中删除retain会使应用程序崩溃,即使属性设置为retain

Objective c 从实现中删除retain会使应用程序崩溃,即使属性设置为retain,objective-c,Objective C,这个问题是从一个问题引出的。然而,stackoverflow让我无法发表评论,就像它有时会阻止我对自己的问题做出正确的回答一样 不管怎么说,我的申请中出现了一些奇怪的事情。我有一个财产: @property (nonatomic, retain) NSMutableArray *hotelList; //I also synthesize it - (void)populateHotelList { SearchWebServiceController *searchWS = [[Se

这个问题是从一个问题引出的。然而,stackoverflow让我无法发表评论,就像它有时会阻止我对自己的问题做出正确的回答一样

不管怎么说,我的申请中出现了一些奇怪的事情。我有一个财产:

@property (nonatomic, retain) NSMutableArray *hotelList; //I also synthesize it
- (void)populateHotelList
{
    SearchWebServiceController *searchWS = [[SearchWebServiceController alloc]init]; 

    //If I remove this retain the App crashes
    hotelList = [[searchWS getHotelsByRegionCode:@"12345" AndByKid:@"12345"] retain]; 

    [searchWS release];
}
以下是我设置属性的方式:

@property (nonatomic, retain) NSMutableArray *hotelList; //I also synthesize it
- (void)populateHotelList
{
    SearchWebServiceController *searchWS = [[SearchWebServiceController alloc]init]; 

    //If I remove this retain the App crashes
    hotelList = [[searchWS getHotelsByRegionCode:@"12345" AndByKid:@"12345"] retain]; 

    [searchWS release];
}
但是,如果删除“保留”,我的应用程序将崩溃。但根据苹果的文档,我不需要保留它

这是该方法的实现:

- (NSMutableArray *)getHotelsByRegionCode:(NSString *)regionCode 
                                 AndByKid:(NSString *)Kid
{
    NSMutableArray *result = [[NSMutableArray alloc]init];

    ...

    return [result autorelease];
}

谁能帮帮我吗

您必须保留它,因为您的方法返回的autorelease对象将在下一个事件循环中被销毁。

添加了自限定符,它起作用了

self.hotelList=[searchWS-getHotelsByRegionCode:@“12345”和bykid:@“12345”]


感谢spbfox

但它有两个不同的类别。那么被调用的类正在自动删除它,而调用的类正在保留它?您能解释一下您是否有机会吗?另外,属性是has a retain attribute这些是不同的类是可以的,因为它们使用的对象是相同的,因此它们将影响相同的retain计数器。然而,事实上,该财产是一个保留一使我张贴不正确。这也意味着问题不在您发布的代码中。你能告诉代码在没有retain的情况下失败的确切位置吗?我不知道这有什么帮助:2010-08-26 11:34:55.556 Renovatio[13062:207]***-[CFXPreferenceSearchListSource count]:发送到实例0x6046960 2010-08-26 11:34:55.556 Renovatio[13062:207]的无法识别的选择器***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'***-[CFXPreferenceSearchListSource count]:发送到实例0x6046960的无法识别的选择器'2010-08-26 11:34:55.557 Renovatio[13062:207]堆栈:(是的,这是保留问题(请参阅此处的类似帖子: