Swift 2反射错误
Swift 2 Reflection Error
我查看了一个 swift 代码,试图创建一个将对象序列化为 JSON 的反射函数。问题是当我调用初始化器来获取反射时它抛出了一个疯狂的错误
struct Address {
var street: String
}
struct Person {
var name: String = "Dre"
var age: Int = 33
var dutch: Bool = false
var address: Address? = Address(street: "Clark Drive")
}
// Throws an error on the next line
let mirror = Mirror(reflecting: Person)
当我尝试将镜像的值设置为镜像初始化的结果时,出现以下错误:
- Missing argument label 'reflecting:' in call
- Cannot create a single-element tuple with an element label
知道这里可能出了什么问题吗?
您需要传递 Person
的实例而不是 class Person
。例如:
struct Address {
var street: String
}
struct Person {
var name: String = "Dre"
var age: Int = 33
var dutch: Bool = false
var address: Address? = Address(street: "Clark Drive")
}
let person = Person()
let mirror = Mirror(reflecting: person)
print(mirror.displayStyle)
for case let (label?, value) in mirror.children {
print(label, value)
}
打印:
Optional(Struct)
name Dre
age 33
dutch false
address Optional(Address(street: "Clark Drive"))
Mirror for Bookmark
错误:
func jediGreet(name: String, ability: String) -> (farewell: String, mayTheForceBeWithYou: String) {
return ("Good bye, \(name).", " May the \(ability) be with you.")
}
let retValue = jediGreet("old friend", "Force")
println(retValue)
println(retValue.farewell)
println(retValue.mayTheForceBeWithYou)
正确:
func jediGreet(name: String, ability: String) -> (farewell: String, mayTheForceBeWithYou: String) {
return ("Good bye, \(name).", " May the \(ability) be with you.")
}
let retValue = jediGreet (name:"old friend",ability: "Force")
print(retValue)
print(retValue.farewell)
print(retValue.mayTheForceBeWithYou)
我查看了一个 swift 代码,试图创建一个将对象序列化为 JSON 的反射函数。问题是当我调用初始化器来获取反射时它抛出了一个疯狂的错误
struct Address {
var street: String
}
struct Person {
var name: String = "Dre"
var age: Int = 33
var dutch: Bool = false
var address: Address? = Address(street: "Clark Drive")
}
// Throws an error on the next line
let mirror = Mirror(reflecting: Person)
当我尝试将镜像的值设置为镜像初始化的结果时,出现以下错误:
- Missing argument label 'reflecting:' in call
- Cannot create a single-element tuple with an element label
知道这里可能出了什么问题吗?
您需要传递 Person
的实例而不是 class Person
。例如:
struct Address {
var street: String
}
struct Person {
var name: String = "Dre"
var age: Int = 33
var dutch: Bool = false
var address: Address? = Address(street: "Clark Drive")
}
let person = Person()
let mirror = Mirror(reflecting: person)
print(mirror.displayStyle)
for case let (label?, value) in mirror.children {
print(label, value)
}
打印:
Optional(Struct)
name Dre
age 33
dutch false
address Optional(Address(street: "Clark Drive"))
Mirror for Bookmark
错误:
func jediGreet(name: String, ability: String) -> (farewell: String, mayTheForceBeWithYou: String) {
return ("Good bye, \(name).", " May the \(ability) be with you.")
}
let retValue = jediGreet("old friend", "Force")
println(retValue)
println(retValue.farewell)
println(retValue.mayTheForceBeWithYou)
正确:
func jediGreet(name: String, ability: String) -> (farewell: String, mayTheForceBeWithYou: String) {
return ("Good bye, \(name).", " May the \(ability) be with you.")
}
let retValue = jediGreet (name:"old friend",ability: "Force")
print(retValue)
print(retValue.farewell)
print(retValue.mayTheForceBeWithYou)