使用Swift将对象添加到数组而不重复

时间:2016-10-24 16:00:05

标签: ios arrays swift

我仍然熟悉Swift,此时我遇到了向阵列添加对象的麻烦。 该阵列也不应该有重复。

到目前为止我还有什么 -

用户单击原型单元格时调用的函数。 我正在努力实现 -

  1. 选择按钮(并带有选中/取消选中该项目的复选标记)
  2. 对于每个所选项目,我有两个值 - 布尔状态isActive和所选项目subscriptionID
  3. 当用户选择项目时,我需要将此选项添加为对象,并进一步将其附加到数组中。
  4. 为此,我有subscriptionUpdateData: NSDictionary和我的新空数组subscriptionsArray: [NSDictionary] = []
  5. 完整功能

     func subscriptionCell(cell: SubscriptionCell, didToggleSubscription subscription: Subscriptions) {
    
            var subscriptionsArray: [NSDictionary] = []
            var subscriptionUpdateData: NSDictionary = ["subscriptionID": 0, "isActive": false]
    
            if let matchingSubscription = subscriptionInformation?.filter({ $0.subscriptionID == subscription.subscriptionID }).first {
    
                matchingSubscription.isActive = !(matchingSubscription.isActive!)
    
                let subscriptionStatus = matchingSubscription.isActive
                let subscriptionStatusForId = matchingSubscription.subscriptionID
    
                subscriptionUpdateData = ["subscriptionID": subscriptionStatusForId!, "isActive": subscriptionStatus!]
                tableView.reloadData()
            }
            subscriptionsArray.append(subscriptionUpdateData)
            print("\(subscriptionsArray)")
    
        }
    

    上面发生了什么 - 我能够选择一个项目,将其形成为字典,并将其添加到我的数组中。 :-) 但每当我在项目列表中选择不同的项目时,它就会用新选择的项目替换数组中的现有元素。 : - (

    我正在寻找下面的内容(没有重复项),这是REST端点的输入 -

        [{   "subscriptionID" : 1234, 
            "isActive" : true
        },
        {
            "subscriptionID" : 5678, 
            "isActive" : false
        },
        {
            "subscriptionID" : 3489, 
            "isActive" : true
        }]
    

    有人可以看看我错过的地方吗?或者我是否有更好的方法可以做到这一点?

2 个答案:

答案 0 :(得分:3)

您必须将 subscriptionsArray 声明为全局变量,请尝试以下代码:

    var subscriptionsArray: [NSDictionary] = []
    func subscriptionCell(cell: SubscriptionCell, didToggleSubscription subscription: Subscriptions) {
        var subscriptionUpdateData: NSDictionary = ["subscriptionID": 0, "isActive": false]

        if let matchingSubscription = subscriptionInformation?.filter({ $0.subscriptionID == subscription.subscriptionID }).first {

            matchingSubscription.isActive = !(matchingSubscription.isActive!)

            let subscriptionStatus = matchingSubscription.isActive
            let subscriptionStatusForId = matchingSubscription.subscriptionID

            subscriptionUpdateData = ["subscriptionID": subscriptionStatusForId!, "isActive": subscriptionStatus!]
            tableView.reloadData()
        }
        subscriptionsArray.append(subscriptionUpdateData)
        print("\(subscriptionsArray)")

    }

答案 1 :(得分:1)

您可以使用swift字典来存储这些唯一订阅,方法是使用其ID作为键:


mvn clean install -Dmaven.test.skip

代码示例:

id1: status1
id2: status2
...

现在,当您需要将结果发送到服务器时,只需从字典中创建一个数组:

var statusesDict = [Int: Bool?]()

func subscriptionToggled(subscription: Subscription) {
    if let matchingSubscription = subscriptionInformation?.filter({ $0.subscriptionID == subscription.subscriptionID }).first {
        let status = matchingSubscription.isActive
        let id = matchingSubscription.subscriptionID
        statusesDict[id] = status
    }
    //if you're using the same object from dataSource array, you dont need to look for it by filtering, just use the parameter object
}

您不应该将字典描述发送到服务器。首先,您应该将其序列化为JSON:

func getSubscriptionsArray() -> [[String: Any]] {
    var result = [Dictionary<String, Any>]()
    for (id, status) in statusesDict.enumerated() {
        let subscriptionDict: [String: Any] = [
            "subscriptionID" : id,
            "isActive" : status
        ]
        result.append(subscriptionDict)
    }
    print("\(result)")
    return result
}

漂亮印刷 json:

 let jsonData = try JSONSerialization.data(withJSONObject: array, options: [])
 let string = String(data: jsonData, encoding: String.Encoding.utf8)