Question

I have a bug where it won´t store if I add NSIndexPath to the dictionary i store, the NSIndexPath object is self.lectureNumber if I remove this from the dictionary i want to store, it stores just fine. What can the problem be?

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

Then I have a chace class I store the dict in

    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;
}
Was it helpful?

Solution

From the docs for writeToFile:atomically:

This method recursively validates that all the contained objects are property list objects (instances of NSData, NSDate, NSNumber, NSString, NSArray, or NSDictionary) before writing out the file, and returns NO if all the objects are not property list objects, since the resultant file would not be a valid property list.

NSIndexPath is not a property list object so it can't be written to a file like this. You should be able to store the dictionary using NSKeyedArchiver since it does conform to the NSCoding protocol.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top