Kerem
Kerem

Reputation: 43

Swift Array Type

I think I should be more clear, here are my classes:

    import Foundation
    import Alamofire

    public class BaseService<T>  : ServiceRequest {

        public var requestType: Alamofire.Method = .GET
        public var path: String = "/"

        var requireLogin:Bool = false

        var consumer:RequestInformer?


        public func requestSuccess<T>(request: Request, response: T) {
            consumer?.requestSuccess(request, response: response)

        }

        public func requestFailed(request: Request, error: NSError) {
            consumer?.requestFailed(request, error: error)
        }
    }

    extension BaseService where T:Sortable {

        func start() {
            if requireLogin {

            }
//problem is here, T is [CampaignModel] but I need CampaignModel class, not array.            NetworkManager.sharedInstance.sendRequest(self,ResponseListModel<T>.self)
        }
    }



    extension BaseService where T:NSObject {

        func start() {
            if requireLogin {

            }
            NetworkManager.sharedInstance.sendRequest(self,ResponseModel<T>.self)
        }
    }

Then I want to use BaseService like below:

import Alamofire

public class ListCampaignService : BaseService<[CampaignModel]> {

    override init() {
        super.init()
        path = "/xxx/yyy.json"
        requestType = .GET
    }
}

That means ListCampaignService returns CampaignModel array. Bu I have to handle object return type rather than array, something like below

import Alamofire

public class SomeOtherService : BaseService<SomeClass> {

    override init() {
        super.init()
        path = "/aaa/bbb.json"
        requestType = .GET
    }
}

Upvotes: 3

Views: 1427

Answers (3)

Kametrixom
Kametrixom

Reputation: 14973

If you use generics in your function, it's really simple:

func test<T>(array: [T]) {
    // array elements are of type T
    print(T.self) 
}

If you don't use generics (outside functions) then you can do this:

let array = [1, 2, 3]
let type = array.dynamicType.Element.self
print(type)     // Int.Type

If you're coming from the future: Swift 3.0 will use Self instead of dynamicType

Upvotes: 1

Grimxn
Grimxn

Reputation: 22487

Try this as a start:

func getType<T>(array:[T]) -> T.Type {
    return T.self
}

getType(["1", "2", "3"]) // String.Type
getType([1, 2, 3]) // Int.Type
getType([1, "2", 3.0]) // NSObject.Type
getType([[1], [2], [3]]) // Array<Int>.Type
getType([]) // compile time error "Generic parameter 'T' could not be inferred"

Upvotes: 3

Casey Fleser
Casey Fleser

Reputation: 5787

If you're trying to get all the objects of a particular type from a generic Array you could extend Array like this:

extension Array {
    func itemsOfType<T>() -> [T] {
        return self.flatMap { $0 as? T }
    }
}

let x = [1, 2, 3, "foo", "bar", NSDate()]

let ints : [Int] = x.itemsOfType()      // [1, 2, 3]
let altInts = x.itemsOfType() as [Int]  // [1, 2, 3]
let str : [String] = x.itemsOfType()    // ["foo", "bar"]
let dates : [NSDate] = x.itemsOfType()  // ["May 17, 2016, 6:23 AM"]

Upvotes: 2

Related Questions