ios NSArrayM insertObject:atIndex:]: 对象不能为 nil'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17031736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:52:38  来源:igfitidea点击:

NSArrayM insertObject:atIndex:]: object cannot be nil'

iosobjective-cuiviewuiviewcontrolleruncaught-exception

提问by user2057209

After a lot of search, and tries with some solutions on stackoverflow, i didn't find any answer which could solve my error : I have a UIViewcontroller, who's name is WorldViewController. In this UIViewcontroller, i had init some UIViews. I want to modificate some variables which depends of WorldViewController from some UIViews. And now that i put those lines :

经过大量搜索,并在 stackoverflow 上尝试了一些解决方案,我没有找到任何可以解决我的错误的答案:我有一个 UIViewController,它的名字是 WorldViewController。在这个 UIViewcontroller 中,我初始化了一些 UIView。我想从一些 UIViews 修改一些取决于 WorldViewController 的变量。现在我把这些行:

WorldViewController * World = [[WorldViewController alloc] init];

it gives me :

它给了我:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

So, how to fix it ? Could it comes from the fact that WorldViewController is running ? And so, how to fix it ?

那么,如何修复呢?难道是因为 WorldViewController 正在运行?那么,如何修复它?

Edit : init method of my viewcontroller

编辑:我的视图控制器的 init 方法

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

        World1 = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 480.0)];
        [World1 setBackgroundColor:[UIColor blackColor]];
        [self.view addSubview:World1];
        [World1 release];

        [self.view addSubview:ViewPig];
        [self.view addSubview:level1view];
        [self.view addSubview:level2view];
        [self.view addSubview:LoadView];
        [LoadView AnimPigLoadingWith:PigChargement];
        [LoadView AppearLabelLoading];
        [self.view addSubview:FondPauseGrise];
        [self.view addSubview:FondPause];
        [self.view addSubview:BoutonResume];
        [self.view addSubview:BoutonHome];
        [self performSelector:@selector(Chargement) withObject:nil afterDelay:0.5];
        [self performSelector:@selector(ViewAppears) withObject:nil afterDelay:5.5+2.5];
        [self performSelector:@selector(LabelPrevientDepartWithImage:) withObject:Trois afterDelay:9];
        [self performSelector:@selector(LabelPrevientDepartWithImage:) withObject:Deux afterDelay:10];
        [self performSelector:@selector(LabelPrevientDepartWithImage:) withObject:Un afterDelay:11];
        [self performSelector:@selector(LabelPrevientDepartWithImage:) withObject:Go afterDelay:12];
        [self performSelector:@selector(Lancement) withObject:nil afterDelay:(3)];
        [self performSelector:@selector(GestionMaps) withObject:nil afterDelay:(11+2)];
    }
    return self;
}

Thanks !

谢谢 !

回答by Gordonium

I had a similar problem. My mistake was in not marking the view that I was trying to load as the initial view controller. My guess is that the there was no initial view controller to add to the array and hence the object was nil. I'm not sure how good this is at answering your question but it worked for me.

我有一个类似的问题。我的错误是没有将我试图加载的视图标记为初始视图控制器。我的猜测是没有初始视图控制器可以添加到数组中,因此对象为零。我不确定这在回答您的问题方面有多好,但它对我有用。

enter image description here

enter image description here

回答by Michael Dautermann

You haven't listed enough code in your question to definitely show where you're making an error.

您没有在问题中列出足够的代码来明确显示您在哪里出错。

Look in your code for any lines that say "insertObject:atIndex:". The object you're inserting is apparently nil.

在您的代码中查找任何显示“ insertObject:atIndex:”的行。您插入的对象显然为零。

If you can't find this line, add a symbolic breakpoint on the symbol "[NSArray insertObject:atIndex:]"(click the link to see specific instructions in an answer of a closely related question to yours) and see if you can break on it right before the crash happens.

如果您找不到这一行,请在符号“ [NSArray insertObject:atIndex:]”上添加一个符号断点(单击链接以查看与您的问题密切相关的答案中的具体说明),看看您是否可以在崩溃发生前立即中断.