2012-03-08 47 views
1

我有5個字符串的文本文件。我需要使用NSURLConnection來獲取這個文件的大小。但NSLog告訴我,'轉儲'是空的。如何將數據從NSMutableData轉換爲NSArray。數組是因爲我需要在TableView中顯示這5個項目。如何從NSMutableData獲取數組

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的作品,你可以看到該文件。

+0

:在您.m文件

@interface MyClass : NSObject <NSURLConnectionDelegate, NSURLConnectionDataDelegate> @property (nonatomic, retain) NSMutableData *receivedData; @property (nonatomic, retain) NSArray *namesArray; @end 

在您的.h文件中當你用分配一個空對象的構造函數創建你'receivedData'時,它是非空的嗎? – dasblinkenlight 2012-03-08 13:33:45

回答

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。

+0

謝謝,它適用於我。但是如何與代表一起觀察呢? – 2012-03-08 14:27:19

+0

這是一個更爲複雜的解決方案。讓我看看我能否找到一個例子。 – 2012-03-08 14:49:59

+1

我添加了第二個答案,以使用委託。 – 2012-03-08 15:00:01

0

你必須使用委託,然後保存接收到的數據receivedData(這當然是空的現在..你只是initalized它),然後將數據轉換成字符串,像你這樣做是你的例。看看NSURLConnectionDelegate

2

這裏是你如何實現這個解決方案可以與委託:你爲什麼想到`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