Error[8]: Undefined offset: 5, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述玩 swift 2.0中的反射我正在尝试键入检查子值. 问题:任何项目的镜像中的子数组的每个元素都不是可选的,但是他的类型可以是可选的……当然,即使值为nil,我也会有子值 也许它不清楚,所以我在这里放一些代码来解释更好. 为方便起见,我在Mirror扩展中定义了一个下标,用于获取具有给定标签的子对象 extension Mirror { public subscript(key: Str 玩 swift 2.0中的反射我正在尝试键入检查子值.

问题:任何项目的镜像中的子数组的每个元素都不是可选的,但是他的类型可以是可选的……当然,即使值为nil,我也会有子值

也许它不清楚,所以我在这里放一些代码来解释更好.

为方便起见,我在Mirror扩展中定义了一个下标,用于获取具有给定标签的子对象

extension Mirror {    public subscript(key: String)->Child?{        var child = children.filter {            var valID = false            if let label = 
class Rule: NSObject,AProtocol {    var hello: String?    var subRule: Rule?}
.label { valID = label == key } return valID }.last if child == nil,let superMirror = superclassMirror() { child = superMirror[key] } return child }}

完美,现在让我说我有这个课程

let aRule = Rule()let mirroredRule = Mirror(reflecting:aRule)if let child = mirroredRule["subRule"] {     //child.value always exists     //i can't do child.value is AProtocol? because child.value is not optional     //child.value is AProtocol of course returns false     //child.dynamicType is Optional(Rule)     if let unwrapped = unwrap(child.value) where unwrapped is AProtocol {         //This of course works only if child.value is not nil         //so the unwrap function returns an unwrapped value         //this is not a definitive solution     }}

好的,现在问题

private func unwrap(subject: Any) -> Any? {    var value: Any?    let mirrored = Mirror(reflecting:subject)    if mirrored.displayStyle != .Optional {        value = subject    } else if let firstChild = mirrored.children.first {        value = firstChild.value    }    return value}

child.value尚未初始化,所以它是nil,我无法使用unwrap函数检查他的类型.我正在编写一个反序列化器,所以我需要检查var是否为nil,因为在字典中将用于反序列化,它可以被定义.

struct ErrorResponse: Codable {    let message: String?    let authorizationException: [String: String]?    let valIDationException: String?    let generalException: String?    var isValID: Bool {        var hasAtLeastOneNonNilErrorValue = false        Mirror(reflecting: self).children.forEach {            if case Optional<Any>.some(_) = [+++].value {                hasAtLeastOneNonNilErrorValue = true            }        }        return hasAtLeastOneNonNilErrorValue    }}let errorTest = ErrorResponse(message: "some message",authorizationException: nil,valIDationException: nil,generalException: nil)let errorTest2 = ErrorResponse(message: nil,generalException: nil)print("is valID: \(errorTest.isValID)")  //is valID: trueprint("is valID: \(errorTest2.isValID)") //is valID: false

我希望问题很清楚.有什么建议?

解决方法 基于 this answer,我建议使用if case Optional< Any> .some(_).

我最近做了一些事情,以确保我的结构上至少有一个可选集.你可以粘贴到游乐场:

[+++] 总结

以上是内存溢出为你收集整理的反思和类型检查选项全部内容,希望文章能够帮你解决反思和类型检查选项所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 6, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述玩 swift 2.0中的反射我正在尝试键入检查子值. 问题:任何项目的镜像中的子数组的每个元素都不是可选的,但是他的类型可以是可选的……当然,即使值为nil,我也会有子值 也许它不清楚,所以我在这里放一些代码来解释更好. 为方便起见,我在Mirror扩展中定义了一个下标,用于获取具有给定标签的子对象 extension Mirror { public subscript(key: Str 玩 swift 2.0中的反射我正在尝试键入检查子值.

问题:任何项目的镜像中的子数组的每个元素都不是可选的,但是他的类型可以是可选的……当然,即使值为nil,我也会有子值

也许它不清楚,所以我在这里放一些代码来解释更好.

为方便起见,我在Mirror扩展中定义了一个下标,用于获取具有给定标签的子对象

extension Mirror {    public subscript(key: String)->Child?{        var child = children.filter {            var valID = false            if let label = 
class Rule: NSObject,AProtocol {    var hello: String?    var subRule: Rule?}
.label { valID = label == key } return valID }.last if child == nil,let superMirror = superclassMirror() { child = superMirror[key] } return child }}

完美,现在让我说我有这个课程

let aRule = Rule()let mirroredRule = Mirror(reflecting:aRule)if let child = mirroredRule["subRule"] {     //child.value always exists     //i can't do child.value is AProtocol? because child.value is not optional     //child.value is AProtocol of course returns false     //child.dynamicType is Optional(Rule)     if let unwrapped = unwrap(child.value) where unwrapped is AProtocol {         //This of course works only if child.value is not nil         //so the unwrap function returns an unwrapped value         //this is not a definitive solution     }}

好的,现在问题

private func unwrap(subject: Any) -> Any? {    var value: Any?    let mirrored = Mirror(reflecting:subject)    if mirrored.displayStyle != .Optional {        value = subject    } else if let firstChild = mirrored.children.first {        value = firstChild.value    }    return value}

child.value尚未初始化,所以它是nil,我无法使用unwrap函数检查他的类型.我正在编写一个反序列化器,所以我需要检查var是否为nil,因为在字典中将用于反序列化,它可以被定义.

struct ErrorResponse: Codable {    let message: String?    let authorizationException: [String: String]?    let valIDationException: String?    let generalException: String?    var isValID: Bool {        var hasAtLeastOneNonNilErrorValue = false        Mirror(reflecting: self).children.forEach {            if case Optional<Any>.some(_) = .value {                hasAtLeastOneNonNilErrorValue = true            }        }        return hasAtLeastOneNonNilErrorValue    }}let errorTest = ErrorResponse(message: "some message",authorizationException: nil,valIDationException: nil,generalException: nil)let errorTest2 = ErrorResponse(message: nil,generalException: nil)print("is valID: \(errorTest.isValID)")  //is valID: trueprint("is valID: \(errorTest2.isValID)") //is valID: false

我希望问题很清楚.有什么建议?

解决方法 基于 this answer,我建议使用if case Optional< Any> .some(_).

我最近做了一些事情,以确保我的结构上至少有一个可选集.你可以粘贴到游乐场:

[+++] 总结

以上是内存溢出为你收集整理的反思和类型检查选项全部内容,希望文章能够帮你解决反思和类型检查选项所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
反思和类型检查选项_app_内存溢出

反思和类型检查选项

反思和类型检查选项,第1张

概述玩 swift 2.0中的反射我正在尝试键入检查子值. 问题:任何项目的镜像中的子数组的每个元素都不是可选的,但是他的类型可以是可选的……当然,即使值为nil,我也会有子值 也许它不清楚,所以我在这里放一些代码来解释更好. 为方便起见,我在Mirror扩展中定义了一个下标,用于获取具有给定标签的子对象 extension Mirror { public subscript(key: Str 玩 swift 2.0中的反射我正在尝试键入检查子值.

问题:任何项目的镜像中的子数组的每个元素都不是可选的,但是他的类型可以是可选的……当然,即使值为nil,我也会有子值

也许它不清楚,所以我在这里放一些代码来解释更好.

为方便起见,我在Mirror扩展中定义了一个下标,用于获取具有给定标签的子对象

extension Mirror {    public subscript(key: String)->Child?{        var child = children.filter {            var valID = false            if let label = 
class Rule: NSObject,AProtocol {    var hello: String?    var subRule: Rule?}
.label { valID = label == key } return valID }.last if child == nil,let superMirror = superclassMirror() { child = superMirror[key] } return child }}

完美,现在让我说我有这个课程

let aRule = Rule()let mirroredRule = Mirror(reflecting:aRule)if let child = mirroredRule["subRule"] {     //child.value always exists     //i can't do child.value is AProtocol? because child.value is not optional     //child.value is AProtocol of course returns false     //child.dynamicType is Optional(Rule)     if let unwrapped = unwrap(child.value) where unwrapped is AProtocol {         //This of course works only if child.value is not nil         //so the unwrap function returns an unwrapped value         //this is not a definitive solution     }}

好的,现在问题

private func unwrap(subject: Any) -> Any? {    var value: Any?    let mirrored = Mirror(reflecting:subject)    if mirrored.displayStyle != .Optional {        value = subject    } else if let firstChild = mirrored.children.first {        value = firstChild.value    }    return value}

child.value尚未初始化,所以它是nil,我无法使用unwrap函数检查他的类型.我正在编写一个反序列化器,所以我需要检查var是否为nil,因为在字典中将用于反序列化,它可以被定义.

struct ErrorResponse: Codable {    let message: String?    let authorizationException: [String: String]?    let valIDationException: String?    let generalException: String?    var isValID: Bool {        var hasAtLeastOneNonNilErrorValue = false        Mirror(reflecting: self).children.forEach {            if case Optional<Any>.some(_) = .value {                hasAtLeastOneNonNilErrorValue = true            }        }        return hasAtLeastOneNonNilErrorValue    }}let errorTest = ErrorResponse(message: "some message",authorizationException: nil,valIDationException: nil,generalException: nil)let errorTest2 = ErrorResponse(message: nil,generalException: nil)print("is valID: \(errorTest.isValID)")  //is valID: trueprint("is valID: \(errorTest2.isValID)") //is valID: false

我希望问题很清楚.有什么建议?

解决方法 基于 this answer,我建议使用if case Optional< Any> .some(_).

我最近做了一些事情,以确保我的结构上至少有一个可选集.你可以粘贴到游乐场:

总结

以上是内存溢出为你收集整理的反思和类型检查选项全部内容,希望文章能够帮你解决反思和类型检查选项所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/999913.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存