NSURLConnection委托返回Null

时间:2015-06-05 13:06:51

标签: objective-c nsurlconnection

我在NSURLConnection上有一些示例,请参阅下面的最新内容,但我仍然在返回的FinishLoading上获取null。我检查了didReceiveResponse并获取了数据。我在这里做的不是什么?

编辑:现在按预期工作。

strings.Replace(f, "|". '\|', -1)

1 个答案:

答案 0 :(得分:1)

取出NSMutableData * theReceivedData。使它成为一个类变量。您的问题是theReceivedData在每种方法中都被释放。您正在每个方法中创建一个新对象。留下旧的。

NSMutableData *_theReceivedData;

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
            _theReceivedData = [NSMutableData new];
            [_theReceivedData setLength:0];
        }

        - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
            [_theReceivedData appendData:data];
        }

        - (NSCachedURLResponse *)connection:(NSURLConnection *)connection
                          willCacheResponse:(NSCachedURLResponse*)cachedResponse {
            // Return nil to indicate not necessary to store a cached response for this connection
            return nil;
        }

        // LOOK AT THIS http://stackoverflow.com/questions/1064920/iphone-corrupt-jpeg-data-for-image-received-over-http

        - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
            NSLog(@"%@",_theReceivedData);