简体   繁体   中英

How can I solve this problem “Cannot assign to property: 'typ' is a get-only property”

class NotificationModel {
    var id: String?
    var typ: NotificationType {

        switch self.typ {
        case .order(let id):
            return .order(id)
        case .product(let id):
            return .product(id)
        case .manager:
            return .manager
        }
    }

    init(_ info: [AnyHashable: Any]) {
        self.id = info[AnyHashable("id")] as? String
        typ = (info[AnyHashable("type")] as? NotificationType)!
    }
}
enum NotificationType {
    case manager
    case product(String)
    case order(String)
}

maybe, please try with this way:

enum NotificationType: String {
    case manager = "master"
    case product = "product"
    case order = "order"
}

class NotificationModel {
    var id: String?
    var type: NotificationType = .master

    init(_ info: [String: Any]) {
        self.id = info["id"] as? String
        if let strType = info["type"] as? String, let temp = NotificationType(rawValue:strType) {
           self.type = temp
        }       
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM