【问题标题】:How to append objects to a custom array in Swift如何在 Swift 中将对象附加到自定义数组
【发布时间】:2016-09-27 17:32:37
【问题描述】:

我有一个字符串数组:

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 也不起作用。如果需要,我想我可以更改帖子对象的类型。

【问题讨论】:

    标签: ios arrays swift for-loop


    【解决方案1】:
    var post: Dictionary<String, AnyObject>
    
    var childUpdates: Dictionary<String, AnyObject> = Dictionary<String, AnyObject>()
    
    for userId:String in userIds
    {
        childUpdates["/user-posts/\(userId)"] = post
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-23
      • 1970-01-01
      • 2019-07-28
      • 2023-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多