xcode 应用程序终止尝试从对象 [0] 插入 nil 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19373565/
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-09-15 04:06:22  来源:igfitidea点击:

App terminated attempt to insert nil object from objects[0]

iosobjective-cxcodexcode5

提问by Brandon Boynton

This process is sending eMails, and it works fine, but the framework wants the body of the email to be an NSArray. So I try to convert the NSString to a NSDictionary to include with the NSArray here:

这个过程是发送电子邮件,它工作正常,但框架希望电子邮件的正文是一个 NSArray。所以我尝试将 NSString 转换为 NSDictionary 以包含在 NSArray 中:

NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:kSKPSMTPPartContentTypeKey,
    message,kSKPSMTPPartMessageKey,kSKPSMTPPartContentTransferEncodingKey, nil];

then I implement that here:

然后我在这里实现:

smtpTestMessage.parts = [NSArray arrayWithObject:plainPart];

Overall, here is my code:

总的来说,这是我的代码:

NSLog(@"Start eMail");


SKPSMTPMessage *smtpTestMessage = [[SKPSMTPMessage alloc] init];
smtpTestMessage.fromEmail = @"***@gmail.com";
smtpTestMessage.toEmail = @"***@yahoo.com";
smtpTestMessage.relayHost = @"smtp.gmail.com";
smtpTestMessage.requiresAuth = YES;
smtpTestMessage.login = @"***@gmail.com";
smtpTestMessage.pass = @"***";
smtpTestMessage.subject = @"***";
smtpTestMessage.wantsSecure = YES;

smtpTestMessage.delegate = self;

NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:kSKPSMTPPartContentTypeKey,
    message,kSKPSMTPPartMessageKey,kSKPSMTPPartContentTransferEncodingKey, nil];


smtpTestMessage.parts = [NSArray arrayWithObject:plainPart];

[NSString stringWithFormat:@"%@",message];

[smtpTestMessage send];
NSLog(@"sent");

But sadly enough, when I run the app, the eMail gets to the last stage... Then the app terminates saying

但遗憾的是,当我运行该应用程序时,电子邮件会进入最后阶段......然后应用程序终止说

2013-10-15 00:43:50.512 BullyBox[3662:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]'
*** First throw call stack:
(0x2f61ae8b 0x399156c7 0x2f5540cb 0x2f556749 0xc77f3 0x31dd555f 0x31dd54fb 0x31dd54cb 0x31dc10f3 0x31dd4f13 0x31dd4bdd 0x31dcfc09 0x31da4f59 0x31da3747 0x2f5e5f27 0x2f5e53ef 0x2f5e3bdf 0x2f54e541 0x2f54e323 0x342852eb 0x31e051e5 0xc91b5 0x39e0eab7)
libc++abi.dylib: terminating with uncaught exception of type NSException

EDIT

编辑

Now I keep getting the error:

现在我不断收到错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString appendString:]: nil argument'

Any help is greatly appreciated! Thank you for even reading this! xD <3

任何帮助是极大的赞赏!感谢您阅读本文!xD <3

回答by Kirsteins

Make sure plainPartis created successfully and not a nil.

确保plainPart创建成功而不是零。

回答by Jake Vizzoni

It's tough to judge but if I had to guess plainPartis not getting created. Why do I think this? Let's take a look at the error:

很难判断,但如果我不得不猜测plainPart是不是被创造出来。为什么我会这样想?我们来看看错误:

[__NSPlaceholderArray initWithObjects:count:]:

[__NSPlaceholderArray initWithObjects:count:]:

This points to an alloc/init sequence of calls. The only place in the code you shared where you are allocating an array is here:

这指向一个 alloc/init 调用序列。您共享的代码中分配数组的唯一位置在这里:

smtpTestMessage.parts = [NSArray arrayWithObject:plainPart];

smtpTestMessage.parts = [NSArray arrayWithObject:plainPart];

Therefore, if you are trying to create an array with a nil object, plainPart, then it makes sense why you are getting an error about trying to insert a nil object at location 0.

因此,如果您尝试创建一个带有 nil 对象的数组,plainPart,那么您在尝试在位置 0 插入一个 nil 对象时遇到错误是有道理的。

Try setting a breakpoint right at this call in your code. This will help you narrow down where the crash happens.

尝试在代码中的此调用处设置断点。这将帮助您缩小崩溃发生的范围。

回答by Andrew Hoos

I am not sure what you are trying to do here

我不确定你想在这里做什么

 NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:kSKPSMTPPartContentTypeKey,
message,kSKPSMTPPartMessageKey,kSKPSMTPPartContentTransferEncodingKey, nil];

but based on you naming convention you are storing a key in a value location. Also you could switch to the modern objective-C syntax

但根据您的命名约定,您将键存储在值位置。你也可以切换到现代的 Objective-C 语法

 NSDictionary *plainPart =  @{
                                message: kSKPSMTPPartContentTypeKey,
 kSKPSMTPPartContentTransferEncodingKey: kSKPSMTPPartMessageKey
 };

This is equivalent to what you wrote, but more readable. As stated in the other answers it look like there is a problem with the creation of plainPart. Hopefully looking at this should make it easier to debug.

这相当于你写的,但更具可读性。正如其他答案中所述,plainPart 的创建似乎存在问题。希望看看这个应该能让调试更容易。

回答by Hussain Shabbir

Check first this line smtpTestMessage.parts = [NSArray arrayWithObject:plainPart];. Whether it is smtpTestMessageallocated if yes then check any data exist inside this or not

首先检查这一行smtpTestMessage.parts = [NSArray arrayWithObject:plainPart];。是否已smtpTestMessage分配,如果是,则检查其中是否存在任何数据