2

我试图洗牌一个数组,但我得到一个“没有可见的@interface for 'NSArray' 在最后一个语句中声明了选择器'exchangeObjectAtIndex:withObjectAtIndex:'。

bArray 应该如何声明?

NSArray *bArray;

并且在

 - (void)viewDidLoad
      [self shuffleb];

然后

-(void) shufflb
   {
 bArray = [NSArray arrayWithObjects:
             @"ca",
             @"do",
             @"ba",
             @"tr",
             @"bu",
             @"bl",
             @"bo",
             @"pu",
             nil];

NSInteger count = [bArray count];
for (NSUInteger i = 0; i < count; ++i) {
    // Select a random element between i and end of array to swap with.
    NSInteger nElements = count - i;
    n = (arc4random() % nElements) + i;
    [bArray exchangeObjectAtIndex:i withObjectAtIndex:n];
}
4

2 回答 2

6

exchangeObjectAtIndex是 NSMutableArray 的一个方法。

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html

于 2013-09-08T07:05:42.057 回答
2
  1. 使用现代的 Objective-C(它让你的代码更好读)

  2. 您要使用的方法仅存在于 NSMutableArray 上,因此请创建一个可变数组或 NSArray 的可变副本(如下):

    bArray = [@[@"ca", @"do", @"ba", @"tr", @"bu", @"bl",@"bo", @"pu"] mutableCopy];
    
于 2013-09-08T07:08:21.767 回答