【问题标题】:How to get an array from NSMutableData如何从 NSMutableData 获取数组
【发布时间】:2012-03-08 13:28:17
【问题描述】:

我有 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 有效,你可以看到文件。

【问题讨论】:

  • 当您使用分配空对象的构造函数创建 youc receivedData 时,为什么您希望 dump 不为空?

标签: objective-c ios nsarray nsmutabledata


【解决方案1】:

以下是使用委托实施此解决方案的方法:

在您的 .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

【讨论】:

    【解决方案2】:

    如果你不想使用委托,你可以使用 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。

    【讨论】:

    • 谢谢,它对我有用。但是它必须如何与代表一起看?
    • 这是一个更复杂的解决方案。让我看看能不能找到一个例子。
    • 我添加了第二个答案,使用委托。
    【解决方案3】:

    您必须使用委托,然后将接收到的数据保存到 receivedData (现在当然是空的..您刚刚初始化它。)然后您将数据转换为字符串,就像您在示例中所做的那样.看看NSURLConnectionDelegate

    【讨论】:

      【解决方案4】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-18
        • 1970-01-01
        • 2011-11-24
        • 1970-01-01
        相关资源
        最近更新 更多