简体   繁体   English

创建一个swift泛型数组

[英]Create an array of swift generics

I have the following struct 我有以下结构

struct APIResource<T> {
    let urlRequest: NSMutableURLRequest
    let resource: T
    let parse: (NSData) -> T
 }

I also have the following view controller. 我还有以下视图控制器。 It's job it is to ask the network stack to load the resource from the network. 它的工作是要求网络堆栈从网络加载资源。 Once it is done, it will call the build function to build the appropriate view controller and adds it as a child view controller 完成后,它将调用构建函数来构建适当的视图控制器并将其添加为子视图控制器

 final class LoadingVCL: UIViewController {

init<T>(_ resources: APIResource<T>, build: ((T) -> UIViewController)) {
    super.init(nibName: nil, bundle: nil)
    //ask the network stack for the data
    //once data is loaded, I call the build closure 
    //to return a view controller and add it as a childviewcontroller
}

I am struggling to extend this LoadingVC to accept an array of resources and load them before adding the view controller. 我正在努力扩展此LoadingVC以接受一组资源并在添加视图控制器之前加载它们。

I came across this when following this tutorial. 我在阅读教程时遇到了这个问题。

Edit One 编辑一个

For Instance, suppose I have the following resources 对于Instance,假设我有以下资源

let resourceOne = APIResource<Int>(urlRequest: NSMutableURLRequest(), resource: 1, parse: { _ in return 1})
let resourceTwo = APIResource<Double>(urlRequest: NSMutableURLRequest(), resource: 1, parse: { _ in return 1})
let resourceThree = APIResource<String>(urlRequest: NSMutableURLRequest(), resource: "1", parse: { _ in return "1"})

I am trying to figure out a way for the view controller to accept an array of resources with different types. 我试图找出一种方法让视图控制器接受不同类型的资源数组。

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks 谢谢

As soon as an instance of APIResource<T> is created it has an assigned type and is no longer generic. 一旦创建了APIResource<T>的实例,它就具有指定的类型,并且不再是通用的。 APIResource<String> , for example, is of a different type to APIResource<Double> . 例如, APIResource<String>APIResource<Double>类型不同。 To create an array of mixed types you'll need to use a protocol, eg 要创建混合类型的数组,您需要使用协议,例如

protocol APIResourceProtocol{}
struct APIResource<T>:APIResourceProtocol {
    let urlRequest: NSMutableURLRequest
    let resource: T
    let parse: (NSData) -> T
}

let resourceOne = APIResource<Int>(urlRequest: NSMutableURLRequest(), resource: 1, parse: { _ in return 1})
let resourceTwo = APIResource<Double>(urlRequest: NSMutableURLRequest(), resource: 1, parse: { _ in return 1})
let resourceThree = APIResource<String>(urlRequest: NSMutableURLRequest(), resource: "1", parse: { _ in return "1"})

let arr:[APIResourceProtocol] = [resourceOne, resourceTwo, resourceThree]

Of course this probably isn't what you want because you're probably wanting to be able to go through an array and use certain methods. 当然这可能不是你想要的,因为你可能希望能够通过一个数组并使用某些方法。 Unfortunately, protocols are not generic so it will only be non-generic types that you can leverage this type of behaviour with, eg 不幸的是,协议不是通用的,所以它只是非泛型类型,你可以利用这种类型的行为,例如

protocol APIResourceProtocol {
    var urlRequest: NSMutableURLRequest {get set}
}

struct APIResource<T>:APIResourceProtocol {
    var urlRequest: NSMutableURLRequest
    let resource: T
    let parse: (NSData) -> T
}

protocol ResourceType {}

let resourceOne = APIResource<Int>(urlRequest: NSMutableURLRequest(), resource: 1, parse: { _ in return 1})
let resourceTwo = APIResource<Double>(urlRequest: NSMutableURLRequest(), resource: 1, parse: { _ in return 1})
let resourceThree = APIResource<String>(urlRequest: NSMutableURLRequest(), resource: "1", parse: { _ in return "1"})

let arr:[APIResourceProtocol] = [resourceOne, resourceTwo, resourceThree]

arr.map{$0.urlRequest}

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

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