Tôi đã phạm sai lầm trong khi tạo ra một TableView class
, và vô tình giữ tôi @property
như copy
khi tôi định nghĩa nó:Tại sao thuộc tính NSMutableArray (bản sao, nonatomic) tạo NSArrays?
@property (copy, nonatomic) NSMutableArray *words;
tôi khởi tạo mảng "đúng": (lưu ý đây là nỗ lực thứ ba, vì vậy hãy bỏ qua thực tế là tôi không sử dụng mutableCopy và cách khác tốt hơn để làm điều này)
NSArray *fixedWords = @[@"Eeny", @"Meeny", @"Miny", @"Moe", @"Catch", @"A", @"Tiger", @"By", @"His", @"Toe"];
NSMutableArray *mutWords = [[NSMutableArray alloc] initWithArray:fixedWords];
self.words = mutWords;
Tuy nhiên khi tôi sau đó đến để sắp xếp lại mảng, nó đã bị rơi trên dòng removeObjectAtIndex:
id object = [self.words objectAtIndex:fromIndexPath.row];
NSUInteger from = fromIndexPath.row;
NSUInteger to = toIndexPath.row;
[self.words removeObjectAtIndex:from];
Với thông điệp lỗi
unrecognized selector sent to instance
Đã rất nhiều đào bới để tìm ra rằng đây là vì sao có nghĩa là gán kết quả NSMutableArray trong việc tạo ra một tiêu chuẩn (nonmutable) NSArray. Bất cứ ai có thể giải thích lý do tại sao đây là hành vi chính xác?
'@property (sao chép, nonatomic) NSMutableArray * từ;' không sử dụng bản sao, bạn sử dụng shuld giữ lại. –
hoặc mạnh thay vì giữ lại –
bản sao có thể có của [Sử dụng -mutableCopyWithZone: trên lớp tùy chỉnh khiến nó không thay đổi được] (http://stackoverflow.com/questions/14841130/using-mutablecopywithzone-on-custom-class-makes-it-immutable) –