Kevin
Kevin

Reputation: 1479

Reading NSMutableArray from File

I am trying to read an 2 NSMutableArrays from file. I am saving and loading as such:

SAVE:

NSMutableDictionary *saveDict = [NSMutableDictionary dictionary];
[saveDict setValue:name forKey:@"name"];
[saveDict setValue:last_episodue forKey:@"whereat"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingString:@"/ShopFile.sav"];
[saveDict writeToFile:filePath atomically:YES];

LOAD:

        name = [[NSMutableArray alloc]init];
    last_episodue = [[NSMutableArray alloc]init];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingString:@"/ShopFile.sav"];
    NSDictionary *loadDict = [NSDictionary dictionaryWithContentsOfFile:filePath];
    name =            [loadDict valueForKey:@"name"];
    last_episodue=     [loadDict valueForKey:@"whereat"];

The variables name and last_episodue have been declared in the header file.

The program compiles and runs, however at runtime when trying to load the file, the LOAD part of the code executes, and when it finishes, the program stops working. This is the debugging information (first part):

2012-10-13 12:14:10.801 series[5223:303] -[NSISRestrictedToZeroMarkerVariable copyWithZone:]: unrecognized selector sent to instance 0x1001900c0
2012-10-13 12:14:10.803 series[5223:303] -[NSISRestrictedToZeroMarkerVariable copyWithZone:]: unrecognized selector sent to instance 0x1001900c0
2012-10-13 12:14:10.906 series[5223:303] (

Any idea what the problem might be? Thanks!

Edit: This is the content of the file where the saving takes place:

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>name</key>
    <array>
        <string>a</string>
    </array>
    <key>whereat</key>
    <array>
        <string>a</string>
    </array>
</dict>
</plist>

Upvotes: 0

Views: 1488

Answers (1)

mattnunes
mattnunes

Reputation: 69

This is kind of a shot in the dark, since I can't tell without more context what your memory management looks like, but I just had this issue stemming from a variable whose value was not retained properly (we were assigning it using objc_setAssociatedObject but passing OBJC_ASSOCIATION_ASSIGN as the objc_AssociationPolicy). The pointer I held consistently ended up pointing over to an instance of NSISRestrictedToZeroMarkerVariable.

Since NSISRestrictedToZeroMarkerVariable is not a publicly-exposed class, what you're seeing is most likely the result of a memory overwrite. Set an exception breakpoint in Xcode and check out which line is throwing this error, and then track your memory management for that variable.

Upvotes: 4

Related Questions