【问题标题】:How to release a JSONConnection object如何释放 JSONConnection 对象
【发布时间】:2012-03-30 06:47:07
【问题描述】:

我正在使用JSONConnection 对象,如下所示。

- (void)startGettingSearchResultsByKeyword:(NSString *)keyword
                              delegate:(id <DataProviderDelegate>)delegate
{

    /* Prepare request */
    JSONConnection *jsonConnection = [[JSONConnection alloc] initWithURLString:NSLocalizedString(@"GET_SEARCH_RESULTS", nil)];

    [jsonConnection.requestValues setObject:APP_VERSION_VALUE forKey:APP_VERSION_KEY];
    [jsonConnection.requestValues setObject:keyword forKey:KEYWORD_KEY];

    /* Send asynchronnous URL request and process response */
    [jsonConnection sendAsynchronousRequestWithCompletionHandler:^(NSDictionary *responseValues, NSError *error) {

        NSArray *responseDataArray = [[responseValues objectForKey:JSON_RESPONSE_DICTIONARY_KEY] JSONValue];

        NSMutableArray *searchResults = [[NSMutableArray alloc] init];

        if ([responseDataArray count] > 0) {
            for (int index = 0; index < [responseDataArray count]; index++) {
            NSDictionary *result = [responseDataArray objectAtIndex:index];

                ProductDetails *searchResult = [[ProductDetails alloc] init];

                NSNumber *contentId = [result objectForKey:NSLocalizedString(@"CONTENT_ID", nil)];

                searchResult.contentId = [NSString stringWithFormat:@"%d", [contentId intValue]];            
                searchResult.productName = [result objectForKey:NSLocalizedString(@"NAME", nil)];

                [searchResults addObject:searchResult];
                [searchResult release];
            }
        }

        [delegate didFinishGettingSearchResults:searchResults];

        [searchResults release];

    }];
}

我不知道release 我的JSONConnection 对象的正确位置。

请帮忙。

【问题讨论】:

    标签: ios objective-c memory-management memory-leaks


    【解决方案1】:

    您是否尝试过在分配 JSONConnection 时自动释放它?

    JSONConnection *jsonConnection = [[[JSONConnection alloc] initWithURLString:NSLocalizedString(@"GET_SEARCH_RESULTS", nil)] autorelease];
    

    【讨论】:

    • 没有。我没有。所以,我添加了自动释放,它工作正常。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2013-06-08
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    • 2020-12-24
    • 1970-01-01
    • 1970-01-01
    • 2011-05-13
    相关资源
    最近更新 更多