배열에 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; // " "
}
정확한 오류 메시지의 정의 –
'NSInvalidArgumentException'이라는 캐치되지 않은 예외로 인해 응용 프로그램을 종료합니다. '- [__ NSArrayI shuffle] : 인스턴스로 전송 된 인식 할 수없는 선택기 0x4b259d0' – Mahir
allRows가 인스턴스화 된 위치를 확인할 수 있습니까? –