无法在 NSUserDefaults 中存储 NSMutableArray(数组的每个实例都包含一个自定义对象)

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

这是一个习俗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);
  }

非常感谢您提前帮助我。

请告诉我如何将包含时间表 class 对象的数组保存到 nsUseDefaults 中,然后如何将其加载回来。

请帮帮我。我读了很多类似的问题和答案,但不知道如何让它们为我工作。

使用 NScoding 对每个自定义对象进行编码,然后将该自定义对象添加到数组中,然后对其他对象进行编码,然后将其添加到数组中,然后将该数组保存到 NSUserDefaults

上题的编解码 是

自定义 class.h 文件

#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;

自定义 class .m 文件

#import "timeTable.h"

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

-(void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:self.Key ID:@"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

在其中对每个自定义对象一一编码并将其添加到数组中,然后保存该 NSMutableArray 或 NSArray 进入 NSUserDefaults

对自定义对象进行编码,然后将其添加到数组并将其保存到用户默认值中

 // 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");
 }

检索可变或非可变数组,然后解码其每个对象

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

// 如果是上给定的自定义 class 时间 Table 时间Table *objPrint = [NSKeyedUnarchiver unarchiveObjectWithData:decode];

使用 NSArray 从 NSUSerDefaults 获取数组作为 NSUSerDefaults return 不可变数组。 如果你需要 NSMutableArray,那么将这个 NSArray 转换为 NSMutableArray。

// 从 NSUSerDefaults 中检索保存的数组并打印它 // 使用 slog

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

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

for (int i=0; i