0

制作了一个名为 Item 的自定义 obj,其中包含一些字符串字段和一个浮点数。

.h
        @interface Item : NSObject {

        NSString * name;        
        NSString * artNum;      
        NSString * collection;  
        NSString * description; 

        float num;

    }

    @property (nonatomic, copy) NSString * name;
    @property (nonatomic, copy) NSString * artNum;
    @property (nonatomic, copy) NSString * collection;
    @property (nonatomic, copy) NSString * description;
    @property (nonatomic) float num;

- (id)initWithName:(NSString *) name_ 
         andArtNum:(NSString *) artNum_
     andCollection:(NSString *) collection_
    andDescription:(NSString *) description_
          andNum:(float)num_;
- (id) init;
- (BOOL)isEqualToItem:(Item *)anItem;
- (NSUInteger)hash;

执行:

    #import "Item.h"


@implementation Item

@synthesize name;
@synthesize artNum;
@synthesize collection;
@synthesize description;
@synthesize num;

- (id)initWithName:(NSString *) name_ 
         andArtNum:(NSString *) artNum_
     andCollection:(NSString *) collection_
    andDescription:(NSString *) description_
          andNum:(float)num_
{
    if (self = [super init]) {
        self.name       = name_;
        self.artNum     = artNum_;
        self.collection = collection_;
        self.description= description_;
        self.num        = num_;
    }
    return self;
}

-(id)init{
    return [self initWithName:@"" 
                    andArtNum:@""        
                andCollection:@""
              andDescription :@""
                     andNum:.0];
}

- (void)dealloc {
    [name release];
    [artNum release];
    [collection release];
    [description release];
    [super dealloc];
}

- (BOOL)isEqual:(id)other {
    if (other == self)
        return YES;
    if (!other || ![other isKindOfClass:[self class]])
        return NO;
    return [self isEqualToItem:other];
}

- (BOOL)isEqualToItem:(Item *)anItem {
    if (self == anItem)
        return YES;
    if (![(id) self.name         isEqual:anItem.name])
        return NO;
    if (![(id) self.artNum       isEqual:anItem.artNum])
        return NO;
    if (![(id) self.collection   isEqual:anItem.collection])
        return NO;
    if (![(id) self.description  isEqual:anItem.description])
        return NO;
    if (!self.num == anItem.num)
        return NO;

//    if (![[self customObject] isEqualToCustomObject:[anItem customObject]])
//        return NO;
    return YES;
}

- (NSUInteger)hash {
    NSString *string4Hash = [NSString stringWithFormat:@"%@%@%@%@%f",self.name,self.artNum,self.collection,self.description,self.num];
    NSUInteger hash = [string4Hash hash];
    return hash;
}

@end

现在,我将 NSMutableSet(itemsAdded 是另一个类的属性)初始化为 [[NSMutableSet alloc]init] 并且我不能向其中添加超过 3 个 Item 对象。顺便说一句,项目以非常奇怪的方式添加......

    -(IBAction) itemAdd:(id)sender{
    NSLog(@"itemAdded");
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    Item *item = [[Item alloc] initWithName:lblItemName.text
                                  andArtNum:lblItemArt.text
                              andCollection:lblItemCollection.text
                                                         andDescription:lblItemDescriprion.text
                                   andNum:random()*100];
    [self.itemsAdded addObject:item];

    NSLog(@"itemAdd: intems added count: %i",[self.itemsAdded count]);
    for (Item *it in itemsAdded){
        NSLog(@"item added num is: %f",it.num);
    }
    [pool release];
}

这就是我得到的:

2012-03-28 20:22:49.479 MyApp[2493:207] itemAdded
2012-03-28 20:22:49.479 MyApp[2493:207] itemAdd: intems added count: 1
2012-03-28 20:22:49.479 MyApp[2493:207] item added num is: 40311868.000000
2012-03-28 20:22:51.175 MyApp[2493:207] itemAdded
2012-03-28 20:22:51.176 MyApp[2493:207] itemAdd: intems added count: 2
2012-03-28 20:22:51.176 MyApp[2493:207] item added num is: 40311868.000000
2012-03-28 20:22:51.176 MyApp[2493:207] item added num is: -1206257280.000000
2012-03-28 20:22:51.448 MyApp[2493:207] itemAdded
2012-03-28 20:22:51.448 MyApp[2493:207] itemAdd: intems added count: 2
2012-03-28 20:22:51.448 MyApp[2493:207] item added num is: 40311868.000000
2012-03-28 20:22:51.448 MyApp[2493:207] item added num is: -1206257280.000000
2012-03-28 20:22:51.623 MyApp[2493:207] itemAdded
2012-03-28 20:22:51.623 MyApp[2493:207] itemAdd: intems added count: 3
2012-03-28 20:22:51.623 MyApp[2493:207] item added num is: 40311868.000000
2012-03-28 20:22:51.623 MyApp[2493:207] item added num is: -335000352.000000
2012-03-28 20:22:51.624 MyApp[2493:207] item added num is: -1206257280.000000
2012-03-28 20:22:51.815 MyApp[2493:207] itemAdded
2012-03-28 20:22:51.816 MyApp[2493:207] itemAdd: intems added count: 3
2012-03-28 20:22:51.816 MyApp[2493:207] item added num is: 40311868.000000
2012-03-28 20:22:51.816 MyApp[2493:207] item added num is: -335000352.000000
2012-03-28 20:22:51.816 MyApp[2493:207] item added num is: -1206257280.000000
2012-03-28 20:22:51.991 MyApp[2493:207] itemAdded
2012-03-28 20:22:51.992 MyApp[2493:207] itemAdd: intems added count: 3
2012-03-28 20:22:51.992 MyApp[2493:207] item added num is: 40311868.000000
2012-03-28 20:22:51.992 MyApp[2493:207] item added num is: -335000352.000000
2012-03-28 20:22:51.992 MyApp[2493:207] item added num is: -1206257280.000000
2012-03-28 20:22:52.175 MyApp[2493:207] itemAdded
2012-03-28 20:22:52.175 MyApp[2493:207] itemAdd: intems added count: 3
2012-03-28 20:22:52.175 MyApp[2493:207] item added num is: 40311868.000000
2012-03-28 20:22:52.175 MyApp[2493:207] item added num is: -335000352.000000
2012-03-28 20:22:52.176 MyApp[2493:207] item added num is: -1206257280.000000
2012-03-28 20:22:52.360 MyApp[2493:207] itemAdded
2012-03-28 20:22:52.361 MyApp[2493:207] itemAdd: intems added count: 3
2012-03-28 20:22:52.361 MyApp[2493:207] item added num is: 40311868.000000
2012-03-28 20:22:52.361 MyApp[2493:207] item added num is: -335000352.000000
2012-03-28 20:22:52.361 MyApp[2493:207] item added num is: -1206257280.000000
2012-03-28 20:22:52.591 MyApp[2493:207] itemAdded
2012-03-28 20:22:52.591 MyApp[2493:207] itemAdd: intems added count: 3
2012-03-28 20:22:52.592 MyApp[2493:207] item added num is: 40311868.000000
2012-03-28 20:22:52.592 MyApp[2493:207] item added num is: -335000352.000000
2012-03-28 20:22:52.592 MyApp[2493:207] item added num is: -1206257280.000000

看看那里打印了 2 次,该套装有 2 个项目,但此时必须有 3 个项目。它随机工作,可以打印 2 个项目超过 2 次。对添加超过 3 个项目也没有反应。只有 3 个项目持续 4ever。这是为什么?我做错了什么?

4

2 回答 2

3

如果您创建相同的对象,则只会将一个副本放入集合中。

这是设计使然。如果您想要多个副本,请改用数组。

但是,这可能比您预期的要多,因为您正在使用random它而不是正确地播种。如果您希望它不经常发生,请使用arc4random() % 100(或甚至大于 100 的数字),或更改其他一些变量以使对象不相同,您将看到此问题消失。

编辑:

您遇到的另一个问题是这行代码:

if (!self.num == anItem.num)

为了进行正确的测试,请将其更改为:

if (self.num != anItem.num)

(否则,您将 BOOL !self.num 与 float anItem.num 进行比较)

于 2012-03-28T16:42:52.483 回答
0

我在您的 IBAction 代码中有一些评论:

  1. 首先,您不需要 NSAutoreleasePool 并且要释放池,您必须这样做:[池排水]

  2. 您确定通过 alloc init noramly 或 alloc initWithCapacity 声明您的 NSMutableSet !

  3. 我建议你使用 NSMutableArray

  4. 添加到您的 MutableArray 后,不要放弃释放您的项目

我希望对你有帮助

于 2012-03-28T17:40:03.740 回答