[英]CoreData: ValueTransformer functions are not called
I'm storing some key/value-pairs as Strings in CoreData which - in a newer version - should now be encrypted. 我将一些键/值对存储为CoreData中的字符串 - 在较新的版本中 - 现在应加密。 To not alone relying on Apples DataProtection I now want to encrypt the data before storing with RNCryptor and with the help of the ValueTransformer class.
不仅仅依赖于Apples DataProtection,我现在想要在使用RNCryptor存储之前以及在ValueTransformer类的帮助下加密数据。
However, the transform-functions are not called, neither debug-outputs nor breakpoints are triggered. 但是,不会调用转换函数,也不会触发调试输出和断点。 The strings are now stored as data objects, but can be read in plain text in binary representation - so they are obviously not encrypted.
字符串现在存储为数据对象,但可以以二进制表示形式以纯文本形式读取 - 因此它们显然不加密。
Here is what I changed: 这是我改变的:
import Foundation
import RNCryptor
class EncryptedStringTransformer : ValueTransformer {
let password = "SuperSecurePassword"
override class func allowsReverseTransformation() -> Bool{
return true
}
func transformedValue(value: String?) -> NSData? {
guard let data = value else {
return nil
}
let encryptData = Data(data.utf8)
let ciphertext = RNCryptor.encrypt(data: encryptData, withPassword: password)
return ciphertext as NSData
}
func reverseTransformedValue(value: NSData?) -> String? {
guard value != nil else {
return "nil"
}
do {
let originalData = try RNCryptor.decrypt(data: (value! as Data), withPassword: password)
return String(data: originalData, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!
} catch {
print(error)
return "nil"
}
}
}
So, the app continues to work flawlessly, and all database objects can be stored and retrieved (with the difference that they are now stored as a Data object rather than a String). 因此,应用程序继续完美地工作,并且可以存储和检索所有数据库对象(区别在于它们现在存储为Data对象而不是String)。 I'm checking the SQLite database directly with »DB Browser for SQLite«.
我正在使用»DB Browser for SQLite«直接检查SQLite数据库。
The expected behavior would be encrypted entries in CoreData. 预期的行为将是CoreData中的加密条目。 Can someone tell me what I am missing?
有人能告诉我我错过了什么吗? Some tutorials I found don't do any additional implementations and the few articles here on StackOverflow aren't helping either with this issue.
我发现的一些教程没有做任何其他实现,而StackOverflow上的一些文章对这个问题没有任何帮助。
I tried to change the output data from the transform-functions from Data to NSData, with no result. 我试图将转换函数的输出数据从Data更改为NSData,没有结果。 Am I missing something, so that the ValueTransformer is actually called?
我错过了什么,以便实际调用ValueTransformer? Any hint would be highly appreciated!
任何提示都将受到高度赞赏!
You haven't overridden the correct methods of ValueTransformer
. 您没有覆盖
ValueTransformer
的正确方法。 Your methods are: 你的方法是:
func transformedValue(value: String?) -> NSData?
func reverseTransformedValue(value: NSData?) -> String?
The correct methods are: 正确的方法是:
func transformedValue(_ value: Any?) -> Any?
func reverseTransformedValue(_ value: Any?) -> Any?
The big hint that you're implementing the wrong methods is that you didn't need to add the override
keyword. 您正在实施错误方法的一个重要提示是您不需要添加
override
关键字。
BTW, this expression: BTW,这个表达式:
encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!
can be replaced with: 可以替换为:
encoding: .uf8
It would also likely be better to replace your return "nil"
with return nil
; 用
return nil
替换你的return "nil"
也可能更好; it's a String?
这是一个
String?
, so it can be nil
if things go wrong. 如果出现问题,它可以是
nil
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.