[英]Can not add more than 3 objects to a NSMutableSet
使用一些字符串字段和一個浮點數創建了一個名為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(屬於另一個類的屬項,屬性名)初始化為[[NSMutableSet alloc] init],並且不能向其添加3個以上的Item對象。 而且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次以上兩次。 同樣,添加超過3個項目也沒有反應。 只有3個物品可以永久存在4個。 這是為什么? 我做錯了什么?
如果創建相同的對象,則僅將一個副本放入集合中。
這是設計使然。 如果要多個副本,請改用數組。
但是,這可能比您預期的要發生的更多,因為您使用的是random
變量,並且沒有正確植入種子。 如果希望它不經常發生,請使用arc4random() % 100
(或大於100的數字),或者使用其他一些變量來使對象不相同,這樣您就可以解決此問題。
編輯:
您遇到的另一個問題是以下代碼行:
if (!self.num == anItem.num)
為了進行適當的測試,請將其更改為:
if (self.num != anItem.num)
(否則,您將BOOL!self.num與float anItem.num進行比較)
我在您的IBAction中的代碼中有一些評論:
首先,您不需要NSAutoreleasePool,而要釋放池,您必須執行以下操作:[池排水]
您確定要通過alloc init或alloc initWithCapacity來聲明您的NSMutableSet!
我建議你NSMutableArray
添加到MutableArray中后,請不要放開您的物品
希望對您有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.