2012-03-05 3 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이 화면의 레이블에 표시됩니다. 따라서 텍스트는 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; 
} 

: 여기에 대한 코드는 그들 내부의 사물.

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

좋은 지적 –

1

두 가지 고려해야 할

당신이 전화 표시되지 않습니다 : 당신은 self.catList 및 self.dogList를 사용

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

, 그러나 그 중 어느 것도 합성, 대신 당신이 beatList이 및 meList 합성

합성을 catList 및 dogList로 변경하고 set list 메서드를 호출했는지 확인한 다음 진행해야합니다.

3

간단한 대답은 배열이나 사전을 설정하는 메서드를 호출하는 코드가 없다는 것입니다.

하지만 진짜 근본적인 문제는 나쁜 '패턴'은 수정해야한다고 여기에가는 몇이 있다는 것입니다 : 당신의 setter 메소드에서

을 (setCatList :, setDogList :, setStandardDictionary : 당신은 아니에요 해당 속성을 전달 된 값으로 설정하십시오. 예를 들어, catList를 "catList"변수에 전달하도록 설정해야합니다. 당신의 초기화에 이러한 기본 값을 설정할 수 있습니다,

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

다른 방법 :

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

그럼 당신은 보통의 viewDidLoad 같은 뷰 컨트롤러의 방법에 일어나는 "설정"의 일종이 있어야합니다 wordDictionary 클래스 :

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

전자는 대부분의 경우에 더 나은,하지만 당신은 클래스의 모든 인스턴스에 대한 모델을 미리 채울 수있는 좋은 이유가있을 수 있습니다.

+0

이 경우 모델 데이터와 컨트롤러를 별도로 유지할 방법이 없습니까? 나는 MVC 디자인을 고수하기 위해리스트를 뷰 컨트롤러에두면 안된다고 생각했다. 그게 불필요한가요? 응답 해 주셔서 감사합니다. –

+0

이 값을 설정하는이 모델 클래스의 초기화 도구를 제공 할 수 있습니다. 나는 그 답을 나의 예제에 추가 할 것이다. –

+0

고마워요! 나는이 일에 익숙하지 않아 도움을 크게 받으실 수 있습니다. –

관련 문제