kakaopensource / KakaJSON

Fast conversion between JSON and model in Swift.
MIT License
1.16k stars 125 forks source link

release 模式下取不出模型里的 Int类型的值 #32

Closed flyuqian closed 4 years ago

flyuqian commented 4 years ago

模型

struct BookResource: Convertible {
    enum ResourceType {
        case picture
        case pictureFllowRead
        case video
        case audio
        case demo
        case test
        case weike
        case unkown
    }
    let rID: Int = 0
    let rName: String = ""
    let ext: String = ""
    let showNum: Int = 0
    let fileType: Int = 0
    let chapterID: Int = 9991
    let sectionID: Int = 0
    let downloadPath: String = ""
    let ossName: String = ""
    let picOption: Int = 0
    let imgType: Int = 0
    let fundamentalTone: String = ""
    var type: ResourceType {
        switch picOption {
        case 1:
            if imgType == 1 {
                return .pictureFllowRead
            }
            else {
                return .picture
            }
        case 2:
            return .demo
        case 3:
            return .test
        case 4:
            return .video
        case 5:
            return .audio
        case 6:
            return .weike
        default:
            return .unkown
        }
    }
    func kj_modelKey(from property: Property) -> ModelPropertyKey {
        switch property.name {
        case "rID": return "r_id"
        case "rName": return "r_name"
        case "showNum": return "show_num"
        case "chapterID": return "chapter_id"
        case "sectionID": return "section_id"
        case "imgType": return "img_type"
        case "fundamentalTone": return "fundamental_tone"
        default:
            return property.name
        }
    }

}

在这里用的, 我把打印复制到里边了

static func save(bookID: Int, resource: BookResource) {
        dbQueue.inDatabase { db in
            let sql: String = """
            INSERT OR REPLACE INTO t_resource (user_id, book_id, chapter_id, section_id, r_id, r_name, ext, show_num, fileType, downloadPath, ossName, picOption, img_type, fundamental_tone) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
            """
            let resource = resource
            print("resource: \(resource)")
/*            
 resource: BookResource(rID: 984, rName: "situational prompts", ext: "html", showNum: 1, fileType: 2, chapterID: 58, sectionID: 212, downloadPath: "http://114.116.28.196:9003/wap/getSectionContent_984.html", ossName: "getSectionContent_984", picOption: 2, imgType: 0, fundamentalTone: "")
*/

            let resourceID: Int = resource.rID

            print("resource.rID: \(resource.rID)")
            print("resourceID: \(resourceID)")
/*
            resource.rID: 0
resourceID: 0
*/

            let arguments: StatementArguments = [userID, bookID, resource.chapterID, resource.sectionID, resourceID, resource.rName, resource.ext, resource.showNum, resource.fileType, resource.downloadPath, resource.ossName, resource.picOption, resource.imgType, resource.fundamentalTone]

            print("arguments: \(arguments)")
/*
arguments: [100764, 200121, 9991, 0, 0, "situational prompts", "html", 0, 0, "http://114.116.28.196:9003/wap/getSectionContent_984.html", "getSectionContent_984", 0, 0, ""]
*/
            do {
                try db.execute(sql: sql, arguments: arguments)
            } catch {
                debugLog("save a book resource, 操作异常, error: \(error)")
            }
        }
    }

控制台, 可以把值 po 出来 (lldb) po resource.rID 984

(lldb) po resourceID 0

chapterID, 在使用的时候就是默认值 9991 let chapterID: Int = 9991

flyuqian commented 4 years ago

不好意思,重新看了文档,原来 Int类型不能使用 let。