我有一个字符串数组:
var userIds = [String]()
我需要使用这些 ID 来制作一个对象数组。这就是我需要的样子,我认为类型需要是 [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]
我在这里遇到的主要问题是,当我尝试在 for 循环中使用 += 时它不允许我使用,而且如果我尝试 .append 也不起作用。我想我可以根据需要更改帖子对象的类型。
最佳答案
var post: Dictionary<String, AnyObject>
var childUpdates: Dictionary<String, AnyObject> = Dictionary<String, AnyObject>()
for userId:String in userIds
{
childUpdates["/user-posts/\(userId)"] = post
}
关于ios - 如何在 Swift 中将对象附加到自定义数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39730982/