3

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
    }
}
3
  • 6
    what are you going to do with the type once you have it ? Commented May 17, 2016 at 10:53
  • will use it for json to object mapping Commented May 17, 2016 at 11:37
  • 1
    I think you've lost the question in your "clarification"... Commented May 17, 2016 at 15:42

3 Answers 3

3

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"
Sign up to request clarification or add additional context in comments.

4 Comments

You should probably replace Any with T.Type
One doesn't need to - I guess it depends what he wants to do with it afterwards!
True, that's why I said "should probably", not "need" ;) Although I would always go with the most descriptive type available – and T.Type is a lot more descriptive than Any.
Agreed. Changed the answer.
2

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"]

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.