當方法reloadData執行時,我想隨機/隨機化NSMutableArray中的項目順序。我試了下面,但控制檯不斷拋出以下錯誤:iOS - NSMutableArray中對象的隨機/隨機順序
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI exchangeObjectAtIndex:withObjectAtIndex:]: unrecognized selector sent to instance 0x1748b2c60'
任何想法,爲什麼這可能是?我很難過。
ViewController.h
@property (strong, retain) NSMutableArray *neighbourData;
ViewController.m
- (void)reloadData:(id)sender
{
NSMutableDictionary *viewParams = [NSMutableDictionary new];
[viewParams setValue:@"u000" forKey:@"view_name"];
[DIOSView viewGet:viewParams success:^(AFHTTPRequestOperation *operation, id responseObject) {
self.neighbourData = (NSMutableArray *)responseObject;
[self.tableView reloadData];
NSUInteger count = [self.neighbourData count];
for (NSUInteger i = 0; i < count; ++i) {
// Select a random element between i and end of array to swap with.
int nElements = count - i;
int n = (arc4random() % nElements) + i;
[self.neighbourData exchangeObjectAtIndex:i withObjectAtIndex:n];
}
NSLog(@"This is the neighbourdata %@",self.neighbourData);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failure: %@", [error localizedDescription]);
}];
對於OP,這是一個重要的學習點。將變量轉換爲其他類型不會更改基礎對象的類型。它只是迫使編譯器*認爲它是一種不同的類型。如果對象是**不是**可變數組,則需要一個將不可變數組作爲參數的函數('mutableCopy'),並將您帶回包含相同對象的新可變數組。 –