如何将来自同一数据数组的不同JSON对象解析为多个UILabel?斯威夫特4

时间:2019-03-24 05:26:36

标签: arrays json swift4

我有一个“通知”视图,其中包含UITextViews,每个视图都需要显示与JSON数组不同的对象。

我已经厌倦了使用许多不同的搜索词查找此问题,但到目前为止,对我来说,什么都没有解决。编译器要么向我抛出错误,要么UITextViews仅显示数组中的同一对象。

struct notifications: Decodable {
        let count: Int
        let results: [results]?
    }
    struct results: Decodable {
        let area: String
        let post: [post]
    }
    struct post: Decodable {
        let text:String
    }

这些是我用来解析此JSON数据的结构;

{
    "count": 7,
    "next": null,
    "previous": null,
    "results": [
        {
            "id": "4188673013",
            "author": {
                "user": 1,
                "name": "admin",
                "avatar": null,
                "bio": "The sampledata superuser.\nLogin: `admin:password123`",
                "banned": false
            },
            "anonym": false,
            "subscribed": true,
            "created": "2019-02-27T05:18:15.807202Z",
            "active": true,
            "text": "testing anonym",
            "image": null,
            "additional_images": [],
            "comments": []
        },
        {
            "id": "5822615812",
            "author": {
                "user": 1,
                "name": "admin",
                "avatar": null,
                "bio": "The sampledata superuser.\nLogin: `admin:password123`",
                "banned": false
            },
            "anonym": false,
            "subscribed": true,
            "created": "2019-02-23T23:21:15.610355Z",
            "active": true,
            "text": "testing anonym",
            "image": null,
            "additional_images": [],
            "comments": []
        },
        {
            "id": "6398321511",
            "author": {
                "user": 1,
                "name": "admin",
                "avatar": null,
                "bio": "The sampledata superuser.\nLogin: `admin:password123`",
                "banned": false
            },
            "anonym": false,
            "subscribed": true,
            "created": "2019-02-23T22:31:16.853392Z",
            "active": true,
            "text": "testing anonym",
            "image": null,
            "additional_images": [],
            "comments": []
        },
        {
            "id": "3377948310",
            "author": null,
            "anonym": true,
            "subscribed": true,
            "created": "2019-02-23T02:35:29.093044Z",
            "active": true,
            "text": "testing anonym",
            "image": null,
            "additional_images": [],
            "comments": []
        },
        {
            "id": "401558607",
            "author": {
                "user": 1,
                "name": "admin",
                "avatar": null,
                "bio": "The sampledata superuser.\nLogin: `admin:password123`",
                "banned": false
            },
            "anonym": false,
            "subscribed": true,
            "created": "2019-02-22T03:24:39.827535Z",
            "active": true,
            "text": "This is a test from the iOS app. Just making sure the severs can read this!",
            "image": null,
            "additional_images": [],
            "comments": []
        },
        {
            "id": "293944243",
            "author": null,
            "anonym": true,
            "subscribed": true,
            "created": "2019-02-20T23:54:44.394823Z",
            "active": true,
            "text": "Example anon post by admin user.",
            "image": "http://localhost:8000/media/images/316543563559387320642618685485080866736.png",
            "additional_images": [],
            "comments": [
                {
                    "id": 5,
                    "author": {
                        "user": 1,
                        "name": "admin",
                        "avatar": null,
                        "bio": "The sampledata superuser.\nLogin: `admin:password123`",
                        "banned": false
                    },
                    "created": "2019-02-20T23:54:44.403393Z",
                    "text": "Hey, who are you to claim that you are me.",
                    "image": null
                },
                {
                    "id": 6,
                    "author": {
                        "user": 2,
                        "name": "user",
                        "avatar": null,
                        "bio": "",
                        "banned": false
                    },
                    "created": "2019-02-20T23:54:44.408061Z",
                    "text": "It would be funny, if it were actually you.",
                    "image": null
                }
            ]
        },
        {
            "id": "603485211",
            "author": {
                "user": 1,
                "name": "admin",
                "avatar": null,
                "bio": "The sampledata superuser.\nLogin: `admin:password123`",
                "banned": false
            },
            "anonym": false,
            "subscribed": true,
            "created": "2019-02-20T23:54:44.322104Z",
            "active": true,
            "text": "Post by admin\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit.",
            "image": null,
            "additional_images": [],
            "comments": [
                {
                    "id": 1,
                    "author": {
                        "user": 1,
                        "name": "admin",
                        "avatar": null,
                        "bio": "The sampledata superuser.\nLogin: `admin:password123`",
                        "banned": false
                    },
                    "created": "2019-02-20T23:54:44.335768Z",
                    "text": "First comment, by admin.\n\nPhasellus fringilla odio vitae nibh aliquet facilisis.",
                    "image": null
                },
                {
                    "id": 2,
                    "author": {
                        "user": 2,
                        "name": "user",
                        "avatar": null,
                        "bio": "",
                        "banned": false
                    },
                    "created": "2019-02-20T23:54:44.341323Z",
                    "text": "Another comment.\n\nNunc pellentesque urna eget ipsum vestibulum luctus. Praesent fermentum purus at pellentesque molestie.",
                    "image": null
                }
            ]
        }
    ]
}

我正在解析数据;

guard let data = data else {return}
            do{
            let decoder = JSONDecoder()
            decoder.keyDecodingStrategy = .convertFromSnakeCase
            let notificationsDataArray = try decoder.decode(notifications.self, from: data)
                print(notificationsDataArray.results)
                for results in notificationsDataArray.results!{
                    for posts in results.post{
                        self.Recent1.text = posts.text 
                    }
                }
            }catch{
                print(error)
            }

我总共有4个UITextViews。

    @IBOutlet weak var Recent1: UILabel!
    @IBOutlet weak var Recent2: UILabel!
    @IBOutlet weak var Recent3: UILabel!
    @IBOutlet weak var Recent4: UILabel!

我只想显示数组中的第四个对象。我也希望每个UITextView显示一个不同的对象。最好的方法是什么?

值得注意的是,我是一个快速编程的初学者。

1 个答案:

答案 0 :(得分:0)

首先请遵守命名约定,变量名称以小写字母开头,而结构和类以大写字母开头。

struct Notifications : Decodable {
    let count: Int
    let results: [Post]
}

struct Post : Decodable {
    let id: String
    let author: Author?
    let anonym: Bool
    let subscribed: Bool
    let created: String
    let active: Bool
    let text: String
    let image: URL?
    let comments: [Comment]
}

struct Comment : Decodable { ...

struct Author : Decodable { ...

...

@IBOutlet weak var recent1: UILabel!
@IBOutlet weak var recent2: UILabel!
@IBOutlet weak var recent3: UILabel!
@IBOutlet weak var recent4: UILabel!

将标签放入数组

let labelArray = [recent1, recent2, recent3, recent4]

在循环中,如果所有4个标签均带有文本(或可用帖子少于4个),则使用计数器退出循环。

guard let data = data else { return }
do {
    let decoder = JSONDecoder()
    decoder.keyDecodingStrategy = .convertFromSnakeCase
    let notifications = try decoder.decode(Notifications.self, from: data)
    print(notifications.results)
    var counter = 0
    for post in notifications.results {
        labelArray[counter].text = post.text
        if counter == 3 { break }
        counter += 1
    }
} catch {
   print(error)
}