내 모델의 구현 파일에 NSDictionary
배열을 많이 만들려고하지만 코드가 아직 작동하지 않습니다. 개와 고양이 유형의 목록 인 배열을 만들고 DOG
및 CAT
이라는 키를 사용하여 해당 배열을 사전에 추가하려고합니다. 여기 내 코드 :모델 데이터 용 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
이 화면의 레이블에 표시됩니다. 따라서 텍스트는 CAT
또는 DOG
중 하나가됩니다. NSInteger randomKeyIndex = arc4random() % keyCount;
에서 Thread 1:EXC_ARITHMETIC (code=EXC_1386_DIV, subcode=0x0)
및 내 NSArray
도 내 NSDictionary
도 어떤을 가지고 있기 때문에 나는 그것을 받고 있어요 나타납니다 : 내가 말하는 오류 메시지가 시뮬레이터에있는 버튼을 누릅니다 불행하게도
- (IBAction)changeGreeting:(UIButton*)sender {
NSString *chosenKey = [self.dictionary selectKey];
NSString *labelText = [[NSString alloc] initWithFormat:@"%@", chosenKey];
self.label.text = labelText;
}
: 여기에 대한 코드는 그들 내부의 사물.
내 NSArray
및 NSDictionary
이 채워지지 않은 이유는 누구나 알 수 있습니까?
대단히 감사합니다.
좋은 지적 –