Wayne Filkins Wayne Filkins - 1 year ago 116
iOS Question

How to append objects to a custom array in Swift

I have an array of strings:

var userIds = [String]()

I need to use those ID's to make an array of objects. This is what I need it to look like, I think the type needs to be [String, 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]

The main problem I am running into here is when I try to use += in a for loop it doesn't let me, also if I try .append that doesn't work either. I think I can change the type of the post object if needed.

Answer Source
var post: Dictionary<String, AnyObject>

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

for userId:String in userIds
    childUpdates["/user-posts/\(userId)"] = post
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download