2012-03-28 55 views
0

做了一個自定義obj稱爲Item與一些字符串字段和一個浮點數。不能添加超過3個對象到一個NSMutableSet

.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 [[ALLOC的NSMutableSet] INIT]和我不能添加超過3項反對它。和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]; 
} 

而這就是我得到:

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項持續存在。 這是爲什麼?我做錯了什麼?

+1

你確定你不是添加相同的物品? – yuji 2012-03-28 16:35:59

+0

怎麼可能連續使用這麼多次?無論如何,先前沒有Num屬性得到了相同的結果。我很快添加了它,以便在項目之間做出區別。但它沒有fx ...而且Num每一次都是不同的,那就是4sure ... – Stan 2012-03-28 16:38:05

+0

實際上,你需要做的是確保''isEqual:''和'hash'的Item'的實現具有合理性結果。 – yuji 2012-03-28 16:42:41

回答

3

如果您創建相同的對象,則只會將一個副本放入集合中。

這是設計。如果您想要多個副本,請改用數組。

雖然這可能發生得比您期望的要多,因爲您正在使用random而不是正確播種。如果您希望發生次數較少,請使用arc4random() % 100(或者一個大於100的數字),或者更改其他一些變量以使對象不相同,您將看到此問題消失。

編輯:

,你有另一個問題是這行代碼:

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

爲了作出適當的測試,將其更改爲:

if (self.num != anItem.num) 

(否則你比較BOOL!self.num和float anItem.num)

+0

我的天啊!我在想這個比較是不對的,但很快就忘了。感謝名單! – Stan 2012-03-28 16:51:32

+1

順便說一句我沒有嘗試NSCountedSet而不是NSMutableSet,但結果相同。現在它適用於這兩種情況 – Stan 2012-03-28 16:59:58

0

我在你的c中有一些註釋頌在IBAction爲:

  1. 首先你不需要NSAutoreleasePool併發布你必須做這個池:池排水]

  2. 你一定要聲明你的NSMutableSet通過的alloc初始化noramly或通過alloc initWithCapacity!

  3. 我建議你的NSMutableArray

  4. 不要forhet釋放你的項目加入您的MutableArray後

我希望幫助您