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

Ios 无法在NSUserDefaults中存储NSMutableArray(数组的每个实例都包含一个自定义对象),ios,objective-c,iphone,nsuserdefaults,Ios,Objective C,Iphone,Nsuserdefaults,这是一个自定义类: #import <Foundation/Foundation.h> @interface timeTable : NSObject @property (nonatomic) int ID; @property (nonatomic) NSString * type; @property (nonatomic) NSString * time; @property (nonatomic) NSString * busno; @property (nonatomi

这是一个自定义类:

#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);
  }
#导入
@接口时间表:NSObject
@属性(非原子)int-ID;
@属性(非原子)NSString*类型;
@属性(非原子)NSString*时间;
@属性(非原子)NSString*busno;
@属性(非原子)NSString*停止;
//它的.m文件中没有任何操作,甚至没有合成
//那是另一门课
#进口
#导入“timeline.h”
@接口刷新数据库:NSObject
@属性(强,非原子)NSMutableArray*ArrayMetable;
@属性(强,非原子)时间表*对象时间表;
//在它的.m文件中,我使用
服务,然后我将其保存到NSM阵列
//下载包含一行数据的json数组
n错误*错误;
NSArray*jsonArray=[NSJSONSerialization JSONObjectWithData:
[安全字符串数据使用编码:NSUTF8StringEncoding]
选项:NSJSONReadingAllowFragments错误:&错误];
NSLog(@“json数组%@”,jsonArray);
//用于获取数组的实例
NSDictionary*jsonement;

对于(int i=0;i使用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;
-(void)编码器与编码器:(NSCoder*)一个编码器 { [aCoder encodeObject:self.ID forKey:@“ID”]; [aCoder encodeObject:self.type forKey:@“type”]; [aCoder-encodeObject:self.time-forKey:@“time”]; [A编码对象:self.busno-forKey:@“busno”]; [aCoder-encodeObject:self.stops-forKey:@“stops”]

将每个自定义对象逐个编码并将其添加到数组中,然后保存NSMutableArray或NSArray 陷入违约状态

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

 // 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];
//如果是上给定的自定义类时间表
timeline*objPrint=[NSKeyedUnarchiver unarchiveObjectWithData:decode];

使用NSArray从NSUSerDefaults获取数组,因为NSUSerDefaults返回不可变数组。 如果需要NSMutableArray,请将此NSArray转换为NSMutableArray

//从NSUSerDefaults检索保存的数组并打印它 //用功

时间表*objPrint=[[timeline alloc]init]

NSArray*arrayLoader=[userDefaults ArrayWorkey:@“ArrayOfTimeTables]”


为了(int i=0;i您的自定义对象必须符合
NSCodingProtocol
检查这个答案:如果我只是将时间表类对象存储到NSUserDEFAULTS中,那么这个答案是好的,但是我首先将时间表对象存储到NSMutableArray中,然后将该数组保存到NSUserDEFAULTS中,顺便说一句,您的自定义对象所拥有的时间遵守NSCODING协议。
 // 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];