ios - 将 nil 作为参数传递给 stringWithString 时出现异常 :?

标签 ios objective-c exception

我有下面两段代码

在这种情况下,应用程序没有崩溃。

[NSArray arrayWithArray:nil];

但是如果我将 nil 传递给 stringWithString: 应用程序崩溃了。

[NSString stringWithString:nil];

Result uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSPlaceholderString initWithString:] nil argument'

这背后的原因是什么?

最佳答案

如果允许 nil 参数,则 100% 取决于您调用的方法的实现。通常传递 nil 是允许的。但是,如果您依赖传入的非 nil 参数,则可能会引发异常。

Apple/iOS 开发人员决定调用arrayWithArray:nil 将导致一个空数组。
对于 stringWithString:nil,他们决定不允许您传入 nil,因此会引发异常。

为什么他们这样决定,你可能想去苹果开发者论坛问问那里的开发者。

关于ios - 将 nil 作为参数传递给 stringWithString 时出现异常 :?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30951455/

相关文章:

iphone - 任何 iOS block 的标识

asp.net - 构建excel文件使内存异常

c++ - 科学计算人员的异常与断言(我是我的代码的唯一用户)?

c++ - 如何导出从 std::runtime_error 派生的类?

iOS ScrollView 不工作

ios - 我们可以为具有动态高度的 UITableViewCell 使用 XIB 吗?

c++ - 从 Objective C 到 C++ 打字头痛

iphone - 为 UI 导航栏设置提示

ios - 如何在xcode中通过调用栈查找相关代码?

ios - 动态增加 UILabel 和 TableView Cell 的高度?