2016-09-27 42 views
1

對象爲自定義數組有一個字符串數組:我如何追加斯威夫特

var userIds = [String]() 

我需要用這些ID的使對象的數組。這就是我需要它的樣子,我覺得類型必須[字符串,AnyObject]

var post: Dictionary<String, AnyObject> 

let childUpdates = ["/user-posts/\(userIDs[0])": post, 
        "/user-posts/\(userIDs[1])": post, 
        "/user-posts/\(userIDs[2])": post, 
        "/user-posts/\(userIDs[3])": post] 

我遇到這裏的主要問題是,當我嘗試使用一個+ = for循環它不讓我,也如果我嘗試.append不起作用。我想我可以根據需要更改後期對象的類型。

回答

1
var post: Dictionary<String, AnyObject> 

var childUpdates: Dictionary<String, AnyObject> = Dictionary<String, AnyObject>() 

for userId:String in userIds 
{ 
    childUpdates["/user-posts/\(userId)"] = post 
}