如何从NSMutableData获取数组

时间:2012-03-08 13:28:17

标签: objective-c ios nsarray nsmutabledata

我有5个字符串的文本文件。我需要使用NSURLConnection来获取此文件的内容。但NSLog告诉我,“转储”是空的。如何将数据从NSMutableData转换为NSArray。数组是因为我需要在TableView中显示这5个项目。

NSURLRequest *theRequest=[NSURLRequest
                         requestWithURL:[NSURL URLWithString:@"http://dl.dropbox.com/u/25105800/names.txt"]
                         cachePolicy:NSURLRequestUseProtocolCachePolicy
                         timeoutInterval:60.0];

NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
    receivedData = [NSMutableData data];
    NSString *dump = [[NSString alloc] initWithData:receivedData
                         encoding:NSUTF8StringEncoding];
    NSLog(@"data: %@", dump);
    NSArray *outputArray=[dump componentsSeparatedByString:@"\n"];
    self.namesArray = outputArray;

提前致谢。 BTW URL有效,你可以看到该文件。

4 个答案:

答案 0 :(得分:2)

以下是如何使用委托实现此解决方案:

在你的.h文件中:

@interface MyClass : NSObject <NSURLConnectionDelegate, NSURLConnectionDataDelegate>

@property (nonatomic, retain) NSMutableData *receivedData;
@property (nonatomic, retain) NSArray *namesArray;

@end

在.m文件中:

@implementation MyClass

@synthesize receivedData = _receivedData;
@synthesize namesArray = _namesArray;

- (id)init {
    self = [super init];
    if (self) {
        self.receivedData = [NSMutableData data];
        NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://dl.dropbox.com/u/25105800/names.txt"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
        [connection start];

    }
    return self;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"Received response! %@", response);
}

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

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSString *dump = [[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding];
    NSLog(@"data: %@", dump);
    self.namesArray = [dump componentsSeparatedByString:@"\n"];
}

@end

答案 1 :(得分:1)

如果您不想使用委托,可以使用NSURLConnection同步调用,如下所示:

NSURLRequest *theRequest=[NSURLRequest
                     requestWithURL:[NSURL URLWithString:@"http://dl.dropbox.com/u/25105800/names.txt"]
                     cachePolicy:NSURLRequestUseProtocolCachePolicy
                     timeoutInterval:60.0];

NSError *error = nil;
NSHTTPURLResponse *response = nil;
NSData *receivedData = [NSURLConnection sendSynchronousRequest:theRequest response:&response error:&error];

if (error == nil) {
    NSString *dump = [[NSString alloc] initWithData:receivedData
                     encoding:NSUTF8StringEncoding];
    NSLog(@"data: %@", dump);
    NSArray *outputArray=[dump componentsSeparatedByString:@"\n"];
    self.namesArray = outputArray;
}

请注意,这不会异步运行。如果您不希望它在主线程上运行并阻止您的主线程/ UI,请考虑使用单独的线程来执行该代码或使用GCD。

答案 2 :(得分:0)

你必须使用委托,然后将接收到的数据保存到receivedData中(现在当然是空的......你只是把它搞砸了。)然后你将数据转换成字符串,就像你在你的例子中所做的那样。看看NSURLConnectionDelegate

答案 3 :(得分:0)

您需要为NSURLConnection实现委托方法,以便收到传入数据的通知。您正在使用异步方法。

另请注意[NSMutableData data]只创建一个空的数据对象..所以你不能指望它包含任何数据..

我建议你阅读https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html#//apple_ref/doc/uid/20001836-BAJEAIEE (完全!)