UserDefault取值decode出现allowed unarchiving safe plist type ''NSString' 警告

1,705 阅读1分钟

从UserDefault中取出对象进行decode时出现如下警告:

-[NSKeyedUnarchiver validateAllowedClass:forKey:] allowed unarchiving safe plist type ''NSString' (XXXXXX) [/System/Library/Frameworks/Foundation.framework]' for key 'xxxxxx', even though it was not explicitly included in the client allowed classes set: {( "'XXXXXX' (xxxxx) [/private/var/containers/Bundle/Application/3FC52E6B-41F7-4C81-8922-C5070569A40D/XXXXXX.app]" )}'. This will be disallowed in the future.

代码能正常运行,但这个警告好烦啊。

去除这个警告:

修改前:

(coder.decodeObject(forKey: CodingKeys.XXXXX.rawValue) as? String) ?? ""

修改后:

(coder.decodeObject(of: NSString.self, forKey: CodingKeys.XXXXX.rawValue) as? String) ?? ""