Write a complex array of custom structs to file Objective C -
i need save , load contents of array of structs, know objective c particular data types can read/write with.
here struct:
struct scourse { nsmutablearray* holes; // holds integers (pars) nsstring* name; int size; bool inuse; }; @interface coursesmanager : nsobject { struct scourse courses[5]; }
what data types i'll need use? each have different methods needed in order read/write? i'm looking non-complex way data need , file. quite in language i'm more familiar (c++), of particulars of objective-c still lost on me.
edit: solution (thanks help, everyone)
-(void)applicationwillresignactive:(uiapplication *)application { // save courses nsmutablearray* totalwritearray = [[nsmutablearray alloc] initwithcapacity:max_courses]; (int = 0; < max_courses; ++i) { struct scourse savecourse = [coursesmanager getcourseatindex:i]; nsnumber* ninuse = [nsnumber numberwithbool:savecourse.inuse]; nsnumber* nsize = [nsnumber numberwithint:savecourse.size]; nsmutablearray* writearray = [[nsmutablearray alloc] initwithcapacity:4]; [writearray addobject:ninuse]; [writearray addobject:nsize]; [writearray addobject:savecourse.name]; [writearray addobject:savecourse.holes]; [totalwritearray addobject:writearray]; } [totalwritearray writetofile:[self savefilepath] atomically:yes]; }
and loading in...
-(void)loadfile { nsstring *mypath = [self savefilepath]; bool fileexists = [[nsfilemanager defaultmanager] fileexistsatpath:mypath]; if (fileexists) { nsmutablearray* totalreadarray = [[nsmutablearray alloc] initwithcontentsoffile:[self savefilepath]]; (int = 0; < max_courses; ++i) { struct scourse loadcourse = [coursesmanager getcourseatindex:i]; nsmutablearray* loadarray = [totalreadarray objectatindex:i]; nsnumber* ninuse = [loadarray objectatindex:0]; loadcourse.inuse = [ninuse boolvalue]; nsnumber* nsize = [loadarray objectatindex:1]; loadcourse.size = [nsize integervalue]; nsstring* inname = [loadarray objectatindex:2]; loadcourse.name = inname; nsmutablearray* inholes = [loadarray objectatindex:3]; loadcourse.holes = inholes; [coursesmanager replacecourseatindex:i with:loadcourse]; } } }
first thing first. shouldn't use plain old c structures. arc memory management not appreciate.
if familiar c++, should maybe use c++ class instead, please compiler , runtime. depends on want do.
array. use either nsarray or std::vector please, no plain c arrays. not sure how arc handle suppose not appreciate much. objective-c , c++ both provides tools need handle collections of whatever.
serialization. have several possibilities, 1 of them nscoder.
last word, called modern syntax, converting things objc objects quite easy.
bool b = yes; int = 10; double d = 3.14; char* s = "pouf pouf";
you objc equivalents boxin' thingy:
nsnumber* bo = @( b ); nsnumber* io = @( ); nsnumber* = @( d ); nsstring* = @( s ); nsarray* ao = @[ @( ), ]; nsdictionary* = @{ @"num" : io, @"str" : @( s ) };
to write in file, in 1 gracious step:
[@{ @"bool" : bo, @"array" : @[ @"string", @10, @( 10 + 20 ) ] } writetofile: @"path.plist" atomically: yes];
but question remains, trying accomplish?
Comments
Post a Comment