加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift:将对象数组转换为子类型数组

发布时间:2020-12-14 04:37:33 所属栏目:百科 来源:网络整理
导读:说我有一系列动物,我想将它投射到一系列的猫.在这里,Animal是Cat采用的协议.我喜欢让猫吃的东西:[猫] =动物! [Cat]但这个seg在编译时出错(顺便说一句,我在 Linux Swift 3和Mac Swift 2.2上).我的解决方法是创建一个单独向下转换每个项目并将其添加到新数组
说我有一系列动物,我想将它投射到一系列的猫.在这里,Animal是Cat采用的协议.我喜欢让猫吃的东西:[猫] =动物! [Cat]但这个seg在编译时出错(顺便说一句,我在 Linux Swift 3和Mac Swift 2.2上).我的解决方法是创建一个单独向下转换每个项目并将其添加到新数组的函数(请参阅下面的小示例).它会产生预期的效果,但不像我想的那样干净.

我的问题是:

>这是完全愚蠢的,我只是错过了一个更简单的方法吗?
>如何在下面的函数中传递一个类型作为目标参数,而不是传递实例? (例如,我想传递Cat.self而不是Cat(id:0),但这样做会导致错误,说无法将Cat.Type转换为预期的参数类型Cat)

这是我到目前为止所拥有的:

protocol Animal: CustomStringConvertible 
{
    var species: String {get set}
    var id: Int {get set}
}

extension Animal
{
    var description: String 
    {
        return "(self.species):(self.id)"
    }
}

class Cat: Animal 
{
    var species = "felis catus"
    var id: Int

    init(id: Int)
    {
        self.id = id
    }
}

func convertArray<T,U>(_ array: [T],_ target: U) -> [U]
{
    var newArray = [U]()
    for element in array
    {
        guard let newElement = element as? U else
        {
            print("downcast failed!")
            return []
        }

        newArray.append(newElement)
    }

    return newArray
}

let animals: [Animal] = [Cat(id:1),Cat(id:2),Cat(id:3)]
print(animals)
print(animals.dynamicType)

// ERROR: cannot convert value of type '[Animal]' to specified type '[Cat]'
// let cats: [Cat] = animals

// ERROR: seg fault
// let cats: [Cat] = animals as! [Cat]

let cats: [Cat] = convertArray(animals,Cat(id:0))
print(cats)
print(cats.dynamicType)

解决方法

  1. Am I missing an easier way to do this?

您可以使用map进行转换:

let cats: [Cat] = animals.map { $0 as! Cat }
  1. how can I pass a type as the target parameter in the function below,rather than passing an instance?

首先,您需要删除实例参数:

func convertArray<T,U>(array: [T]) -> [U] {
    var newArray = [U]()
    for element in array {
        guard let newElement = element as? U else {
            print("downcast failed!")
            return []
        }
        newArray.append(newElement)
    }
    return newArray
}

由于您无法显式指定类型参数,因此需要为编译器提供一些信息以推断U的类型.在这种情况下,您需要做的就是将结果分配给Cat数组:

let cats: [Cat] = convertArray(animals)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读