ios - Parsing RSS in Obj-C? -
i'm trying parse rss date view in app, following this tutorial. have json file retrieve information , display on screen.
here's code
// // viewcontroller.m // jsonyt // // created yo_291 on 3/16/15. // copyright (c) 2015 yo_291. rights reserved. // #import "viewcontroller.h" @interface viewcontroller () { nsmutabledata *webdata; nsurlconnection *connection; nsmutablearray *array; } @end @implementation viewcontroller - (void)viewdidload { [[self myview]setdelegate:self]; [[self myview]setdatasource:self]; array = [[nsmutablearray alloc]init]; [super viewdidload]; // additional setup after loading view, typically nib. } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } -(void)connection:(nsurlconnection *)connection didreceiveresponse:(nsurlresponse *)response { [webdata setlength:0]; } -(void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data { [webdata appenddata:data]; } -(void)connection:(nsurlconnection *)connection didfailwitherror:(nserror *)error { nslog(@"fail error"); } -(void)connectiondidfinishloading:(nsurlconnection *)connection { nsdictionary *alldatadictionary = [nsjsonserialization jsonobjectwithdata:webdata options:0 error:nil]; nsdictionary *rd = [alldatadictionary objectforkey:@"responsedata"]; nsdictionary *feed = [rd objectforkey:@"feed"]; nsarray *aoe = [feed objectforkey:@"entries"]; for(nsdictionary *diction in aoe) { nsdictionary *numb = [diction objectforkey:@"0"]; nsstring *title = [numb objectforkey:@"title"]; [array addobject:title]; } [[self myview] reloaddata]; } - (ibaction)getfeed:(id)sender { nsurl *url = [nsurl urlwithstring:@"http://ajax.googleapis.com/ajax/services/feed/load?v=2.0&q=http://www.britannica.com/feeds/tdih.rss&num="]; nsurlrequest *request = [nsurlrequest requestwithurl:url]; connection = [nsurlconnection connectionwithrequest:request delegate:self]; if(connection) { webdata = [[nsmutabledata alloc]init]; } } -(nsinteger)numberofsectionsintableview:(uitableview *)tableview { return 1; } -(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return [array count]; } -(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *celli = @"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:celli]; if(!cell) { cell = [[uitableviewcell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:celli]; } cell.textlabel.text = [array objectatindex:indexpath.row]; return cell; } @end
the application crashes on line [array addobject:title];
.
error stack trace:
2015-03-16 22:32:51.650 jsonyt[8195:547408] *** terminating app due uncaught exception 'nsinvalidargumentexception', reason: '*** -[__nsarraym insertobject:atindex:]: object cannot nil' *** first throw call stack: ( 0 corefoundation 0x000000010ba28a75 __exceptionpreprocess + 165 1 libobjc.a.dylib 0x000000010b6c1bb7 objc_exception_throw + 45 2 corefoundation 0x000000010b8f70ca -[__nsarraym insertobject:atindex:] + 954 3 jsonyt 0x000000010b18da5f -[viewcontroller connectiondidfinishloading:] + 735 4 cfnetwork 0x000000010d85998c __65-[nsurlconnectioninternal _withconnectionanddelegate:onlyactive:]_block_invoke + 69 5 cfnetwork 0x000000010d859930 -[nsurlconnectioninternal _withconnectionanddelegate:onlyactive:] + 199 6 cfnetwork 0x000000010d859a97 -[nsurlconnectioninternal _withactiveconnectionanddelegate:] + 48 7 cfnetwork 0x000000010d729937 ___zn27urlconnectionclient_classic26_delegate_didfinishloadingeu13block_pointerfvve_block_invoke + 107 8 cfnetwork 0x000000010d7f6a31 ___zn27urlconnectionclient_classic18_withdelegateasyncepkcu13block_pointerfvp16_cfurlconnectionpk33cfurlconnectionclientcurrent_vmaxe_block_invoke_2 + 273 9 cfnetwork 0x000000010d714d16 _zn19runloopblockcontext13_invoke_blockepkvpv + 72 10 corefoundation 0x000000010b930b44 cfarrayapplyfunction + 68 11 cfnetwork 0x000000010d714bd7 _zn19runloopblockcontext7performev + 133 12 cfnetwork 0x000000010d714a16 _zn17multiplexersource7performev + 256 13 cfnetwork 0x000000010d71482c _zn17multiplexersource8_performepv + 72 14 corefoundation 0x000000010b95dc91 __cfrunloop_is_calling_out_to_a_source0_perform_function__ + 17 15 corefoundation 0x000000010b953b5d __cfrunloopdosources0 + 269 16 corefoundation 0x000000010b953194 __cfrunlooprun + 868 17 corefoundation 0x000000010b952bc6 cfrunlooprunspecific + 470 18 graphicsservices 0x000000010f005a58 gseventrunmodal + 161 19 uikit 0x000000010be18580 uiapplicationmain + 1282 20 jsonyt 0x000000010b18e483 main + 115 21 libdyld.dylib 0x000000010dfd6145 start + 1 22 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating uncaught exception of type nsexception
but have no idea how go fixing it. eyes i'm searching json file correctly may wrong. json file rss feed i've used google tool "turn" json database, i'm unsure if complications there.
let me know any/all information needed, first post don't know if i'm delivering enough context.
the error indicates object inserted nil
. problem lies in following lines:
nsdictionary *numb = [diction objectforkey:@"0"]; nsstring *title = [numb objectforkey:@"title"]; [array addobject:title];
you never check whether title
nil
or not before adding object array. can use nslog()
debug values , check nil
values using techniques similar follow:
if([numb objectforkey:@"title"] != nil) { [array addobject:[numb objectforkey:@"title"]]; }
p.s. assigning value title
useless, title
used once (wasting memory).
also, using libraries afnetworking
make codes simpler, , provides error handling.
Comments
Post a Comment