0

Made a custom obj called Item with some string fields and one float.

.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. And BTW Items adding in very strange way...

    -(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. It works randomly and could print it has 2 items more than 2 times. Also no reaction on adding more than 3 items at all. Only 3 items persist 4ever. Why is that? What I'm doing wrong?

4

2 に答える 2

3

同一のオブジェクトを作成すると、1 つのコピーのみがセットに入れられます。

これは仕様によるものです。複数のコピーが必要な場合は、代わりに配列を使用してください。

ただし、これはおそらく予想以上に多く発生し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 に答える