快速反射导致任何不可能的零值(swift reflection causes impossible nil value for any)

编程入门 行业动态 更新时间:2024-10-25 09:39:58
快速反射导致任何不可能的零值(swift reflection causes impossible nil value for any)

我正在尝试使用快速反射来检查对象中的更改,以便我只能将更改的属性发送到服务器。 我的一些属性是可选的。 为了比较这些值,我需要打开它们,但是,当然,你只能打开实际值,而不是nil值。 因此,在比较它们之前,我需要检查其中一个值是否为零。

在我的操场上,我尝试了以下方法:

import UIKit class myClass { var fieldOne:String? var fieldTwo:Int? var fieldThree:Float? } var oneMyClass = myClass() oneMyClass.fieldOne = "blah" oneMyClass.fieldThree = 3.5 var oneOtherClass = myClass() oneOtherClass.fieldOne = "stuff" oneOtherClass.fieldTwo = 3 let aMirror = Mirror(reflecting: oneMyClass) let bMirror = Mirror(reflecting: oneOtherClass) for thing in aMirror.children { for thing2 in bMirror.children { if thing.label! == thing2.label! { print("property: \(thing.label!)") print("before: \(thing.value)") print("after: \(thing2.value)") print("") //let myTest = thing.value == nil ? "nil" : "not nil" } } }

它会生成以下输出:

property: fieldOne before: Optional("blah") after: Optional("stuff") property: fieldTwo before: nil after: Optional(3) property: fieldThree before: Optional(3.5) after: nil

如您所见,预期属性显示为“nil”。 但是,如果取消注释let语句,则会收到错误消息:

playground52.swift:37:38: error: value of type 'Any' (aka 'protocol<>') can never be nil, comparison isn't allowed

然而,我们从输出中知道它是零。 这怎么可能,我能做些什么呢?

I'm trying to use swift reflection to check for changes in objects so I can send only changed properties up to the server. Some of my properties are optional. To compare those values, I need to unwrap them but, of course, you can ONLY unwrap actual values, not nil values. So, I need to check if one of the values is nil before I compare them.

In my playground, I tried the following:

import UIKit class myClass { var fieldOne:String? var fieldTwo:Int? var fieldThree:Float? } var oneMyClass = myClass() oneMyClass.fieldOne = "blah" oneMyClass.fieldThree = 3.5 var oneOtherClass = myClass() oneOtherClass.fieldOne = "stuff" oneOtherClass.fieldTwo = 3 let aMirror = Mirror(reflecting: oneMyClass) let bMirror = Mirror(reflecting: oneOtherClass) for thing in aMirror.children { for thing2 in bMirror.children { if thing.label! == thing2.label! { print("property: \(thing.label!)") print("before: \(thing.value)") print("after: \(thing2.value)") print("") //let myTest = thing.value == nil ? "nil" : "not nil" } } }

And it generates the following output:

property: fieldOne before: Optional("blah") after: Optional("stuff") property: fieldTwo before: nil after: Optional(3) property: fieldThree before: Optional(3.5) after: nil

As you can see, the expected properties are displayed as "nil". However, if you uncomment the let statement, you get an error stating:

playground52.swift:37:38: error: value of type 'Any' (aka 'protocol<>') can never be nil, comparison isn't allowed

And yet, we know from the output that it IS nil. How can this be and what can I do about it?

最满意答案

那看起来像某种bug。 看那个

let x = childMirror.value == nil ? "Nil" : "Not Nil" //dont compile. let y = { (value:Any?) in return value == nil ? "Nil" : "Not Nil" } let z = y(childMirror.value) //compile, but doesn't evaluate.

我猜问题是因为Any可以存储一个Optional,但不能包含一个。 尝试这个:

func getValue(unknownValue:Any) -> Any { let value = Mirror(reflecting: unknownValue) if value.displayStyle != .Optional || value.children.count != 0 { return "Not Nil" } else { return "Nil" } }

Thats look like some sort of bug. Look at that

let x = childMirror.value == nil ? "Nil" : "Not Nil" //dont compile. let y = { (value:Any?) in return value == nil ? "Nil" : "Not Nil" } let z = y(childMirror.value) //compile, but doesn't evaluate.

I guess the problem is because Any can store a Optional, but can't be wrapped around one. Try this:

func getValue(unknownValue:Any) -> Any { let value = Mirror(reflecting: unknownValue) if value.displayStyle != .Optional || value.children.count != 0 { return "Not Nil" } else { return "Nil" } }

更多推荐

本文发布于:2023-08-06 03:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1444592.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不可能   反射   快速   swift   impossible

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!