Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何组合两个SwiftyJSON对象_Ios_Swift_Swifty Json - Fatal编程技术网

Ios 如何组合两个SwiftyJSON对象

Ios 如何组合两个SwiftyJSON对象,ios,swift,swifty-json,Ios,Swift,Swifty Json,我有一个swiftyJSON对象,例如: [{ "location" : "http://...", "img" : "http://...", "commentCount" : 0, "timestamp" : 1432460217550, }] 我希望能够向其附加另一个swiftyJSON对象,使其看起来像: [{ "location" : "http://...", "img" : "http://...", "commentCount" : 0, "ti

我有一个swiftyJSON对象,例如:

[{
  "location" : "http://...",
  "img" : "http://...",
  "commentCount" : 0,
  "timestamp" : 1432460217550,
}]
我希望能够向其附加另一个swiftyJSON对象,使其看起来像:

[{
  "location" : "http://...",
  "img" : "http://...",
  "commentCount" : 0,
  "timestamp" : 1432460217550,
},
{
  "location" : "http://...",
  "img" : "http://...",
  "commentCount" : 1,
  "timestamp" : 1432460217571,
}
]

我不能在swiftyJSON对象上使用
+=
.append
。我该怎么做呢?

正如您所说,swiftyJSON没有附加功能

您可以做的是将swiftyJSON对象解析为anyObject类型的数组并附加它们

let json = JSON(data: data!) 
var JSONObject = JSON(json["content"].arrayObject! + json["content"].arrayObject!)

数据->从HTTP请求接收的NSData对象。

维克托的回答对我不起作用。但我通过将JSON对象
data
放入如下数组解决了这个问题:

var data: [JSON] = []
并使用以下代码:

self.data = self.data + JSON["content"].arrayValue

我喜欢@user2215977的答案,但我还需要合并嵌套的JSON。我扩展了扩展以合并嵌套的JSON和数组,而包含JSON的数组不会合并,而是都在新生成的JSON的数组中

导入快捷JSON

extension JSON {
    mutating func merge(other: JSON) {
        if self.type == other.type {
            switch self.type {
                case .dictionary:
                    for (key, _) in other {
                        self[key].merge(other: other[key])
                    }
                case .array:
                    self = JSON(self.arrayValue + other.arrayValue)
                default:
                    self = other
            }
        } else {
            self = other
        }
    }

    func merged(other: JSON) -> JSON {
        var merged = self
        merged.merge(other: other)
        return merged
    }
}
为了说明其用法,我还将发布此扩展的测试

import XCTest
import SwiftyJSON

class JSONTests: XCTestCase {
    func testPrimitiveType() {
        let A = JSON("a")
        let B = JSON("b")
        XCTAssertEqual(A.merged(other: B), B)
    }

    func testMergeEqual() {
        let json = JSON(["a": "A"])
        XCTAssertEqual(json.merged(other: json), json)
    }

    func testMergeUnequalValues() {
        let A = JSON(["a": "A"])
        let B = JSON(["a": "B"])
        XCTAssertEqual(A.merged(other: B), B)
    }

    func testMergeUnequalKeysAndValues() {
        let A = JSON(["a": "A"])
        let B = JSON(["b": "B"])
        XCTAssertEqual(A.merged(other: B), JSON(["a": "A", "b": "B"]))
    }

    func testMergeFilledAndEmpty() {
        let A = JSON(["a": "A"])
        let B = JSON([:])
        XCTAssertEqual(A.merged(other: B), A)
    }

    func testMergeEmptyAndFilled() {
        let A = JSON([:])
        let B = JSON(["a": "A"])
        XCTAssertEqual(A.merged(other: B), B)
    }

    func testMergeArray() {
        let A = JSON(["a"])
        let B = JSON(["b"])
        XCTAssertEqual(A.merged(other: B), JSON(["a", "b"]))
    }

    func testMergeNestedJSONs() {
        let A = JSON([
            "nested": [
                "A": "a"
            ]
        ])

        let B = JSON([
            "nested": [
                "A": "b"
            ]
        ])

        XCTAssertEqual(A.merged(other: B), B)
    }
}

现在,SwiftyJSON支持这一点

myJson.merged(with: otherJson)
您可以在他们的合并测试中看到这方面的示例


完美的Swift 4。非常感谢。
myJson.merged(with: otherJson)