* * First Way: Object Single * *//Archive-(void) nskeyedarchiver{Nsarray*array = [Nsarrayarraywithobjects:@"L",@"J",@"F",Nil];NSString*filepath = [Nshomedirectory () stringbyappendingpathcomponent:@"Array. LJF "];NSLog(@"%@", FilePath);BOOLSuccess = [Nskeyedarchiver Archiverootobject:array Tofile:filepath];if(success) {NSLog(@"saved successfully"); }}//Solve files-(void) nskeyedunarchiver{NSString*filepath = [Nshomedirectory () stringbyappendingpathcomponent:@"Array. LJF "];IDArray = [Nskeyedunarchiver Unarchiveobjectwithfile:filepath];NSLog(@"%@", array);}
* * Second Way: Object Multiple * *//Archive 2-(void) nskeyedarchivertwo{Nsarray*array = [Nsarrayarraywithobjects:@"L",@"J",@"F",Nil];nsdictionary*dic = @{@"Name": array}; Nsmutabledata *data = [Nsmutabledata data]; Nskeyedarchiver *archiver = [[Nskeyedarchiver alloc] initforwritingwithmutabledata:data];//Coding[Archiver encodeobject:dic forkey:@"DIC"]; [Archiver Encodeobject:array forkey:@"Array"]; [Archiver Encodeint: -forkey:@"num"]; [Archiver encodeobject:@"LJF"forkey:@"Name"];//Complete the encoding to populate the above archived data into data, where the archived objects are already stored[Archiver finishencoding];NSString*filepath = [Nshomedirectory () stringbyappendingpathcomponent:@"Array. LJF "];BOOLSuccess = [Data Writetofile:filepath atomically:YES];if(success) {NSLog(@"Archive Success"); }}//Solution 2-(void) nskeyedunarchivertwo{NSString*filepath = [Nshomedirectory () stringbyappendingpathcomponent:@"Array. LJF "];//Read archived dataNSData *data = [[NSData alloc] initwithcontentsoffile:filepath];//Create an Archive object to unpack dataNskeyedunarchiver *unarchiver = [[Nskeyedunarchiver alloc] initforreadingwithdata:data];//Solution archive Nsarray*array = [Unarchiver decodeobjectforkey:@"Array"];NSLog(@"%@", array);nsdictionary*dic = [Unarchiver decodeobjectforkey:@"DIC"];NSLog(@"%@", DIC);NSString*name = [Unarchiver decodeobjectforkey:@"Name"];NSLog(@"%@", name);intValue = [Unarchiver decodeintegerforkey:@"num"];NSLog(@"%d", value);}
Archive files (file write and read)