Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 对于';没有可见的@界面;NSArray';声明选择器';exchangeObjectAtIndex:withObjectAtIndex_Ios_Objective C_Nsarray - Fatal编程技术网

Ios 对于';没有可见的@界面;NSArray';声明选择器';exchangeObjectAtIndex:withObjectAtIndex

Ios 对于';没有可见的@界面;NSArray';声明选择器';exchangeObjectAtIndex:withObjectAtIndex,ios,objective-c,nsarray,Ios,Objective C,Nsarray,我试图洗牌数组,但在最后一条语句中,选择器“exchangeObjectAtIndex:withObjectAtIndex:”声明了“NSArray”的“No visible@interface” bArray应该如何申报 NSArray *bArray; 而且 - (void)viewDidLoad [self shuffleb]; 然后 -(无效)shufflb { bArray=[NSArray arrayWithObjects: @“ca”, @“做”, @“ba”,

我试图洗牌数组,但在最后一条语句中,选择器“exchangeObjectAtIndex:withObjectAtIndex:”声明了“NSArray”的“No visible@interface”

bArray应该如何申报

NSArray *bArray;
而且

 - (void)viewDidLoad
      [self shuffleb];
然后

-(无效)shufflb
{
bArray=[NSArray arrayWithObjects:
@“ca”,
@“做”,
@“ba”,
@“tr”,
@“bu”,
@“基本法”,
@“博”,
@"普",,
零];
NSInteger计数=[bArray计数];
对于(整数i=0;i
交换对象索引
是NSMutableArray的一种方法

  • 使用现代Objective-C(它使您的代码读起来更好)

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


  • 请注意,如果您将代码重写为新语法,您需要删除
    nil
    !只是一个备注。这就是为什么nil不再存在的原因。或者您只是想强调这一事实?
    -(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];
    }
    
    bArray = [@[@"ca", @"do", @"ba", @"tr", @"bu", @"bl",@"bo", @"pu"] mutableCopy];