Objective c 如何从NSMutableData获取数组
我有5个字符串的文本文件。我需要使用NSURLConnection来获取此文件的内容。但NSLog告诉我,“转储”是空的。如何将数据从NSMutableData转换到NSArray。数组是因为我需要在TableView中显示这5项Objective c 如何从NSMutableData获取数组,objective-c,ios,nsarray,nsmutabledata,Objective C,Ios,Nsarray,Nsmutabledata,我有5个字符串的文本文件。我需要使用NSURLConnection来获取此文件的内容。但NSLog告诉我,“转储”是空的。如何将数据从NSMutableData转换到NSArray。数组是因为我需要在TableView中显示这5项 NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://dl.dropbox.com/u/25105800
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;
提前谢谢。顺便说一句,URL起作用,您可以查看文件。您必须使用委托,然后将接收到的数据保存到receivedData(现在当然是空的..您只是初始化了它。)然后将数据转换为字符串,就像您在示例中所做的那样。查看一下
nsurlconnectionelegate
您需要为NSURLConnection实现委托方法,以便收到传入数据的通知。您正在使用异步方法
还要注意,[NSMutableData]
只创建了一个空数据对象。。所以你不能期望它包含任何数据
我建议你读一读
(完全!)如果您不想使用委托,可以使用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。 < P>这里您是如何用委托实现这个解决方案: 在.h文件中:
@interface MyClass : NSObject <NSURLConnectionDelegate, NSURLConnectionDataDelegate>
@property (nonatomic, retain) NSMutableData *receivedData;
@property (nonatomic, retain) NSArray *namesArray;
@end
当您使用分配空对象的构造函数创建c
receivedData
时,为什么希望dump
为非空?谢谢,它对我很有用。但它在委派时必须是什么样子呢?这是一个更复杂的解决方案。让我看看是否能找到一个例子。我添加了第二个答案,使用委托。
@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