无法从公共可变数组中删除对象(Objective-C)

时间:2014-06-14 19:52:32

标签: ios objective-c xcode nsmutablearray nsarray

理论上,这个程序应该通过点击加载带图像的随机字符串。这工作正常,但我想添加一个从两个数组中删除当前对象的函数,所以我将所有数组和整数公开(对吗?)并为数组创建了可变副本。然后我在底部写了三行,Xcode显示了这一行:

  

没有可见的@interface用于' NSArray'声明选择器   ' removeObjectAtIndex'

这是什么意思?如何解决?

  #import "KotikViewController.h"

@interface KotikViewController (){
    NSArray *movieNames;
    NSArray *movieCovers;
    NSArray *movieNamesMutableCopie;
    NSArray *movieCoversMutableCopie;
    NSInteger yourRandomIndex;
}

@end

@implementation KotikViewController

- (IBAction)showNewMovie:(id)sender {
    movieNames =@[@"Побег из шаушенка", @"Бэтмен:Начало", @"Форрест Гамп"];
    movieCovers= @[@"shawshank.jpg", @"batmanone.jpg", @"forrest.jpg"];
    movieNamesMutableCopie=[movieNames mutableCopy];
    movieCoversMutableCopie=[movieCovers mutableCopy];


    //Setting a randomindex for movie
    yourRandomIndex = arc4random_uniform(movieNamesMutableCopie.count);

    //Getting imageName and movieName at randomIndex
    NSString* movieName= movieNamesMutableCopie[yourRandomIndex];
    NSString* movieCover= movieCoversMutableCopie[yourRandomIndex];

    //Setting  ImageView image and movieName in a label from strings
    [_movieCover setImage:[UIImage imageNamed:movieCover]];
    _movieName.text = movieName;
   ;
}

- (IBAction)alreadyWatched:(id)sender {
    [movieNamesMutableCopie removeObjectAtIndex:yourRandomIndex];
}
@end

1 个答案:

答案 0 :(得分:1)

NSArray不是可变副本。即你不能添加删除项目。您应该使用NSMutableArray代替。

你的私有变量(和public ??)应该像这样改变

NSArray *movieNames;
NSArray *movieCovers;
NSMutableArray *movieNamesMutableCopie;
NSMutableArray *movieCoversMutableCopie;