Hugo
Hugo

Reputation: 767

Swift: Want to add Array to an existing Dictionary

Have a JSON object with all conversations, and a JSON object with all messages from a specific conversation. Those JSON objects get parsed and put the values in a Dictionary and then in an Array. So far so good, this works as expected.

func didReceiveConversations(result: JSON) {
    var conversationsArray = [AnyObject]()

    for (key: String, subJson: JSON) in result["Data"] {
        var conversationDictionary = [String: AnyObject]()
        conversationDictionary["DeviceOnline"] = subJson["DeviceOnline"].boolValue
        conversationDictionary["Id"] = subJson["Id"].stringValue

        self.conversationsArray.append(conversationDictionary)
    }
}

conversationsArray is filled as follow:

[{
    Id = "94cc96b5-d063-41a0-ae03-6d1a868836fb";
    DeviceOnline = 1;
}, {
    Id = "4b321903-af6a-405d-ac4f-c0799a7f714a";
    DeviceOnline = 1;
}]

Then I create a messagesArray that contains a Dictionary with the values:

func didReceiveResultSpecificConversation(result: JSON) {
    var messagesArray = [AnyObject]()

    for (key: String, subJson: JSON) in result {
        var messageDictionary = [String: AnyObject]()

        messageDictionary["MessageId"] = subJson["MessageId"].stringValue
        messageDictionary["MessageText"] = subJson["MessageText"].stringValue

        messagesArray.append(messageDictionary)
    }

    // I guess here comes the code to add the messages to the conversationsArray 
    // in the Dictionary under a key: "Messages" for example so I tried this: 
    for conversation in self.conversationsArray {
        if conversation["Id"] as! String == messagesArray[0]["ConversationId"] as! String {
           conversation["Messages"] = messagesArray as [String: AnyObject]
        }
    }
    // This gives me: Cannot assign a value of type '[String: AnyObject]' to 
    // a value of type 'AnyObject?!'


     messagesArray.removeAll(keepCapacity: false)
}

I thought now adding the messagesArray to the conversationsArray (by creating a new key: "Messages", and filling the value with the messagesArray) was pretty straight forward, but I cannot seem to get it to work. I have tried in a for loop going through the conversation, but I keep getting errors with types and stuff.

The final result of the conversationsArray should be something like this:

[{
    Id = "94cc96b5-d063-41a0-ae03-6d1a868836fb";
    DeviceOnline = 1;
    Messages [{
        MessageId = "1"
        MessageText = "Foo"
    },{
        MessageId = "2"
        MessageText = "Bar"
    }]
}, {
    Id = "4b321903-af6a-405d-ac4f-c0799a7f714a";
    DeviceOnline = 1;
    Messages [{
        MessageId = "1"
        MessageText = "Hello"
    },{
        MessageId = "2"
        MessageText = "World"
    }]
}]

Can someone point me in the correct direction, because I must be missing something very simple here. Thanks!

Upvotes: 0

Views: 125

Answers (2)

vadian
vadian

Reputation: 285069

Since you know that all dictionary keys are String, define both arrays with a more specific type as

 [Dictionary<String,AnyObject>]()

Upvotes: 1

afraisse
afraisse

Reputation: 3542

I'm going for the simplest explanation here, but if you get the error :
Cannot assign a value of type '[String: AnyObject]' to a value of type AnyObject

Wouldn't trying to cast your array to AnyObject work?

conversation["Messages"] = messagesArray as AnyObject

Upvotes: 0

Related Questions