小编man*_*112的帖子

如何忽略可编码中的 nil json 值

我目前收到了收据Auto-RenewableNon-Renewable。但是Non-Renewable没有返回expires_datejson 密钥。我怎么能忽略这一点。我试图避免做expires_date一个可选的。当我将其设为可选时,Apple 会返回响应。有没有办法可以在不进行expires_date可选的情况下解码 json 。

struct Receipt: Codable {

    let expiresDate: String

    private enum CodingKeys: String, CodingKey {
        case expiresDate = "expires_date"
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我可以得到

“没有与键 CodingKeys(stringValue: \"expires_date\", intValue: nil) (\"expires_date\") 关联的值。”

json ios swift codable

3
推荐指数
1
解决办法
957
查看次数

如何在Swift中的完成处理程序中返回布尔值

我正在尝试重构我的代码,并想在中返回Bool一个closure。当我尝试它时,它说它是未使用的并且不起作用。我可以用另一种方式来做,但是我要重复不想做的代码。我该怎么办。

func tableView(_ pTableView: UITableView, canEditRowAt pIndexPath: IndexPath) -> Bool {

    // These lines are the one that work but would like to get rid of them
    if let rowConversation = self.objectAtIndexPath(pIndexPath) as? Conversation {
        if rowConversation.isGroupChat && rowConversation.expired  {
            return true
        }
    }

    self.getRowConversation(pIndexPath: pIndexPath) {
        // how to return true here
    }
    return false
}

private func getRowConversation(pIndexPath: IndexPath, completion pCompletion: () -> Void) {
    if let rowConversation = self.objectAtIndexPath(pIndexPath) as? Conversation {
        if …
Run Code Online (Sandbox Code Playgroud)

closures ios completionhandler swift

0
推荐指数
1
解决办法
352
查看次数

标签 统计

ios ×2

swift ×2

closures ×1

codable ×1

completionhandler ×1

json ×1