objective c - Writing NSArray of NSDictionary to file as XML -


i bit stuck on objective c function , looking help!

this osx app pulls in data twitter array, inserts them core data.

i have load of objects in core data want write file xml, have fetch request filters , returns array of library objects write disk xml.

i have been looking @ various libraries , tutorials online , seem orientated around reading rather writing, , fact it's array of libraries seems complicate matters.

so far have tried this, never seems create file, annoyingly doesn't give me kind of error either:

-(void)writetweetstoxml{ //create new fetch request nsfetchrequest *request = [[nsfetchrequest alloc] init];  //set new predicate fetch tweets have been favourited nspredicate *filterfavourite = [nspredicate predicatewithformat:@"approved == yes"];  //setup request [request setentity:[nsentitydescription entityforname:@"tweet"    inmanagedobjectcontext:_managedobjectcontext]];  //assign predicate fetch request [request setpredicate:filterfavourite]; nserror *error = nil;  //create array returned objects nsarray *tweetstoexport = [_managedobjectcontext executefetchrequest:request error:&error]; nsassert2(tweetstoexport != nil && error == nil, @"error fetching events: %@\n%@", [error localizeddescription], [error userinfo]);  //for (id obj in tweetstoexport)   //nslog(@"obj: %@", obj);   nsstring *documents = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0]; nsstring *path = [nsstring stringwithformat:@"%@/tweets.xml", documents]; nsstring *writeerror;  if(tweetstoexport) {     [tweetstoexport writetofile:path atomically:yes];     nslog(@"%@ written %@", tweetstoexport, path); } else {     nslog(@"%@",writeerror);  }   } 

and structure of managed objects:

@property (nonatomic, retain) nsstring * text; @property (nonatomic, retain) nsstring * username; @property (nonatomic, retain) nsstring * realname; @property (nonatomic, retain) nsstring * avatarurl; @property (nonatomic, retain) nsstring * tweetid; @property (nonatomic, retain) nsnumber * approved; @property (nonatomic, retain) nsdate * postdate; @property (nonatomic, retain) nsdecimalnumber * longitude; @property (nonatomic, retain) nsdecimalnumber * latitude; @property (nonatomic, retain) nsnumber * profanity; @property (nonatomic, retain) nsstring * otherusertweet; @property (nonatomic, retain) nsstring * favouritetweet; 

a pointer @ useful info or libraries incredibly helpful!

thanks

gareth

edit 1 ok, have tried re-encoding array data, , modified code bit report success/fail on write, without encoding getting failed write, output garbage.

code follows:

    nsdictionary* environ = [[nsprocessinfo processinfo] environment]; bool insandbox = (nil != [environ objectforkey:@"app_sandbox_container_id"]); nslog(@"app sandbox status = %d", insandbox);  nsstring *documents = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0]; nsstring *path = [nsstring stringwithformat:@"%@/tweets.xml", documents]; nsstring *writeerror; nsdata *data = [nskeyedarchiver archiveddatawithrootobject:tweetstoexport]; if(tweetstoexport) {     bool successful = [data writetofile:path atomically:yes];     nslog(@"success = %d", successful); } else {     nslog(@"%@",writeerror);  } 

however produces garbage output:

bplist00‘úùx$versionx$objectsy$archivert$top܆Ø$*+,-./04:fmdrsxfghijknsxÜáàâäãéìòu$null“     zns.objectsv$class£ ÄÄÄÄ#›       !"#$%&'(^otherusertweetwtweetidxrealnamexapprovedylongitudexpostdatexlatitudeyavatarurlyprofan    ityxusernamettext^favouritetweetÄÄÄÄÄÄ  Ä ÄÄÄÄÄÄ_wps myself alongside mighty @cjbeatz co-hosting , dj huge gig      schools @ wembley arena wed.\therealnihalwnihal         a_qhttp://a0.twimg.com/profile_images/1021089851/bb213320_radio_1_-   _nihal_normal.jpg_353964930917085186“1  23wns.time#a∑â˛¶Ä “5678z$classnamex$classesvnsdate¢79xnsobject◊ ;<=>?    @abcd/dc[ns.mantissa[ns.negative[ns.exponent^ns.mantissa.boyns.lengthzns.compactÄo“56gh_nsdeci    malnumberplaceholder•ijkl9_nsdecimalnumberplaceholder_nsdecimalnumberxnsnumberwnsvalue◊ ;<=>?@aocd/dcÄorno“56tuutweet£vw9utweet_nsmanagedobject› z[ ]^_`%b'dÄÄÄÄÄÄÄÄÄÄÄÄÄ_"bird. bath. http://t.co/1plf2gki4t[derrenbrown\derren     brown_zhttp://a0.twimg.com/profile_images/3682019430/d4636e8301b238c6a10c55efacd2442c_normal.j     peg_353965570888192000“1 l3#a∑âˇ@Ä ◊ ;<=>?@apcd/dcÄo◊ ;<=>?@aucd/dcÄo› z{ }~Ä%Ç'ÑÄÄÄÄÄ!Ä Ä"ÄÄÄÄÄÄ_âthe surgery myself , new resident doctor @drradhamodgil     starts @ 9pm on @bbcr1 it's summer special - 03700100100, text on 81199sahj_aled haydn     jones_mhttp://a0.twimg.com/profile_images/2254837339/aled-012_-        _version_3_normal.jpg_353965267564507138“1 å3#a∑â˛ˆÄ ◊ ;<=>?@aêcd/dcÄo◊ ;<=>?@aïcd/dcÄo“56ôö^nsmutablearray£ôõ9wnsarray_nskeyedarchiver—ûütrootÄ#-27^dit{ÅÉÖᢠ   ±π¬À’fiÁÒ˚  "$&(*,.02¨π¡*,19bdit]dgpãó£≤º«…‹›fl‡Â%7@hwylmnqv|ÄÜò≥µ∑πªΩø¡√≈«…ÀÕÚ˛h}Çãçúû    ±≤≥¬ƒ◊ÿŸÙˆ¯˙¸˛ öû±$&57jkl[]pqrwÜäí§ß¨†Æ 

edit 2

ok, so, if open file in suitable viewer (bbedit) formatted ok, it's not useful, want able encode objects there values xml, appreciated.

your first attempt failed because writetofile:atomically: works property list types-- nsstring, nsnumber, nsdata, nsdate, nsarray, , nsdictionary. nsmanagedobject not on list, nor subclasses of might create. you're not attempting write array of dictionaries-- you're trying write array of managed objects.

converting nsdata , writing worked, you're getting binary property list. it's not garbage, it's expected format code, it's not designed human readable.

if took original code added before performing fetch:

[request setresulttype:nsdictionaryresulttype]; 

...you'd writetofile:atomically:.


Comments