2012-06-08 32 views
0

我有一個可變數組,allRows。在循環中,我將allRows(其中也包含數組)的每個索引分配給另一個數組。但是,在該數組上調用shuffle方法時,程序會崩潰。該方法是一個類別,適用於其他陣列;只是不是row當我在NSMutableArray上調用方法時,出現錯誤「發送給實例的無法識別的選擇器」

- (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]; 
} 
} 

EDIT

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

什麼是確切的錯誤信息? – 2012-06-08 18:09:25

+0

由於未捕獲異常'NSInvalidArgumentException'而終止應用程序,原因:' - [__ NSArrayI shuffle]:無法識別的選擇程序發送到實例0x4b259d0' – Mahir

+0

我們可以看到allRows的實例嗎? –

回答

3

根據錯誤您發佈它看起來像[allRows objectAtIndex:j]將返回一個NSArray不是一個NSMutableArray。快速解決就是這樣的。

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

但它可能更好,你把NSMutableArrays如果你想要得到一個。

+1

+1現在,初始化代碼被添加到帖子中,它清楚問題在哪裏。但是你在更新之前發現了它! – dasblinkenlight

+0

是的,那是問題所在。謝謝 – Mahir

+0

很高興幫助:)我們有時會犯這個錯誤。爲NSMutable *做一個性感的類別,然後去發起一堆不可變的笑聲。快樂編碼。 –

相關問題