xcode 如何解决 NSInvalidArgumentException 错误?

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

How to solve NSInvalidArgumentException error?

iosobjective-ciphonexcode

提问by user5908520

My app is crashing intermittently for users, but unfortunately I am not able reproduce the problem on any of my devices. The reported error is:

我的应用程序为用户间歇性崩溃,但不幸的是我无法在我的任何设备上重现该问题。报告的错误是:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'NSConcreteAttributedString initWithString:: nil value'

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“NSConcreteAttributedString initWithString:: nil value”

The log trace is not clear about the crash source. I have attached a log trace. I'm looking for any pointers that can help me to track this down.

日志跟踪不清楚崩溃源。我附上了日志跟踪。我正在寻找任何可以帮助我追踪此事的指针。

screenshot of sample exception

示例异常的屏幕截图

回答by CW0007007

The reason for the crash is in your title...

崩溃的原因在你的标题中......

reason: 'NSConcreteAttributedString initWithString:: nil value'

原因:'NSConcreteAttributedString initWithString:: nil 值'

It looks like you are trying to create an attributed string from a string but that string is nil.

看起来您正在尝试从字符串创建属性字符串,但该字符串为零。

Add a guard before you do you NSAttributeString initWithString to make sure the string isn't nil i.e.

在你做之前添加一个守卫 NSAttributeString initWithString 以确保字符串不是 nil 即

if (string) {
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithAttributedString:string];

    //other code etc...
}