Iphone 为模型数据填充NSDictionary和NSArray
我正试图在我的模型的实现文件中创建一个Iphone 为模型数据填充NSDictionary和NSArray,iphone,cocoa-touch,nsarray,nsdictionary,xcode4.3,Iphone,Cocoa Touch,Nsarray,Nsdictionary,Xcode4.3,我正试图在我的模型的实现文件中创建一个NSDictionary满是数组,但我的代码还没有运行。我想创建一个数组,该数组是狗和猫的类型列表,然后将这些数组添加到一个字典中,其中包含名为DOG和CAT的键。这是我的密码: @implementation wordDictionary @synthesize catList = _catList; @synthesize dogList = _dogList; @synthesize standardDictionary =_standardDict
NSDictionary
满是数组,但我的代码还没有运行。我想创建一个数组,该数组是狗和猫的类型列表,然后将这些数组添加到一个字典中,其中包含名为DOG
和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
这段代码就是模型。该模型与我的视图控制器相连,这样当用户点击按钮时,从randomKey
返回的NSString
将显示在屏幕上的标签中。因此,文本将读取CAT
或DOG
。下面是代码:
- (IBAction)changeGreeting:(UIButton*)sender {
NSString *chosenKey = [self.dictionary selectKey];
NSString *labelText = [[NSString alloc] initWithFormat:@"%@", chosenKey];
self.label.text = labelText;
}
不幸的是,当我点击模拟器上的按钮时,我收到一条错误消息,上面写着:thread1:EXC_算术(code=EXC_1386_DIV,subcode=0x0)
atNSInteger randomKeyIndex=arc4random()%keyCount
我之所以得到它,似乎是因为我的NSArray
和NSDictionary
中都没有任何对象
有人知道为什么我的NSArray
和NSDictionary
没有被填充吗
非常感谢。需要考虑两件事: 我看不出你把这些叫做:
setCatList:(NSMutableArray*)catList;
setDogList:(NSMutableArray*)dogList;
您使用self.catList和self.dogList,但它们都不是合成的,而是合成了beatList和meList
将合成更改为catList和dogList,并确保调用set list方法,然后应该取得一些进展。需要考虑两件事: 我看不出你把这些叫做:
setCatList:(NSMutableArray*)catList;
setDogList:(NSMutableArray*)dogList;
您使用self.catList和self.dogList,但它们都不是合成的,而是合成了beatList和meList
将synthesis更改为catList和dogList,并确保调用set list方法,然后应该取得一些进展。简单的答案是,这里没有任何代码调用方法来设置数组或字典 但真正的根本问题是,这里存在两种糟糕的“模式”,您应该加以纠正: 在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;
}
在大多数情况下,前者更好,但您可能有充分的理由为类的所有实例预填充模型。简单的答案是,这里没有任何代码调用方法来设置数组或字典 但真正的根本问题是,这里存在两种糟糕的“模式”,您应该加以纠正: 在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;
}
前者在大多数情况下更好,但您可能有充分的理由为类的所有实例预填充模型。假设您之前调用了
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
}
假设您之前调用了
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
}
那么,在这种情况下,是否没有办法将模型数据和控制器分开?我在想,我不应该为了坚持MVC设计而将列表放在视图控制器中。这是没有必要的吗?感谢您的回复。您可以为此模型类提供一个初始值设定项来设置这些值。我也会把我的答案附在这个例子后面。非常感谢!我是新手,因此非常感谢您的帮助。还有一件事——如果列表没有更改——即它在所有情况下都设置为这些值,您通常希望它是NSArray,而不是NSMutableArray。习惯对不变的阵列使用NSArray。这将使你的代码更安全。非常感谢你的提示!非常感谢您抽出时间来帮助我。那么在这种情况下,是否没有办法将模型数据和控制器分开?我在想,我不应该为了坚持MVC设计而将列表放在视图控制器中。这是没有必要的吗?感谢您的回复。您可以为此模型类提供一个初始值设定项来设置这些值。我也会把我的答案附在这个例子后面。非常感谢!我是新手,因此非常感谢您的帮助。还有一件事——如果列表没有更改——即它在所有情况下都设置为这些值,您通常希望它是NSArray,而不是NSMutableArray。习惯对不变的阵列使用NSArray。这将使你的代码更安全。非常感谢你的提示!我感谢你抽出时间来帮忙。