0

我试图NSDictionary在我的模型的实现文件中创建一个完整的数组,但我的代码还没有工作。我想创建作为狗和猫类型列表的数组,然后将这些数组添加到带有名为DOGand的键的字典中CAT。这是我的代码:

@implementation wordDictionary

@synthesize catList = _catList;
@synthesize dogList = _dogList;
@synthesize standardDictionary =_standardDictionary;
- (void)setCatList:(NSMutableArray *)catList
{
     self.catList = [NSMutableArray arrayWithObjects:@"lion", @"puma", @"snow leopard", nil]; 
}
- (void)setDogList:(NSMutableArray *)dogList
{
    self.dogList = [NSMutableArray arrayWithObjects:@"pit bull", @"pug", @"chihuahua", nil];
}
-(void)setStandardDictionary:(NSMutableDictionary *)standardDictionary 
{
    [self.standardDictionary setObject: _catList forKey:@"CAT"];
    [self.standardDictionary setObject: _dogList forKey:@"DOG"];
}

- (NSString*)selectKey    
{
    NSInteger keyCount = [[self.standardDictionary allKeys] count];
    NSInteger randomKeyIndex = arc4random() % keyCount;
    NSString *randomKey = [[self.standardDictionary allKeys] objectAtIndex:randomKeyIndex];
    return randomKey;
}
@end

此代码是模型。该模型连接到我的视图控制器,这样当用户点击一个按钮时,NSString返回的randomKey内容会显示在屏幕上的标签中。因此文本将显示为CATDOG。这是代码:

- (IBAction)changeGreeting:(UIButton*)sender {
    NSString *chosenKey = [self.dictionary selectKey];
    NSString *labelText = [[NSString alloc] initWithFormat:@"%@", chosenKey];
    self.label.text = labelText;
}

不幸的是,当我点击模拟器上的按钮时,我收到一条错误消息:Thread 1:EXC_ARITHMETIC (code=EXC_1386_DIV, subcode=0x0)atNSInteger randomKeyIndex = arc4random() % keyCount;并且似乎我得到了它,因为 myNSArray和 myNSDictionary里面都没有任何对象。

有谁知道为什么我的NSArrayNSDictionary没有被填充?

非常感谢。

4

3 回答 3

3

简单的答案是这里没有任何代码调用方法来设置数组或字典。

但真正的根本问题是,这里有几个不好的“模式”,你应该修复:

在您的 setter 方法(setCatList:、setDogList:、setStandardDictionary:) 中,您没有将相关属性设置为传入的值。例如,您应该将 catList 设置为传入的“catList”变量。

- (void)setCatList:(NSMutableArray *)catList
{
    if (_catList != catList) {
      [_catList release];
      _catList = [catList retain];
    }
}

然后你应该有某种“设置”发生,通常在视图控制器中的一个方法中,比如 viewDidLoad:

[wordDictionary setCatList:[NSMutableArray arrayWithObjects:@"lion", @"puma", @"snow leopard", nil]]; 
// and more for the other two setters

或者,您可以在 wordDictionary 类的 init 中设置这些默认值:

- (id)init {
    self = [super init];
    if (self) {
        [self setCatList:[NSMutableArray arrayWithObjects:@"lion", @"puma", @"snow leopard", nil]]; 
    }
    return self;
}

在大多数情况下,前者更好,但您可能有充分的理由为该类的所有实例预填充模型。

于 2012-03-05T01:53:24.087 回答
2

假设你打电话给setCatList:,setDogList:setStandardDictionary:之前。可能导致是这样的:

 NSString *chosenKey = [self.dictionary selectKey];

改成这样:

 NSString *chosenKey = [self selectKey]; 

更新

我正在努力让你的生活更轻松。如果您最不需要,则无需创建您的对象。

- (NSMutableArray*)getCatList
{
     return [NSMutableArray arrayWithObjects:@"lion", @"puma", @"snow leopard", nil]; 
}
- (NSMutableArray*)getDogList
{
   return [NSMutableArray arrayWithObjects:@"pit bull", @"pug", @"chihuahua", nil];
}
-(NSMutableDictionary*)getStandardDictionary 
{
    NSMutableDictionary *standardDictionary = [NSMutableDictionary new];
    [standardDictionary setObject:[self getCatList] forKey:@"CAT"];
    [standardDictionary setObject:[self getDogList] forKey:@"DOG"];
    return [standardDictionary autorelease];
}

- (NSString*)selectKey    
{
    NSMutableDictionary *standardDictionary = [self getStandardDictionary];
    NSInteger keyCount = [[standardDictionary allKeys] count];
    NSInteger randomKeyIndex = arc4random() % keyCount;
    NSString *randomKey = [[standardDictionary allKeys] objectAtIndex:randomKeyIndex];
    return randomKey;
}

- (IBAction)changeGreeting:(UIButton*)sender {
   // NSString *chosenKey = [self selectKey];
    //NSString *labelText = [[NSString alloc] initWithFormat:@"%@", chosenKey];
    self.label.text = [self selectKey]; //no need to convert it to NSString again
}
于 2012-03-05T01:56:49.507 回答
1

需要考虑两件事:

我没有看到你打电话给这些:

setCatList:(NSMutableArray*)catList;
setDogList:(NSMutableArray*)dogList;

您使用 self.catList 和 self.dogList,但它们都不是合成的,而是合成了 beatList 和 meList

将合成器更改为 catList 和 dogList,并确保调用 set list 方法,然后您应该会取得一些进展。

于 2012-03-05T01:53:07.560 回答