崩溃报告:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_UIConcretePasteboard setString:]: Argument is not an object of type NSString [(null)]'
堆栈跟踪:
0 CoreFoundation 0x00007fff23c7127e __exceptionPreprocess + 350
1 libobjc.A.dylib 0x00007fff513fbb20 objc_exception_throw + 48
2 CoreFoundation 0x00007fff23c710bc +[NSException raise:format:] + 188
3 UIKitCore 0x00007fff47fb21cd -[_UIConcretePasteboard setString:] + 185
4 UIPasteboardwack 0x0000000109a7bc40 $s16UIPasteboardwack14ViewControllerC11viewDidLoadyyF + 816
步骤:
- 创建一个新项目。
- 这样做:
override func viewDidLoad() {
super.viewDidLoad()
UIPasteboard.general.string?.append("We are appending first ")
}
- 退出模拟器(如果它打开(。
- 在模拟的iPhone 8或更早版本上运行该应用程序。
UI板文档
UI板源代码
背景所以我花了一段时间才弄清楚如何重新创建它,因为在某些情况下没有设置字符串属性,但仍然返回Optional("")
例如在使用 iPhone 11 时,将值设置为字符串。
此外,如果字符串返回 nil,则追加方法应该静默失败。 我也不明白setString
UIConcretePasteboard
是如何被召唤的。
在提供完整答案之前,这是我当前的补丁。
import UIKit
extension UIPasteboard {
///This is a safe alternative to UIPasteboard's string setter property.
///While the string property is optional, if you assign the value nil, it will crash.
func safe(set new: String) {
string = new
}
///Since the string property is a computed property, if you attempt to append to it, it won't silently fail when string returns nil, instead it will crash.
func safe(append new: String) {
if !hasStrings {
safe(set: new)
} else {
string?.append(new)
}
}
}
我对目前的答案不满意,因为虽然这是我能想到的最安全的事情,但不了解根本原因,我无法保证这个解决方案。