objective c - How to get an array from NSMutableData -
i have text file 5 strings. need use nsurlconnection contnent of file. nslog shows me, 'dump' empty. how can transform data nsmutabledata nsarray. arrays because need show 5 items in tableview.
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;
thanks in advance. btw url works, can see file.
if don't want use delegate, can use synchronous call nsurlconnection, 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 not running asynchronously. if don't want run on main thread , block main thread/ui, consider using separate thread execute code or use gcd.
Comments
Post a Comment