应用程序崩溃并出现错误:无法推断出通用参数"T" [英] App Crashing with error: generic parameter 'T' could not be inferred

查看:119
本文介绍了应用程序崩溃并出现错误:无法推断出通用参数"T"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从UserDefault获取可哈希化的自定义对象. 我的自定义模型定义如下:

I'm trying to get custom object which is hashable from UserDefault. My custom model is defined below:

class WorkerProfileResponse: Mappable, Hashable{

    static func == (lhs: WorkerProfileResponse, rhs: WorkerProfileResponse) -> Bool {
        return lhs.id == rhs.id
    } 
    var hashValue: Int{
        return self.id!
    }
    var id, loginStatus, lastLogin, lastActive: Int?
    var username, email, mobileNumber: String?
    var userCategories: [String]?
    var userSubCategories: [String]?
    var biometricToken: String?
    var accessToken: AccessToken?
    var userStatus: UserStatus?
    var userProfile: UserProfile?

    required init(map: Map) {        
    }

    func mapping(map: Map) {
        id <- map["id"]
        loginStatus <- map["is_logged_in"]
        lastLogin <- map["last_login"]
        lastActive <- map["last_active"]
        biometricToken <- map["biometricToken"]
        username <- map["username"]
        email <- map["email"]
        mobileNumber <- map["mobile_number"]
        accessToken <- map["accessToken"]
        userStatus <- map["userStatus"]
        userCategories <- map["userCategories"]
        userSubCategories <- map["userSubCategories"]
        userProfile <- map["userProfile"]
    }
 }

我的userdefault方法是:

My userdefault method is:

class func getModel<T: Hashable>(key: String) -> T {
        let decoded  = UserDefaults.standard.data(forKey: key)
        let decodedModel = NSKeyedUnarchiver.unarchiveObject(with: decoded!) as! T
        return decodedModel
    }

我这样称呼它:

UserDefault.getModel(key: "workerProfile")

当我调用此方法时,应用崩溃了,我不明白原因,错误是:

App is crashing when I'm calling this method I don't understand the reason, error is:

error: generic parameter 'T' could not be inferred

推荐答案

我正在回答我自己的问题,如果将来对任何人有帮助的话. 解码时崩溃了,因为userdefault中没有值.

I'm answering my own question, if it helps anyone in the future. It was crashing while decoding because there was no value present in userdefault.

此行由于强制转换而出现问题:

This line had the issue because of force casting:

let decodedModel = NSKeyedUnarchiver.unarchiveObject(with: decoded!) as! T

我已更改此方法:

class func getModel<T: Hashable>(key: String) -> T {
        let decoded  = UserDefaults.standard.data(forKey: key)
        let decodedModel = NSKeyedUnarchiver.unarchiveObject(with: decoded!) as! T
        return decodedModel
    }

对此:

class func getModel<T: Hashable>(key: String) -> T? {
        let decoded  = UserDefaults.standard.data(forKey: key)
        if decoded != nil{
            let decodedModel = NSKeyedUnarchiver.unarchiveObject(with: decoded!) as! T
            return decodedModel
        }
        else
        {
            return nil
        }
    }

这篇关于应用程序崩溃并出现错误:无法推断出通用参数"T"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆