由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“试图插入非属性列表对象

我尝试了互联网上的所有建议。

我在这里试图在用户默认值中保存一系列词典。

struct A {
    a: [u8; 100],num: usize,}

impl Iterator for A {
    type Item = &[u8]; // this requires a lifetime parameter,but there is none declared

    fn next(&mut self) -> Option<Self::Item> {
         if self.num >= 10 {
             return None;
         }

         let res = &self.a[10*self.num..10*(self.num+1)];
         self.num += 1;
         Some(res)
    }
}

在userDefModel类中

let tokenInfo: [String: Any] = ["data": response?.data as Any,"startDate": response?.startDate as Any,"endDate": response?.endDate as Any]
{
 if error == nil
    userDefModel().token = tokenInfo
}
niuhenan 回答:由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“试图插入非属性列表对象

此错误是由于您的词典项引起的,请在将其设置为令牌之前尝试打印词典。

我认为问题可能出在数据中,您需要先对其进行转换,然后再将其存储在字典中。

NSString(data: response!.data,encoding: NSUTF8StringEncoding)

另一种解决方案:

// Save
UserDefaults.standard.set(try? PropertyListEncoder().encode(value),forKey: key)

// Retrieve 
if let data = UserDefaults.standard.value(forKey: key) as? Data {
        userData = try? PropertyListDecoder().decode(Profile.self,from: data)
}
本文链接:https://www.f2er.com/3169918.html

大家都在问