为模型数据填充NSDictionary和NSArrays

时间:2012-03-05 01:36:03

标签: iphone cocoa-touch nsarray nsdictionary xcode4.3

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

@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)NSInteger randomKeyIndex = arc4random() % keyCount;,似乎我得到了它,因为我的NSArray和{{{}} {1}}内部有任何对象。

有没有人知道为什么我的NSDictionaryNSArray尚未填充?

非常感谢。

3 个答案:

答案 0 :(得分: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;
}

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

答案 1 :(得分: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
}

答案 2 :(得分:1)

需要考虑两件事:

我没有看到你称这些:

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

你使用self.catList和self.dogList,但这些都没有被合成,而是你有beatList和meList合成

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