通用Swift函数来测试对象类型

通用Swift函数来测试对象类型

本文介绍了通用Swift函数来测试对象类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我试图编写一个函数,它将一个对象和一个类型作为参数,并返回一个布尔值,指示对象是否为给定类型。似乎没有Type类型,所以我不知道如何做到这一点。我已经能够做的最好的是 $ b $ pre $ func objectIsType< T>(object:AnyObject,someObjectOfType:T) - > ; Bool { return object是T } 所以我可以做 objectIsType(x,5)来测试 x 是否是 Int 或 objectIsType(x,hi)来查看它是否是字符串,但我希望能够调用 objectIsType(x, Int)来查看 x 是否为 Int 和 objectIsType (x,String)来查看它是否是 String 。是这样的可能吗? 编辑: Airspeed Velocity改进了我的功能,并且很好地说明了已经是了。新功能是这样的: pre $ func objectIsType< T>(object:Any,someObjectOfType:T.Type) - > Bool {返回对象是T } 我试图做的是验证 [String:Any] 字典的值是我所期望的类型。例如: let验证器:[String:Any.Type] = [gimme an int:Int .self,this better be a string:String.self ] let validatee:[String:Any] = [gimme an int :3,这最好是一个字符串:它是! ($) (key,type)in validator { if!objectIsType(validatee [key],type){ selfDestruct()} } 但是我收到错误消息,< > protocol.Type不能转换为T.type 。我已经看过Metatype文档,但我仍然有点困惑。 解决方案如果您想提供一个类型作为参数,而不是一个值,您可以执行以下操作: func objectIsType< T>(object:Any,someObjectOfType:T.Type) - > Bool {返回对象是T } 让a:Any = 1 objectIsType(a,Int.self)//返回true 注意, AnyObject 只能引用类,不是结构或枚举。 Int 和 String 是结构。如果你改变你的代码,就像我上面所说的那样,取一个 Any ,它也可以用于结构。 这可能看起来像你的原始工作没有这个变化,但真正发生了什么是互操作性转换你的 Int 转换为 NSNumber ,这是一种迂回的做事方式,并且不适应基于元类型的方法。 但问题的确是,你为什么认为你需要这个? 是已完成此操作。 I'm trying to write a function that takes an object and a type as arguments and returns a boolean indicating whether or not the object is of the given type. There doesn't seem to be a Type type, so I'm not sure how to do this. The best I've been able to do is func objectIsType<T>(object: AnyObject, someObjectOfType: T) -> Bool { return object is T}So I can do objectIsType(x, 5), to test if x is an Int or objectIsType(x, "hi") to see if it's a string, but I'd like to be able to call objectIsType(x, Int) to see if x is an Int and objectIsType(x, String) to see if it's a String. Is something like this possible?Edit:Airspeed Velocity improved my function and made a great point about it doing exactly what is already does. The new function is this: func objectIsType<T>(object: Any, someObjectOfType: T.Type) -> Bool { return object is T}What I'm trying to do is to validate that the values of a [String: Any] dictionary are of the type that I expect. For instance: let validator: [String: Any.Type] = [ "gimme an int": Int.self, "this better be a string": String.self]let validatee: [String: Any] = [ "gimme an int": 3, "this better be a string": "it is!"]for (key, type) in validator { if !objectIsType(validatee[key], type) { selfDestruct() }}But I'm getting the error, <>protocol.Type is not convertible to T.type. I've looked at the Metatype documentation, but I'm still a bit confused. 解决方案 If you want to supply a type as the argument, not a value, you can do the following:func objectIsType<T>(object: Any, someObjectOfType: T.Type) -> Bool { return object is T}let a: Any = 1objectIsType(a, Int.self) // returns trueNB, AnyObject can only refer to classes, not structs or enums. Int and String are structs. If you change your code, as I have above, to take an Any, it works with structs too.It might have seemed like your original worked without this change, but really what was happening was the interop was converting your Int into an NSNumber which is a bit of a roundabout way of doing things and won't adapt to the metatype-based approach.But the question really is, why do you think you need this? is already does exactly this. 这篇关于通用Swift函数来测试对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-14 04:25