我正在研究将Swift 2.3转换为Swift 3.0;
Swift 2.3代码:
extension ContextDidSaveNotification: CustomDebugStringConvertible {
public var debugDescription: String {
var components = [notification.name]
components.append(managedObjectContext.description)
for (name, set) in [("inserted", insertedObjects), ("updated", updatedObjects), ("deleted", deletedObjects)] {
let all = set.map { $0.objectID.description }.joinWithSeparator(", ")
components.append("\(name): {\(all)}")
}
return components.joinWithSeparator(" ")
}
}
Run Code Online (Sandbox Code Playgroud)
Swift 3.0代码:
extension ContextDidSaveNotification: CustomDebugStringConvertible {
public var debugDescription: String {
var components = [notification.name]
components.append(Notification.Name(rawValue: managedObjectContext.description))
for (name, set) in [("inserted", insertedObjects), ("updated", updatedObjects), ("deleted", deletedObjects)] {
let all = set.map { $0.objectID.description }.joined(separator: ", ")
components.append(Notification.Name(rawValue: "\(name): {\(all)}"))
}
return components.joined(separator: " ")
}
}
Run Code Online (Sandbox Code Playgroud)
但是我得到了一个错误:在Swift 3.0代码中,有误地对成员'joined()'进行了归还。
如何解决这个问题呢?我做了很多研究,但找不到可行的解决方案。
谢谢
joined(separator:)声明为Array<String>,不是Array<Notification.Name>:
// somewhere in standard library
extension Array where Element == String {
public func joined(separator: String = default) -> String
}
Run Code Online (Sandbox Code Playgroud)
正如@vadian指出的,Notification.Name它不等于String,因此您需要先转换数组。这应该工作:
components
.map({ $0.rawValue })
.joined(separator: " ")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4419 次 |
| 最近记录: |