开发者

Problem with NSOrderedSet addObject when unarchiving from file

开发者 https://www.devze.com 2023-03-31 07:55 出处:网络
I have been searching everywhere on the web and trying everything that I can think of so far to no avail. Here is my problem:

I have been searching everywhere on the web and trying everything that I can think of so far to no avail. Here is my problem: In my program I have a class called Scale, Scale has several properties. The user can add scales to a tableView by clicking a button called addScale. When addScale is pressed, I add it to a OrderedMutableSet. I use the set to store the scales and prevent duplicate scales. I had to override the isEqual method and the Hash method to fool the computer into thinking that two Scales with the same propertie开发者_开发问答s are exactly the same. This works fine. But when I open the app and have had previously some scales in the tableView, I have archiving implemented so these scales reappear, as expected. This time, when addScale is pressed, I get an exception:

    -[__NSOrderedSetI addObject:]: unrecognized selector sent to instance 0x746dbf0
    2011-08-29 13:20:49.095 MusicLog[678:f203]
    *** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
    reason: '-[__NSOrderedSetI addObject:]: unrecognized selector sent to instance 0x746dbf0'

Here is the code for my isEqual method and my Hash method: (tonic, mode, rhythm, octaves, and tempo are all ints)

- (NSUInteger)hash
{
    NSUInteger prime = 31;
    NSUInteger result = 1;
    result = prime * result + tonic;
    result = prime * result + mode;
    result = prime * result + rhythm;
    result = prime * result + octaves;
    result = prime * result + tempo;
    NSLog(@"%u", result);
    NSLog(@"%u", NSUIntegerMax);

    return result;
}


- (BOOL)isEqual:(Scale *)object
{
    if ((tonic == [object tonic])
        && (mode == [object mode])
        && (rhythm == [object rhythm])
        && (octaves == [object octaves])
        && (tempo == [object tempo]))
        return YES;
    else 
        return NO;
}

Here is the code for my addScale method:

- (IBAction)addScale:(id)sender
{
    Scale *chosenScale = [[Scale alloc] init];
    [chosenScale setTonic:[myPicker selectedRowInComponent:0]];
    [chosenScale setMode:[myPicker selectedRowInComponent:1]];
    [chosenScale setRhythm:[myPicker selectedRowInComponent:2]];
    [chosenScale setOctaves:[octavesSegmentedControl selectedSegmentIndex] + 1];
    [chosenScale setTempo: (int) [tempoStepper value]];
    [[ScaleStore defaultStore] addScale:chosenScale];
}

the addScale method of the ScaleStore just calls addObject from a NSOrderedMutableSet which is where all the scales are stored.

Any help you can give is greatly appreciated.

Thank you,

K


When you unarchive, the objects are restored to an immutable version (NSMutableSet becomes NSSet, NSMutableArray becomes an NSArray, etc.)

Just create a new NSOrderedMutableSet with objects from the restored set.

0

精彩评论

暂无评论...
验证码 换一张
取 消