-1

It's 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 contains object of timetable class into nsUseDefaults and then how to load it back.

Please help me. I read a lot of similar question and answers, but don't know how to make them work for me.

ufo20
  • 33
  • 1
  • 5
Raim
  • 1
  • 2
  • Your custom object has to conform to the `NSCodingProtocol` check out this answer: [question](http://stackoverflow.com/questions/2315948/how-to-store-custom-objects-in-nsuserdefaults) – croX Mar 01 '15 at 11:33
  • well that answer is good if i am only storing the timetable class object into NSUserDEFAULTS but i am storing timetable objects first into NSMutableArray then saving that Array into NSUserDefaults btw thanx for your time – Raim Mar 01 '15 at 12:20
  • Your custom object has to conform to the NSCodingProtocol. – Hot Licks Mar 01 '15 at 13:57

2 Answers2

0

Use NScoding to encode each of your custom object then add that custom object into an array then encode other and then add it to the array then save that array into NSUserDefaults

encoding and decoding of upper given question is

the custom class .h file

#import <Foundation/Foundation.h>

@interface timeTable : NSObject<NSCoding>

@property (nonatomic) NSString * ID;
@property (nonatomic) NSString * type;
@property (nonatomic) NSString * time;
@property (nonatomic) NSString * busno;
@property (nonatomic) NSString * stops;

the custom class .m file

#import "timeTable.h"

@implementation timeTable
@synthesize ID;
@synthesize type;
@synthesize time;
@synthesize busno;
@synthesize stops;

-(void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:self.ID forKey:@"ID"]; [aCoder encodeObject:self.type forKey:@"type"]; [aCoder encodeObject:self.time forKey:@"time"]; [aCoder encodeObject:self.busno forKey:@"busno"]; [aCoder encodeObject:self.stops forKey:@"stops"];

}

-(id)initWithCoder:(NSCoder *)aDecoder
{
if((self = [super init])) {
    //decode properties, other class vars
    self.ID = [aDecoder decodeObjectForKey:@"ID"];
    self.type = [aDecoder decodeObjectForKey:@"type"];
    self.time = [aDecoder decodeObjectForKey:@"time"];
    self.busno = [aDecoder decodeObjectForKey:@"busno"];
    self.stops = [aDecoder decodeObjectForKey:@"stops"];
}
return self;
}

@end

where you encode each custom object one by one and adding it to the array then save that NSMutableArray or NSArray into NSUserDefaults

encoding a custom object then adding it to array and saving it into user defaults

 // encoding a custom object before saving it to array
 NSData *encodeTimeTableObj = [NSKeyedArchiver
    archivedDataWithRootObject:objectTimeTable];

 addObject:encodeTimeTableObj];
//saving it to user Defaults
 if(userDefaults)
 {
  [userDefaults setObject:arrayTimeTable
         forKey:@"ArrayOfTimeTables"];
         [userDefaults synchronize];
         NSLog(@"saving to usedefaults");
 }

retriving an array either mutable or non mutable then decoding each of its object

  NSMutableArray *arrayLoader = [userDefaults
       objectForKey:@"ArrayOfTimeTables"];
  NSData * decode = [arrayLoader objectAtIndex:0];

  // in case of upper given custom class Time Table timeTable *objPrint = [NSKeyedUnarchiver unarchiveObjectWithData:decode];

Raim
  • 1
  • 2
0

Use NSArray to get array from NSUSerDefaults as NSUSerDefaults return immuttable array. If you need NSMutableArray, then convert this NSArray to NSMutableArray.

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

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

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

for (int i=0; i

Karan Dua
  • 2,401
  • 3
  • 15
  • 17