dimanche 1 mars 2015

Unable to store a NSMutableArray(each instance of array contains a custom object) in NSUserDefaults

its a custom class



#import <Foundation/Foundation.h>

@interface timeTable : NSObject
@property (nonatomic) int ID;
@property (nonatomic) NSString * type;
@property (nonatomic) NSString * time;
@property (nonatomic) NSString * busno;
@property (nonatomic) NSString * stops;


// nothing is done in it's .m file not even synthesise

// thats an other class
#import <Foundation/Foundation.h>
#import "timeTable.h"

@interface refreshDatabase : NSObject

@property (strong, nonatomic) NSMutableArray * arrayTimeTable;
@property (strong, nonatomic) timeTable * objectTimeTable;

// in it's .m file i am downloading a JSON formatted array using a
service then i am saving it to NsMutbaleArray

// downloading a json array which contains a rows of data

NSError * error;
NSArray * jsonArray = [NSJSONSerialization JSONObjectWithData:

[safeString dataUsingEncoding:NSUTF8StringEncoding]

options:NSJSONReadingAllowFragments error:&error];

NSLog(@"json Array %@", jsonArray);

// for getting an instance of array

NSDictionary * jsonElement;

for (int i=0; i<jsonArray.count ; i++)
{ // each row will be saved in an object of timetable class then that
// object will be saved to nsmutablearray

jsonElement = [jsonArray objectAtIndex:i];
objectTimeTable = [[timeTable alloc]init];

objectTimeTable.ID = [[jsonElement objectForKey:@"id"]intValue];

objectTimeTable.type = [jsonElement objectForKey:@"type"];
objectTimeTable.time = [jsonElement objectForKey:@"time"];
objectTimeTable.busno = [jsonElement objectForKey:@"busno"];
objectTimeTable.stops = [jsonElement objectForKey:@"stops"];



// adding an instance from JSON Array to our NSmutablearray
[arrayTimeTable addObject:objectTimeTable];

}//end of json Array FOR loop

// our array containnig all the objects will be saved using
//NSUserDefualts

// userDefaults is an object of NSUserDefaults

if(userDefaults)
{ // its not saving it to userdefaults
[userDefaults setObject:arrayToStore forKey:@"ArrayOfTimeTables"];
[userDefaults synchronize];

}
// retrieving the saved array from NSUSerDefaults and printing it
// using slog

timeTable *objPrint = [[timeTable alloc]init];

NSMutableArray *arrayLoader = [userDefaults arrayForKey:@"ArrayOfTimeTables"];

for (int i=0; i<arrayLoader.count ; i++)
{
objPrint = [arrayLoader objectAtIndex:i];
NSLog(@"outSide Printing For LOOP After Loading of tim # %d times havind id =%d type = %@ time = %@ busno = %@ stops = %@",i,objPrint.ID,objPrint.type,objPrint.time,objPrint.busno,objPrint.stops);
}


thanx a lot in helping me in advance please tell me how to save that array which which contains object of timetable class into nsUseDefaults and then how to load it back


please please help me i read a lot of similar question and answers but don't know how to make them work for me




Aucun commentaire:

Enregistrer un commentaire