2012-06-08 4 views
0

배열에 allRows이라는 가변 배열이 있습니다. 루프에서 각 배열에 배열을 포함하는 allRows을 할당합니다. 그러나 해당 배열에서 shuffle 메서드를 호출하면 프로그램이 충돌합니다. 이 방법은 카테고리이며 다른 배열에서도 작동합니다. 단지 row이 아닙니다.NSMutableArray에서 메서드를 호출하면 "인식 할 수없는 selector가 인스턴스로 전송되었습니다."

- (void)selectQuestions { 

self.quizQuestions = [[NSMutableArray alloc] init]; 

for (int j = 0; j<self.maxRowNumber; j++) { 
    //shuffle each row 
    NSMutableArray *row = [allRows objectAtIndex:j]; 
    [row shuffle]; 

}

@implementation NSMutableArray (shuffle) 

-(void) shuffle { 

NSUInteger count = [self count]; 
for (NSUInteger i = 0; i< count; ++i) { 

    int nElements = count - i; 
    int randomIndex = arc4random() % (nElements) + i ; 
    [self exchangeObjectAtIndex:i withObjectAtIndex:randomIndex]; 
} 
} 

편집

allRows은 클래스의 init 메소드에서 인스턴스화됩니다. 나는 당신이 볼 필요가있는 다른 것을 위해 전체 방법을 추가했다.

- (id)init { 
self = [super init]; 
if (self) { 

    // Question, Reactants, Products, Elements 
    NSArray *R1Q1 = [[NSArray alloc] initWithObjects:@"Methanol is burned completely in air", @"2CH₃OH(l) + 3O₂(g)", @"2CO₂(g) + 4H₂O", @"C,H,O", nil]; 
    NSArray *R1Q2 = [[NSArray alloc] initWithObjects:@"Ammonia is burned in excess oxygen gas", @"4NH₃(g) + 7H₂O(l)", @"4NO₂(g) + 6H₂O(l)", @"N,H,O", nil]; 
    NSArray *R1Q3 = [[NSArray alloc] initWithObjects:@"Hydrogen sulfide gas is burned in excess oxygen gas", @"2H₂S(g) + 3O₂(g)", @"CO₂(g) + 2SO₂(g)", @"H,S,O", nil]; 

    NSArray *R2Q1 = [[NSArray alloc] initWithObjects:@"Solid potassium is added to a flask of oxygen gas", @"K(s) + O₂(g)", @"KO₂(s)", @"K,O", nil]; 
    NSArray *R2Q2 = [[NSArray alloc] initWithObjects:@"Sodium metal is dropped into a flask of pure water", @"2Na(s) + H₂O(l)", @"2Na⁺(aq) + 2OH⁻(aq) + H₂(g)", @"Na,H,O", nil]; 
    NSArray *R2Q3 = [[NSArray alloc] initWithObjects:@"A piece of lithium is heated strongly in oxygen", @"4Li(s) + O₂(g)", @"2Li₂O(s)", @"Li,O", nil]; 

    NSArray *R3Q1 = [[NSArray alloc] initWithObjects:@"Solutions of potassium chloride and silver nitrate are mixed", @"Ag⁺(aq) + Cl⁻(aq)", @"AgCl(s)", @"K,Cl,Ag,N,O", nil]; 
    NSArray *R3Q2 = [[NSArray alloc] initWithObjects:@"Solutions of iron(III) nitrate and sodium hydroxide are mixed", @"Fe³⁺(aq) + 3OH⁻(aq)", @"Fe(OH)₃(s)", @"Fe,N,O,Na,H", nil]; 
    NSArray *R3Q3 = [[NSArray alloc] initWithObjects:@"Solutions of nickel iodide and barium hydroxide are mixed", @"Ni²⁺(aq) + 2OH⁻(aq)", @"Ni(OH)₂(s)", @"Ni,I,Ba,OH", nil]; 

    row1 = [[NSArray alloc] initWithObjects:R1Q1, R1Q2, R1Q3, nil]; 
    row2 = [[NSArray alloc] initWithObjects:R2Q1, R2Q2, R2Q3, nil]; 
    row3 = [[NSArray alloc] initWithObjects:R3Q1, R3Q2, R3Q3, nil]; 
    //add rest 

    allRows = [[NSMutableArray alloc] initWithObjects:row1, row2, row3, nil]; 
    self.maxRowNumber = 3; //hypothetical 
    self.questionsPerRow = 2; // " " 
} 
+0

정확한 오류 메시지의 정의 –

+0

'NSInvalidArgumentException'이라는 캐치되지 않은 예외로 인해 응용 프로그램을 종료합니다. '- [__ NSArrayI shuffle] : 인스턴스로 전송 된 인식 할 수없는 선택기 0x4b259d0' – Mahir

+0

allRows가 인스턴스화 된 위치를 확인할 수 있습니까? –

답변

3

오류에 따르면 당신은 [allRows objectAtIndex:j]이있는 NSMutableArray를있는 NSArray를 반환하지 않습니다 것 같습니다 기록했다. 빠른 수정은 이와 같은 것입니다.

NSMutableArray *row = [NSMutableArray arrayWithArray:[allRows objectAtIndex:j]]; 

하지만 그 중 하나를 얻으려면 NSMutableArrays를 넣는 것이 좋습니다.

+1

+1 초기화 코드가 게시물에 추가되었으므로 문제가있는 곳이 명확합니다. 하지만 당신은 업데이트 전에 그것을 발견했습니다! – dasblinkenlight

+0

예, 그게 문제였습니다. 감사합니다 – Mahir

+0

도움이되기를 기쁘게 생각합니다 :) 우리 모두는 때때로 그 실수를합니다. NSMutable *에 대한 섹시한 카테고리를 만든 다음 가서 immutables 롤을 시작하십시오. 해피 코딩. –