从文件取消归档时 NSOrderedSet addObject 出现问题

发布于 2024-12-02 11:25:36 字数 2067 浏览 1 评论 0原文

我一直在网上到处搜索,并尝试了迄今为止我能想到的一切,但都无济于事。这是我的问题: 在我的程序中,我有一个名为 Scale 的类,Scale 有几个属性。用户可以通过单击名为 addScale 的按钮向 tableView 添加比例。当按下 addScale 时,我将其添加到 OrderedMutableSet 中。我使用该集合来存储秤并防止重复秤。我必须重写 isEqual 方法和 Hash 方法来欺骗计算机,让计算机认为两个具有相同属性的 Scale 完全相同。这很好用。但是,当我打开应用程序并且之前在 tableView 中有一些比例时,我已经实现了归档,因此这些比例会按预期重新出现。这次,当按下 addScale 时,我得到一个异常:

    -[__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'

这是我的 isEqual 方法和 Hash 方法的代码: (主音、调式、节奏、八度和节奏都是整数)

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

以下是我的 addScale 方法的代码:

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

ScaleStore 的 addScale 方法仅从 NSOrderedMutableSet 中调用 addObject,NSOrderedMutableSet 是存储所有音阶的位置。

非常感谢您提供的任何帮助。

谢谢你,

K

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 properties 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍风燃霜 2024-12-09 11:25:36

当您取消归档时,对象将恢复为不可变版本(NSMutableSet 变为 NSSet,NSMutableArray 变为 NSArray 等)。

只需使用恢复集中的对象创建一个新的 NSOrderedMutableSet 即可。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文