Xcode 包含从我的应用程序的用户下载的多个崩溃报告。线程 1 在执行字符串插值时显然崩溃了。所有其他线程仅包含对系统代码的调用。
出现在堆栈跟踪中的
String.appendingPathComponent(_:)
定义如下:
extension String {
func appendingPathComponent(_ pathComponent: String) -> String {
return pathComponent == "" ? self : self == "" || self == "/" ? "\(self)\(pathComponent)" : "\(self)/\(pathComponent)"
}
}
什么可能导致这样的崩溃?
Thread 1 Crashed:
0 CoreFoundation 0x00007ff81566f9df __CFStringEncodeByteStream + 120 (CFStringEncodings.c:692)
1 Foundation 0x00007ff8164c95aa -[NSString(NSStringOtherEncodings) getBytes:maxLength:usedLength:encoding:options:range:remainingRange:] + 204 (NSStringEncodings.m:341)
2 libswiftCore.dylib 0x00007ff822c6c1e0 String.UTF8View._foreignDistance(from:to:) + 208 (StringUTF8View.swift:507)
3 libswiftCore.dylib 0x00007ff822c56715 _StringGuts.append(_:) + 1445 (StringGutsRangeReplaceable.swift:191)
4 MyApp 0x00000001010c3c0f String.appendingPathComponent(_:) + 15 (<compiler-generated>:0)
您可能想尝试更安全的字符串操作方法。只要您正在处理目录..
试试这个
extension String {
func appendingPathComponent(_ pathComponent: String) -> String {
return (
pathComponent == "" ?
self :
URL.init(filePath: s)
.appendingPathComponent(pathComponent)
.path
)
}
}
这还可以处理所有条件,以便您删除嵌套条件运算符的样板代码。