如何使用Swift 3

时间:2017-01-16 20:39:27

标签: swift3

我得到一个Web API响应,我循环遍历并初始化数组中每个JSON对象的Customer类。 Customer类有一个带有几个可选属性的基类。当JSON中存在空值时,我会收到错误,我不确定如何正确处理它们。我特别不确定如何在swift中处理null Date。如果有人能给我一些建议,我会非常感激!

从Web API返回JSON:

[{"Id":1,"BusinessId":1,"CompanyName":"Test Company4","FirstName":"Mike","LastName":"Doe","CustomerType":1,"CustomerStatus":1,"IsDeleted":false,"Created":"2016-12-22T20:12:10.2760144Z","CreatedBy":1,"Modified":"2016-12-29T19:29:26.1245219Z","ModifiedBy":1},{"Id":2,"BusinessId":1,"CompanyName":"Test Company5","FirstName":"Mike","LastName":"Doe","CustomerType":1,"CustomerStatus":1,"IsDeleted":false,"Created":"2017-01-03T20:18:51.7639708Z","CreatedBy":1,"Modified":null,"ModifiedBy":null}]

客户类:

class Customer : BaseEntity {

    var Id: Int64
    var BusinessId: Int64
    var CustomerType: Int64
    var CustomerStatus: Int64
    var CompanyName: String
    var FirstName: String
    var LastName: String

    init?(json: [String: Any]) {

        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"

        guard let id = json["Id"] as? Int64,
            let businessId = json["BusinessId"] as? Int64,
            let customerType = json["CustomerType"] as? Int64,
            let customerStatus = json["CustomerStatus"] as? Int64,
            let companyName = json["CompanyName"] as? String,
            let firstName = json["FirstName"] as? String,
            let lastName = json["LastName"] as? String,
            let isDeleted = json["IsDeleted"] as? Bool
            else {
                return nil
        }

        var created = Date()
        if let cdate = json["Created"] as? String {
            created = formatter.date(from: cdate)!
        }
        let createdBy = json["CreatedBy"] as? String

        var modified = Date()
        if let mdate = json["Modified"] as? String {
            modified = formatter.date(from: mdate)!
        }
        let modifiedBy = json["ModifiedBy"] as? String

        self.Id = id
        self.BusinessId = businessId
        self.CustomerType = customerType
        self.CustomerStatus = customerStatus
        self.CompanyName = companyName
        self.FirstName = firstName
        self.LastName = lastName
        super.init(isDeleted: isDeleted, created: created, createdBy: createdBy, modified: modified, modifiedBy: modifiedBy)
    }
}

包含一些可选属性的BaseEntity类:

 class BaseEntity {

    var IsDeleted: Bool
    var Created: Date
    var CreatedBy: String
    var Modified: Date?
    var ModifiedBy: String?

    init(isDeleted: Bool, created: Date, createdBy: String, modified: Date, modifiedBy: String) {
        self.IsDeleted = isDeleted
        self.Created = created
        self.CreatedBy = createdBy
        self.Modified = modified
        self.ModifiedBy = modifiedBy
    }
 }

我遇到的一个问题是当我初始化Customer时,我的Customer对象看起来像这样。然后,当我尝试将其插入数据库时​​,它不会将BaseEntity属性识别为Customer的属性:

Id = 1
BusinessId = 1
CustomerType = 1
CustomerStatus = 1
CompanyName = "Test Company4"
FirstName = "Mike"
LastName = "Doe"
BaseEntity
    IsDeleted = true
    Created = '1/1/2017'
    CreatedBy = 1
    Modified = '1/1/2017'
    ModifiedBy = 1

1 个答案:

答案 0 :(得分:1)

您需要为模型

定义类类型
 class BaseEntity {

 class BaseEntity: NSObject {

这是您创建模型类的方法: -

//Model Class
class BaseModel: NSObject {
    var name: String
    var address: String
    var mobilenumber: Int

    init(name: String?, address: String?, mobilenumber: Int?) {
        self.name = name ?? ""
        self.address = address ?? ""
        self.mobilenumber = mobilenumber ?? 0
    }
}

//Pass value inside model class
class ViewController: UIViewController {

    var model = [BaseModel]() //Model Array Initialization here

    override func viewDidLoad() {
        super.viewDidLoad()

        //Pass value inside Model
        model.append(BaseModel(name: "Your name", address: "Your address", mobilenumber: 5545545452))

    }
}

//Get value from Model class
class DetailsController: UIViewController {
    var details: BaseModel?

    override func viewDidLoad() {
        super.viewDidLoad()
       //Retrive value from model
       let name = details?.name ?? ""
       let address = details?.address ?? ""
       let mobile = details?.mobilenumber ?? 0
    }
}