NSMutableArray addObject: -[__NSArrayI addObject:]: unrecognized selector sent to instance

后端 未结 9 1082
太阳男子
太阳男子 2020-11-30 21:28

I have tried to initialize my NSMutableArray 100 ways from Sunday, and NOTHING is working for me. I tried setting it equal to a newly allocated and initialized NSMutableArra

9条回答
  •  余生分开走
    2020-11-30 22:00

    Have some indexes (into a data array elsewhere) and wanted to have them in numerical order (for a good reason). Crashing until added mutableCopy at the end. Totally puzzled, until I recalled that using Objective-C literal @[] returns a non-mutable array.

    NSMutableArray *a = [@[@(self.indexA), @(self.indexB)] mutableCopy];
    NSLog(@"%@", a);
    [indexArray sortUsingComparator: ^(NSNumber *obj1, NSNumber *obj2) {
        return [obj1 compare:obj2];
    }];
    NSLog(@"%@", a);
    

    Thanx, Zak!

提交回复
热议问题