简体   繁体   English

不能将超过3个对象添加到NSMutableSet

[英]Can not add more than 3 objects to a NSMutableSet

Made a custom obj called Item with some string fields and one float. 使用一些字符串字段和一个浮点数创建了一个名为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;

implementation: 实施:

    #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

Now, I have NSMutableSet (itemsAdded which is a property of another class) inited as [[NSMutableSet alloc]init] and I can't add more than 3 Item objects to it. 现在,我将NSMutableSet(属于另一个类的属项,属性名)初始化为[[NSMutableSet alloc] init],并且不能向其添加3个以上的Item对象。 And BTW Items adding in very strange way... 而且BTW项目以非常奇怪的方式添加...

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

And this is what i get: 这就是我得到的:

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

Take a look there 2 times printed that set has 2 items but at this point there must be 3 items. 看一看有2次打印,该打印有2个项目,但此时必须有3个项目。 It works randomly and could print it has 2 items more than 2 times. 它可以随机工作,并且可以打印2次以上两次。 Also no reaction on adding more than 3 items at all. 同样,添加超过3个项目也没有反应。 Only 3 items persist 4ever. 只有3个物品可以永久存在4个。 Why is that? 这是为什么? What I'm doing wrong? 我做错了什么?

If you create identical objects, only one copy will be put into the set. 如果创建相同的对象,则仅将一个副本放入集合中。

This is by design. 这是设计使然。 If you want multiple copies, use an array instead. 如果要多个副本,请改用数组。

This is probably happening more than you would expect though, because you are using random and not seeding it properly. 但是,这可能比您预期的要发生的更多,因为您使用的是random变量,并且没有正确植入种子。 Use arc4random() % 100 (or a number even bigger than 100) if you want it to happen less often, or change some of the other variables so that the objects are not identical and you will see this problem go away. 如果希望它不经常发生,请使用arc4random() % 100 (或大于100的数字),或者使用其他一些变量来使对象不相同,这样您就可以解决此问题。

EDIT: 编辑:

Another problem that you have is this line of code: 您遇到的另一个问题是以下代码行:

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

In order to make the proper test, change it to: 为了进行适当的测试,请将其更改为:

if (self.num != anItem.num)

(Otherwise you are comparing the BOOL !self.num to the float anItem.num) (否则,您将BOOL!self.num与float anItem.num进行比较)

i have some remark in your code in your IBAction : 我在您的IBAction中的代码中有一些评论:

  1. First you dont need to NSAutoreleasePool and for release a pool you have to do this : [pool drain] 首先,您不需要NSAutoreleasePool,而要释放池,您必须执行以下操作:[池排水]

  2. Are you sure to declare your NSMutableSet by alloc init noramly or by alloc initWithCapacity !! 您确定要通过alloc init或alloc initWithCapacity来声明您的NSMutableSet!

  3. i suggest you NSMutableArray 我建议你NSMutableArray

  4. Dont forhet to release you item after added in your MutableArray 添加到MutableArray中后,请不要放开您的物品

i Hope that help you 希望对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM