2012-03-28 17 views
0

いくつかの文字列フィールドと1つのfloatを持つItemというカスタムobjを作成しました。NSMutableSetに3つ以上のオブジェクトを追加できません

.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が)としてinited有する[[NSMutableSet ALLOC] INIT]及びIは3つ以上の項目を追加することはできませんそれへのオブジェクト。そしてところでアイテムは...

-(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続く。 なぜですか?私は間違っているの?

+1

はあなたが同一の項目を追加していないことを確認していることを願っていますアイテムをリリースするforhet ? – yuji

+0

どのようにランダムに何度も連続して使用することができますか?とにかく、前にaはNumプロパティなしで同じ結果を得ました。私はすぐに項目の違いを作るために追加しました。しかし、それはfxを持っていません...そして、毎回違いがあります、それは確かです。 – Stan

+0

実際には、 'Item'の' isEqual: 'と' hash'の実装が合理的であることを確認する必要があります結果。 – yuji

答えて

3

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

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

randomを使用しており、適切にシードしていないため、これはおそらく予想以上に発生しています。少ない頻度で実行したい場合はarc4random() % 100(または100より大きい数値)を使用するか、オブジェクトが同一でないように他の変数を変更して、この問題が解消するようにします。

EDIT:

あなたが持っているもう一つの問題は、このコード行です:

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

適切なテストを行うために、それを変更します。

if (self.num != anItem.num) 

(そうでなければBOOL!self.numとfloat anItem.numを比較しています)

+0

ああ私の神!この比較は正しかったと思っていましたが、すぐにそれを忘れてしまいました。ありがとう!!! – Stan

+1

私はNSMutableSetの代わりにNSCountedSetを試しましたが、同じ結果が得られました。今それはどちらの場合にも機能します – Stan

0

私はあなたのcあなたのIBActionでODE:

  1. まず、あなたがこれをしなければならないプールNSAutoreleasePoolに必要と解放のためにいけない:[プールドレイン]を

  2. はあなたがnoramlyのinitのallocしてNSMutableSetを宣言してもよろしいですかallocによってinitWithCapacity !!

  3. 私はあなたにNSMutableArrayの

    を示唆
  4. あなたMutableArray

で追加された後、いけない、私はあなたを助ける

関連する問題