如何在iOS Swift中实现评论和回复系统 [英] How to implement comments and reply system in iOS swift

查看:50
本文介绍了如何在iOS Swift中实现评论和回复系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在我的应用程序中实现评论和答复系统.我正在从API获取数据,但我不知道如何执行此操作,因为我之前从未做过类似的事情.我知道我必须为这种类型的系统使用两个不同的单元格,但是我不知道如何过滤评论和答复的数据并在各自的单元格中显示它们.

I have to implement comment and reply system in my application. I am getting the data from the API and I don't understand how to do this cause I have not done anything like this before. I know I have to take two different cells for this type of system but I don't Know how to filter the data for comments and replies and show them in their respective cells.

这是我的JSON的方式:

Here is how my JSON:

[
    {
        "firstName": "Mark",
        "lastName": "King",
        "profilePic": "avatar2 - Copy - Copy.jpg",
        "reviewId": 54,
        "userId": 4,
        "serviceProviderId": 0,
        "rating": 0,
        "description": "Thank You...",
        "isDeleted": false,
        "parentReviewId": 53,
        "createdDate": "2018-05-08T12:32:43.033",
        "modifiedDate": "0001-01-01T00:00:00",
        "bookingId": 62,
        "startDate": "2018-05-08T12:29:00",
        "endDate": "2018-05-08T14:29:00",
        "serviceName": "Security",
        "businessId": 0,
        "businessProfilePic": ""
    },
    {
        "firstName": "Satya Indra",
        "lastName": "varada",
        "profilePic": "3354757569.jpg",
        "reviewId": 53,
        "userId": 9,
        "serviceProviderId": 0,
        "rating": 4,
        "description": "nice service...",
        "isDeleted": false,
        "parentReviewId": 0,
        "createdDate": "2018-05-08T12:31:44.313",
        "modifiedDate": "0001-01-01T00:00:00",
        "bookingId": 62,
        "startDate": "2018-05-08T12:29:00",
        "endDate": "2018-05-08T14:29:00",
        "serviceName": "Security",
        "businessId": 0,
        "businessProfilePic": ""
    },
    {
        "firstName": "Mark",
        "lastName": "King",
        "profilePic": "avatar2 - Copy - Copy.jpg",
        "reviewId": 52,
        "userId": 4,
        "serviceProviderId": 0,
        "rating": 0,
        "description": "sorry for the trouble",
        "isDeleted": false,
        "parentReviewId": 9,
        "createdDate": "2018-05-07T11:52:23.81",
        "modifiedDate": "0001-01-01T00:00:00",
        "bookingId": 16,
        "startDate": "2018-04-24T11:30:00",
        "endDate": "2018-04-24T12:30:00",
        "serviceName": "Cleaning",
        "businessId": 2,
        "businessProfilePic": "logo-home.png"
    },
    {
        "firstName": "James",
        "lastName": "Miller",
        "profilePic": "avatar.jpg",
        "reviewId": 9,
        "userId": 7,
        "serviceProviderId": 0,
        "rating": 2,
        "description": "bad",
        "isDeleted": false,
        "parentReviewId": 0,
        "createdDate": "2018-04-24T23:59:06.357",
        "modifiedDate": "0001-01-01T00:00:00",
        "bookingId": 16,
        "startDate": "2018-04-24T11:30:00",
        "endDate": "2018-04-24T12:30:00",
        "serviceName": "Cleaning",
        "businessId": 2,
        "businessProfilePic": "logo-home.png"
    },
    {
        "firstName": "Mark",
        "lastName": "King",
        "profilePic": "avatar2 - Copy - Copy.jpg",
        "reviewId": 2,
        "userId": 4,
        "serviceProviderId": 0,
        "rating": 0,
        "description": "Thank you",
        "isDeleted": false,
        "parentReviewId": 1,
        "createdDate": "2018-04-20T14:57:29.073",
        "modifiedDate": "0001-01-01T00:00:00",
        "bookingId": 2,
        "startDate": "2018-04-25T00:00:00",
        "endDate": "2018-04-26T09:00:00",
        "serviceName": "Security",
        "businessId": 0,
        "businessProfilePic": ""
    },
    {
        "firstName": "James",
        "lastName": "Miller",
        "profilePic": "avatar.jpg",
        "reviewId": 1,
        "userId": 7,
        "serviceProviderId": 0,
        "rating": 4,
        "description": "Great",
        "isDeleted": false,
        "parentReviewId": 0,
        "createdDate": "2018-04-20T14:56:57.34",
        "modifiedDate": "0001-01-01T00:00:00",
        "bookingId": 2,
        "startDate": "2018-04-25T00:00:00",
        "endDate": "2018-04-26T09:00:00",
        "serviceName": "Security",
        "businessId": 0,
        "businessProfilePic": ""
    }
]

在此,所有词典都将评论和评论组合在一个数组中.区别在于,任何评论的bookingId将与回复相同,评论的reviewId将等于回复的parentReviewId.如果已经有一个答复,则应在其相应的注释下方显示该注释,如果没有答复,我们应该可以通过单击注释单元格中的答复"按钮来添加它.这是概念,我真的尝试过,但我做不到.希望你们能帮助我做到这一点..谢谢您的任何提前帮助..

Here all the dictionaries represent comments and reviews coming in a single array. The difference is the bookingId of any comment will be same to reply and reviewId of a comment will be equal to parentReviewId of reply. If a reply is already there it should be shown below it's respective comment if there is no reply we should be able to add it by clicking on the Reply button in comment cell. This is the concept and I really tried for it I can't do it. Hope you guys can help me do it.. Thanks for any help in advance..

推荐答案

API响应的最佳做法应该是,回复应在注释字典中.喜欢:

The best practice of API response should be like, reply should be in the comments dictionary. Like:

{
    "firstName": "James",
    "lastName": "Miller",
    "profilePic": "avatar.jpg",
    "reviewId": 9,
    "userId": 7,
    "serviceProviderId": 0,
    "rating": 2,
    "description": "bad",
    "isDeleted": false,
    "parentReviewId": 0,
    "createdDate": "2018-04-24T23:59:06.357",
    "modifiedDate": "0001-01-01T00:00:00",
    "bookingId": 16,
    "startDate": "2018-04-24T11:30:00",
    "endDate": "2018-04-24T12:30:00",
    "serviceName": "Cleaning",
    "businessId": 2,
    "businessProfilePic": "logo-home.png"
    "replies": [
            {
                "firstName": "Mark",
                "lastName": "King",
                "profilePic": "avatar2 - Copy - Copy.jpg",
                "reviewId": 52,
                "userId": 4,
                "serviceProviderId": 0,
                "rating": 0,
                "description": "sorry for the trouble",
                "isDeleted": false,
                "parentReviewId": 9,
                "createdDate": "2018-05-07T11:52:23.81",
                "modifiedDate": "0001-01-01T00:00:00",
                "bookingId": 16,
                "startDate": "2018-04-24T11:30:00",
                "endDate": "2018-04-24T12:30:00",
                "serviceName": "Cleaning",
                "businessId": 2,
                "businessProfilePic": "logo-home.png"
            },
            {
                "firstName": "Mark",
                "lastName": "King",
                "profilePic": "avatar2 - Copy - Copy.jpg",
                "reviewId": 60,
                "userId": 4,
                "serviceProviderId": 0,
                "rating": 0,
                "description": "sorry for the trouble new",
                "isDeleted": false,
                "parentReviewId": 9,
                "createdDate": "2018-05-07T11:52:23.81",
                "modifiedDate": "0001-01-01T00:00:00",
                "bookingId": 16,
                "startDate": "2018-04-24T11:30:00",
                "endDate": "2018-04-24T12:30:00",
                "serviceName": "Cleaning",
                "businessId": 2,
                "businessProfilePic": "logo-home.png"
            }
        ]
}

但是在当前情况下,您可以检查 parentReviewId .如果为 0 ,则以其他方式回复评论.您可以这样:

But in current condition, you could check for the parentReviewId. If it is 0, then it is reply otherwise a comment. You can this as:

    let responseArray = [[String: Any]]() // This is array fro your question

    // Extract commentArray
    let commentArray = responseArray.filter( { Int(exactly: $0["parentReviewId"] as! NSNumber) == 0  } )
    // Load updatedResponseArray with relpies into it
    var updatedResponseArray = [[String: Any]]()

    for itemDict in commentArray {
        let reviewId = Int(exactly: itemDict["reviewId"] as! NSNumber) ?? 0
        let replies = responseArray.filter( { Int(exactly: $0["parentReviewId"] as! NSNumber) == reviewId  } )

        var dict = itemDict
        dict["replies"] = replies
        updatedResponseArray.append(dict)
    }

    print(updatedResponseArray)

现在,您可以使用此 updatedResponseArray ,字典中的主要详细信息是注释,并且您在 replies 键中具有对该注释的相关答复.

Now, you can use this updatedResponseArray, main detail in dictionary are comments and you have the related replies to that comment in replies key.

这篇关于如何在iOS Swift中实现评论和回复系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆