2013-07-07 84 views
0

我有一個錯誤,它不會存儲,如果我添加NSIndexPath到字典存儲,NSIndexPath對象是self.lectureNumber如果我從字典中刪除它我想商店,它存儲得很好。問題是什麼?iOS:NSIndexPath在NSDictionary - 不會存儲到磁盤

NSDictionary *objectToStore = @{ 
            CACHE_KEY_TITLE    : self.audioTitle, 
            CACHE_KEY_AUDIOFILE   : self.audiofile, 
            CACHE_KEY_COLLECTION_NAME : self.collectionName, 
            CACHE_KEY_DATE_ADDED  : [NSDate dateWithTimeIntervalSinceNow:0], 
            CACHE_KEY_LECTURE_NUMBER : self.lectureNumber, 
            CACHE_KEY_NUM_LECTURES  : self.numLecturesInCollection 
            }; 

然後我有一個蔡斯I類存儲在

SimpleCache *cache = [[SimpleCache alloc]init]; 
    [cache storeObject:objectToStore withName:self.audiofile inCategory:kFavorites]; 

SimpleCache的字典:

- (BOOL)storeObject:(id)object withName: (NSString *)filename inCategory:(StorageType)category 
{ 
    NSString *storageFolder = [self getCategoryFor:category]; 

    if (object) { 
     NSFileManager *fileManager = [[NSFileManager alloc]init]; 

     // Create folder 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder 
     NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:storageFolder]; 
     NSString *dataPathFormated = [dataPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

     if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) { 
      [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil]; 
      NSLog(@"Created new directory"); 
     }   

     NSURL *destinationURL = [[NSURL alloc]initWithString:[NSString stringWithFormat:@"%@/%@",dataPathFormated,filename]]; 
     NSString *destinationString = [NSString stringWithFormat:@"%@/%@",dataPath,filename]; 

     if (![fileManager fileExistsAtPath:[destinationURL path]]) { 
      if (YES) { // if table view list is shorter than 50 

       BOOL success = [object writeToFile:destinationString atomically:YES]; 

       //BOOL success2 = [testObject writeToURL:destinationURL atomically:YES encoding:CFStrin error:<#(NSError *__autoreleasing *)#>]; 

       NSLog(@"writing to disk was success?: %d", success); 
      } else { 

      } 
     } 
    } 
    return YES; 
} 

回答

2

從文檔的writeToFile:atomically

這種方法遞歸驗證所有包含的對象在寫出文件之前,ts是屬性列表對象(NSData,NSDate,NSNumber,NSString,NSArray或NSDictionary的實例),如果所有對象都不是屬性列表對象,則返回NO,因爲結果文件不是有效屬性名單。

NSIndexPath不是一個屬性列表對象,所以它不能寫入這樣的文件。您應該能夠使用NSKeyedArchiver存儲字典,因爲它符合NSCoding協議。