0

I'm trying to save two dictionary into one array in swift but I get error because they have different type, is a way to just add generic type, here is my code

        var resolved = Dictionary<String,T -> Void>()
        resolved  = [ "fulfill": fulfill]

        var rejectDic = Dictionary<String,NSError -> Void>()
        rejectDic  = [ "reject": reject]

        var test = [resolved,rejectDic]
1
  • Where do you get the generic parameter T? Commented Sep 11, 2015 at 23:57

3 Answers 3

2

A very nice and Swift-y way of doing this is with an enum. It makes a lot of sense if you think about it: You want your test array to contain either type of a dictionary, which is exactly what an enum does. You can declare it like this:

enum AcceptanceDict<T> {
    case Resolved([String : T -> Void])
    case Rejected([String : ErrorType -> Void])
}

And your code would look maybe like this:

class Test<T> {
    func fulfill(t: T) { print("Fulfilled: \(t)") }
    func reject(error: ErrorType) { print("Rejected: \(error)") }

    var test : [AcceptanceDict<T>] = []

    func testSet() {
        let resolved = [ "fulfill" : fulfill ]
        let rejected = [ "reject" : reject ]

        test = [
            .Resolved(resolved),
            .Rejected(rejected)
        ]
    }

    func testGet() {
        for dict in test {
            switch dict {
            case .Resolved(let resolved): break
                // resolved is of type [String : T -> Void]
            case .Rejected(let rejected): break
                // rejected is of type [String : ErrorType -> Void]
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

just opened a playground to create an example of this with an enum, but you beat me to it 1+
@nPn Hehe, I too was constantly checking for someone else to post an answer like this :P
0

Hi I was able to do this more simply way like this

var promiseDic = Dictionary<String,Any>()
promiseDic  = [ "fulfill": fulfill,"reject":reject]
self.request.updateValue(promiseDic, forKey: uuid)

But when I take the function out of the dictionary do I need to cast to something in particular. Here is my code for taking out from dic

var dictionary:Dictionary<String,Any> = self.request[id] as! Dictionary
    if((error?.isEmpty) == nil){
                    let fun = dictionary["fulfill"]
                    fun(true)
                }else{
                    let rej = dictionary["reject"]
                    rej(NSError())
                }

Comments

-1

You can use the Any type. Of course, you will have to cast your dictionaries back to the proper type when you take them out of the array.

var resolved = Dictionary<String,T -> Void>()
resolved  = [ "fulfill": fulfill]

var rejectDic = Dictionary<String,NSError -> Void>()
rejectDic  = [ "reject": reject]

var test: [Any] = [resolved,rejectDic]

Here is an example of casting when taking the dictionaries out of the array.

for dict in test {
    if let resolved = dict as? Dictionary<String,T -> Void> {
        // Do something 
    }
    else if let rejectDic = dict as? Dictionary<String,NSError -> Void> {
        // Do something else   
    }
}

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.