0

我有一个错误,如果我将 NSIndexPath 添加到我存储的字典中,它不会存储,NSIndexPath 对象是self.lectureNumber如果我从我要存储的字典中删除它,它存储得很好。问题可能是什么?

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

然后我有一个 chace 类,我将字典存储在

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

简单缓存:

- (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;
}
4

1 回答 1

2

从文档中writeToFile:atomically

此方法在写出文件之前递归地验证所有包含的对象是属性列表对象(NSData、NSDate、NSNumber、NSString、NSArray 或 NSDictionary 的实例),如果所有对象都不是属性列表对象,则返回 NO,因为结果文件不是有效的属性列表。

NSIndexPath 不是属性列表对象,因此不能将其写入这样的文件。您应该能够使用存储字典,NSKeyedArchiver因为它确实符合 NSCoding 协议。

于 2013-07-07T15:53:07.593 回答