Domanda

Ho un file di testo con 5 stringhe.Ho bisogno di usare NsurlConnection per ottenere un conteggio di questo file.Ma Nslog ha mostrami, che 'Dump' è vuoto.Come posso trasformare i dati da nsmutabilitata a Nsarray.Gli array sono perché ho bisogno di mostrare quei 5 elementi in un tavolo da tavolo.

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;
.

Grazie in anticipo.BTW URL funziona, puoi vedere il file.

È stato utile?

Soluzione

If you don't want to use a delegate, you can use a synchronous call with NSURLConnection, like this:

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;
}

Just beware that this will not be running asynchronously. If you don't want it to run on the main thread and block your main thread/UI, consider using a separate thread to execute that code or use GCD.

Altri suggerimenti

Here's how you implement this solution with a delegate:

In your .h file:

@interface MyClass : NSObject <NSURLConnectionDelegate, NSURLConnectionDataDelegate>

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

@end

In you .m file:

@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

You have to use the delegate, then save the received data into receivedData (which is of course empty right now.. you just initalized it.) and then you transform the data into a string, like you did it in your example. Have a look at NSURLConnectionDelegate

You need to implement the delegate methods for NSURLConnection to be notified of incoming data. You are using the asynchronous methods.

Also note that [NSMutableData data] just creates an empty data-object.. so you can't expect it to contain any data..

I suggest you read https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html#//apple_ref/doc/uid/20001836-BAJEAIEE (completely!)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top