2012-03-05 98 views
0

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

@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 

此代码是模型。模型连接到我的视图控制器,以便当用户点击按钮时,从randomKey返回的NSString显示在屏幕上的标签中。所以文本将会读取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也不是我NSDictionary有任何在它们内部的对象。

有没有人有任何想法为什么我的NSArrayNSDictionary尚未填充?

非常感谢。

回答

2

假设您以前拨打setCatList:,setDogList:setStandardDictionary:。可能是导致是这样的:

NSString *chosenKey = [self.dictionary selectKey]; 

变化成这样:

NSString *chosenKey = [self selectKey]; 

UPDATE

我试图让您的生活更轻松。如果你不需要最多的话,不需要创建你的对象。

- (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 
} 
+0

也是一个好点 – 2012-03-05 01:57:57

1

两件事情要考虑:

我没有看到你调用这些:

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

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

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

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 

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

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

前者是在大多数情况下更好,但你可能有一个很好的理由来预填充模型类的所有实例。

+0

那么在这种情况下,没有办法让模型数据和控制器分开吗?我在想,我不应该把视图控制器中的列表放在MVC设计中。这是不必要的?感谢您的回应。 – 2012-03-05 02:03:33

+0

您可以为此模型类提供初始值设定项,以设置这些值。我也会附上我的答案。 – 2012-03-05 02:05:02

+0

非常感谢!我是新来的,所以非常感谢帮助。其他 – 2012-03-05 02:07:08