使用 swift 在领域中添加 json 的数组(其中包含内部数组)

Add array of json (which contain inner array) in realm using swift

我创建了 API 生成 JSON,JSON 看起来像这样

[
  {
    "id": 105,
    "date": "2018-09-06T22:37:57",
    "date_gmt": "2018-09-06T17:07:57",
    "guid": {
      "rendered": "http://wh2.6ae.myftpupload.com/?p=105"
    },
    "modified": "2018-09-06T22:37:57",
    "modified_gmt": "2018-09-06T17:07:57",
    "slug": "video-post-20",
    "status": "publish",
    "type": "post",
    "link": "http://wh2.6ae.myftpupload.com/video-post-20/",
    "title": {
      "rendered": "Video post – 20"
    },
    "content": {
      "rendered": "",
      "protected": false
    },
    "excerpt": {
      "rendered": "",
      "protected": false
    },
    "author": 1,
    "featured_media": 0,
    "comment_status": "open",
    "ping_status": "open",
    "sticky": false,
    "template": "",
    "format": "video",
    "meta": [

    ],
    "categories": [
      5
    ],
    "tags": [

    ],
    "fimg_url": false,
    "acf": {
      "imagesCarousel": false,
      "youtube_video_id": "mwzExNYs12Y",
      "upload_video": false
    },
    "_links": {
      "self": [
        {
          "href": "http://wh2.6ae.myftpupload.com/wp-json/wp/v2/posts/105"
        }
      ],
      "collection": [
        {
          "href": "http://wh2.6ae.myftpupload.com/wp-json/wp/v2/posts"
        }
      ],
      "about": [
        {
          "href": "http://wh2.6ae.myftpupload.com/wp-json/wp/v2/types/post"
        }
      ],
      "author": [
        {
          "embeddable": true,
          "href": "http://wh2.6ae.myftpupload.com/wp-json/wp/v2/users/1"
        }
      ],
      "replies": [
        {
          "embeddable": true,
          "href": "http://wh2.6ae.myftpupload.com/wp-json/wp/v2/comments?post=105"
        }
      ],
      "version-history": [
        {
          "count": 1,
          "href": "http://wh2.6ae.myftpupload.com/wp-json/wp/v2/posts/105/revisions"
        }
      ],
      "predecessor-version": [
        {
          "id": 111,
          "href": "http://wh2.6ae.myftpupload.com/wp-json/wp/v2/posts/105/revisions/111"
        }
      ],
      "wp:attachment": [
        {
          "href": "http://wh2.6ae.myftpupload.com/wp-json/wp/v2/media?parent=105"
        }
      ],
      "wp:term": [
        {
          "taxonomy": "category",
          "embeddable": true,
          "href": "http://wh2.6ae.myftpupload.com/wp-json/wp/v2/categories?post=105"
        },
        {
          "taxonomy": "post_tag",
          "embeddable": true,
          "href": "http://wh2.6ae.myftpupload.com/wp-json/wp/v2/tags?post=105"
        }
      ],
      "curies": [
        {
          "name": "wp",
          "href": "https://api.w.org/{rel}",
          "templated": true
        }
      ]
    }
  }
]

我为此

创建了 类

还有我的swift代码

if let allData = response.result.value as? NSArray{
    let realm = try! Realm()
    for selectedData in allData{
        print(selectedData)

        let jsonData = try? JSONSerialization.data(withJSONObject: selectedData, options: [])

        let myString = String(data: jsonData!, encoding: String.Encoding.utf8)
        print(myString)

        // Insert from Data containing JSON
        try! realm.write {

            let json = try! JSONSerialization.jsonObject(with: data!, options: [])
            realm.create(Post.self, value: json, update: true)
            //realm.add(testData.self, value: json, update: true)
        }
    }
}

这是给我这种类型的错误

[__NSCFDictionary longLongValue]: unrecognized selector sent to
 instance 0x7fdcc8785820 2016-07-06 10:25:30.090
 mydrawing[9436:2732447] *** Terminating app due to uncaught exception
 'NSInvalidArgumentException', reason: '-[__NSCFDictionary
 longLongValue]: unrecognized selector sent to instance 0x7fdcc8785820'

但是如果我从 json 数据中删除 [ 和 ] 那么它工作正常

let data = "{\"id\":1088,\"date\":\"2018-09-06T22:37:57\",\"date_gmt\":\"2018-09-06T17:07:57\",\"guid\":{\"rendered\":\"http:\/\/wh2.6ae.myftpupload.com\/?p=105\"},\"modified\":\"2018-09-06T22:37:57\",\"modified_gmt\":\"2018-09-06T17:07:57\",\"slug\":\"video-post-20\",\"status\":\"publish\",\"type\":\"post\",\"link\":\"http:\/\/wh2.6ae.myftpupload.com\/video-post-20\/\",\"title\":{\"rendered\":\"Video post – 20\"},\"content\":{\"rendered\":\"\",\"protected\":false},\"excerpt\":{\"rendered\":\"\",\"protected\":false},\"author\":1,\"featured_media\":0,\"comment_status\":\"open\",\"ping_status\":\"open\",\"sticky\":false,\"template\":\"\",\"format\":\"video\",\"meta\":\"m\",\"categories\":5,\"tags\":\"m\",\"fimg_url\":false,\"_links\":{\"self\":{\"href\":\"http:\/\/wh2.6ae.myftpupload.com\/wp-json\/wp\/v2\/posts\/105\"},\"collection\":{\"href\":\"http:\/\/wh2.6ae.myftpupload.com\/wp-json\/wp\/v2\/posts\"},\"about\":{\"href\":\"http:\/\/wh2.6ae.myftpupload.com\/wp-json\/wp\/v2\/types\/post\"},\"author\":{\"embeddable\":true,\"href\":\"http:\/\/wh2.6ae.myftpupload.com\/wp-json\/wp\/v2\/users\/1\"},\"replies\":{\"embeddable\":true,\"href\":\"http:\/\/wh2.6ae.myftpupload.com\/wp-json\/wp\/v2\/comments?post=105\"},\"version-history\":{\"count\":1,\"href\":\"http:\/\/wh2.6ae.myftpupload.com\/wp-json\/wp\/v2\/posts\/105\/revisions\"},\"predecessor-version\":{\"id\":111,\"href\":\"http:\/\/wh2.6ae.myftpupload.com\/wp-json\/wp\/v2\/posts\/105\/revisions\/111\"},\"wp:attachment\":{\"href\":\"http:\/\/wh2.6ae.myftpupload.com\/wp-json\/wp\/v2\/media?parent=105\"},\"wp:term\":{\"taxonomy\":\"category\",\"embeddable\":true,\"href\":\"http:\/\/wh2.6ae.myftpupload.com\/wp-json\/wp\/v2\/categories?post=105\"},\"curies\":{\"name\":\"wp\",\"href\":\"https:\/\/api.w.org\/{rel}\",\"templated\":true}}}".data(using: .utf8)

let realm = try! Realm()

// Insert from Data containing JSON
try! realm.write {

let json = try! JSONSerialization.jsonObject(with: data!, options: [])
realm.create(Post.self, value: json, update: true)
}

https://realm.io/docs/swift/latest/#json

你的代码工作只是尝试替换这一行

  try! realm.write {

            let json = try! JSONSerialization.jsonObject(with: data!, options: [])
            realm.create(Post.self, value: json, update: true)
            //realm.add(testData.self, value: json, update: true)
        }

对此

  try! realm.write {

            let json = try! JSONSerialization.jsonObject(with:  jsonData!, options: [])
            realm.create(Post.self, value: json, update: true)
            //realm.add(testData.self, value: json, update: true)
        }

结果