简体   繁体   English

快速的对象数组符合协议

[英]Swift array of objects conforms to a protocol

I have a protocol like this: 我有一个这样的协议:

protocol Datable {
    var date: NSDate { get set }
}

struct DEPref: Datable {
   var date: NSDate
}

struct DEPObs: Datable {
   var date: NSDate
}

func fetchDepRefs() -> [DEPRef] {...}
func fetchDepObss() -> [DEPObs] {...}

I would like to create an array of DEPref and DEPObs in order to sort the final list. 我想创建一个DEPrefDEPObs数组,以便对最终列表进行排序。

I have tried many things but the compiler complains. 我尝试了很多事情,但是编译器抱怨。 Example: 例:

var depRefList: Array<Datable> = fetchDepRefs()
var depObsList: Array<Datable> = fetchDepObss()
var allDeps = ...

An error in the first line "Cannot convert value of type [DEPref] to specified type Array" 第一行出现错误“无法将[DEPref]类型的值转换为指定的类型数组”

I'm very new to Swift/iOS programming, but something like this works for me. 我对Swift / iOS编程非常陌生,但是类似的东西对我来说很有效。

func fetchDepRefs() -> Array<Datable> {return Array<Datable>()}

var depRefList = fetchDepRefs()
var testDEPref = DEPref(date: NSDate())
var testDEPref2 = DEPref(date: NSDate())

depRefList.append(testDEPref)
depRefList.append(testDEPref2)

for ref in depRefList {
    print(ref.date)
}

Your functions could also return [Datable] 您的函数也可能返回[Datable]

eg. 例如。

func fetchDepRefs() -> [Datable] {return [Datable]()}

This also works for your scenario 这也适用于您的情况

Edit: 编辑:

Or if you're looking for type safety in your functions, the following code will also work 或者,如果您正在寻找函数中的类型安全性,那么以下代码也将起作用

func fetchDepRefs() -> [DEPref] {return [DEPref]()}
func fetchDepObss() -> [DEPObs] {return [DEPObs]()}

var depRefList = fetchDepRefs()
var depObsList = fetchDepObss()

var allDeps: [Datable] = [Datable]()

In this you can add elements from depRefList and depObsList to allDeps and everything will work fine. 在此方法中,您可以将depRefList和depObsList中的元素添加到allDeps中,一切正常。

Edit again: 再次编辑:

This is a fully working example of what you're trying to achieve. 这是您要实现的目标的完整示例。 In this I'm using the map function to transform the elements of the given arrays to Datable objects 在此,我使用map函数将给定数组的元素转换为Datable对象

protocol Datable {
    var date: NSDate { get set }
}

struct DEPref: Datable {
   var date: NSDate
}

struct DEPObs: Datable {
   var date: NSDate
}

func fetchDepRefs() -> [DEPref] {return [DEPref]()}
func fetchDepObs() -> [DEPObs] {return [DEPObs]()}

var depRefs = fetchDepRefs()
var depObs = fetchDepObs()

var ref1 = DEPref(date: NSDate())
var obs1 = DEPObs(date: NSDate())

depRefs.append(ref1)
depObs.append(obs1)

var allDeps = depRefs.map{$0 as Datable} + depObs.map{$0 as Datable}


for x in allDeps {
    print("I'm in allDeps array: \(x.date)")
}

Are you running Swift 2 on iOS? 您是否在iOS上运行Swift 2? The following runs successfully on Swift 3. 以下代码可以在Swift 3上成功运行。

http://swiftlang.ng.bluemix.net/#/repl/57b13a01133614f70db3347e http://swiftlang.ng.bluemix.net/#/repl/57b13a01133614f70db3347e

protocol Datable {
    var date: NSDate { get set }
}

struct Type1: Datable {
   var date: NSDate
}

struct Type2: Datable {
   var date: NSDate
}

var x : Datable = Type1(date:NSDate())
var y : Datable = Type2(date:NSDate())

var array : [Datable] = [x, y]

var x2 : Datable = Type1(date:NSDate())
var y2 : Datable = Type2(date:NSDate())

array.append(x2)
array.append(y2)

The easiest way is use map : 最简单的方法是使用map

var depRefList: Array<Datable> = fetchDepRefs().map { $0 as Datable }
var depObsList: Array<Datable> = fetchDepObss().map { $0 as Datable }
var allDeps = (depRefList + depObsList)
            .sort { $0.date.timeIntervalSince1970 > $1.date.timeIntervalSince1970 }

since depRefList is [Datable] , and fetchDepRefs() is [DEPref] , Thay are different type, So it's neccecary to transfer [DEPref] to [Datable] , The map function will do this for you. 由于depRefList[Datable] depRefList [Datable] ,而fetchDepRefs()[DEPref] ,Thay是不同的类型,因此有必要将[DEPref]转移到[DEPref] [Datable]map函数将为您完成此操作。 The flowing code: 流动代码:

var depRefList: Array<Datable> = fetchDepRefs().map { $0 as Datable }

is equal to: 等于:

let depRefs: [DEPref] = fetchDepRefs()

var datables = [Datable]()

for depRef in depRefs {
    let datable = depRef as Datable
    datables.append(datable)
}

var depRefList: Array<Datable> = datables

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM