如何检查对象是否为集合? (迅速) [英] How do I check if an object is a collection? (Swift)

查看:123
本文介绍了如何检查对象是否为集合? (迅速)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我广泛使用KVC为应用程序的需求构建统一界面。例如,我的一个函数获取一个对象,该对象仅通过字符串键字典进行多次检查。



因此,我需要一种方法来检查关键是集合类型。



我希望能够进行一些协议检查(如C#中的IEnumerable检查是否可以枚举)如果让refCollection = kvcEntity.value(forKey:refListLocalKey)as?如果让refCollection = kvcEntity.value(forKey:refListLocalKey)作为参数:

  AnySequence< CKEntity> {...} 

我也尝试了AnyCollection。



我知道我可以迭代所有主要的集合类型,只需输入:

  if let a = b as?设置{...} //(或者:如果a是Set {...})
如果让a = b as? Array {...}
如果让a = b as?字典{...}

但从继承/多态的角度来看,这似乎不合适。

解决方案

Collection 不能再用于类型检查,因此Ahmad F的解决方案将不再编译。



我做了一些调查。有些人建议连接到obj-c集合并使用 isKindOfClass ,其他人尝试使用反射(通过使用 Mirror ) 。

如果我们关心的是,那么通过分割对象类型来完成任务有一个非常简单直接,数组字典集合(列表可以更新):

  func isCollection< T>(_ object:T) - > Bool {
let collectionsTypes = [Set,Array,Dictionary]
let typeString = String(描述:type(of:object))

in collectionsTypes {
if typeString.contains(type){return true}
}
return false
}

用法:

  var set:Set! = Set< String>()
var dictionary:[String:String]! = [key:value]
var array = [a,b]
var int = 3
isCollection(int)// false
isCollection (set)// true
isCollection(array)// true
isCollection(dictionary)// true

硬代码是缺点,但它可以胜任。


I'm extensively using KVC to build unified interface for the needs of an app. For instance, one of my functions gets an object, which undergoes several checks based solely on dictionary of string keys.

Thus I need a way to check if an object by the key is of collection type.

I expected to have been able to make some protocol check (like IEnumerable in C# to check if it can be enumerated), but it didn't work out:

if let refCollection = kvcEntity.value(forKey: refListLocalKey) as? AnySequence<CKEntity> { ... }

I tried AnyCollection, too.

I know I could iterate all main collection types, by simply typing:

if let a = b as? Set { ...} // (or: if a is Set {...})
if let a = b as? Array { ...}
if let a = b as? Dictionary { ...}

But this doesn't seem proper from inheritance/polymorphism point of view.

解决方案

Collection can no longer be used for type-checking, hence Ahmad F's solution would no longer compile.

I did some investigation. Some people advice to bridge to obj-c collections and use isKindOfClass, others try to employ reflection (by using Mirror). Neither is satisfactory.

There's a pretty straight-forward, a bit rough yet efficient way to accomplish the task via splitting object type if our concern is Array, Dictionary or Set (list can be updated):

func isCollection<T>(_ object: T) -> Bool {
    let collectionsTypes = ["Set", "Array", "Dictionary"]
    let typeString = String(describing: type(of: object))

    for type in collectionsTypes {
        if typeString.contains(type) { return true }
    }
    return false
}

Usage:

var set : Set! = Set<String>()
var dictionary : [String:String]! = ["key" : "value"]
var array = ["a", "b"]
var int = 3
isCollection(int) // false
isCollection(set) // true
isCollection(array) // true
isCollection(dictionary) // true

Hard-code is the drawback but it does the job.

这篇关于如何检查对象是否为集合? (迅速)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆