How to implement comments and reply system in iOS swift

混江龙づ霸主 提交于 2019-12-03 21:41:13

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"
            }
        ]
}

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)

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!