swift 需要帮助才能使NSSecureUnarchiveFromDataTransformer工作

wnavrhmk  于 2023-11-16  发布在  Swift
关注(0)|答案(1)|浏览(77)

我试图实现NSSecureCoding,但我得到了一些问题,我的代码是这样的。一个名为WrapperClass的类:

public class WrapperClass:NSObject, NSCoding, NSSecureCoding {
    public static var supportsSecureCoding: Bool = true
    
    var attributedStrings = [TextDocumentClass]()
    
    init(pAttributedStrings:[TextDocumentClass]) {
        self.attributedStrings = pAttributedStrings
    }
    
    required public init?(coder aDecoder: NSCoder){
        self.attributedStrings = aDecoder.decodeObject(of: [TextDocumentClass.self], forKey: "attributedStrings") as? [TextDocumentClass]
        
        //return self
    }
    
    public func encode(with aCoder: NSCoder) {
        aCoder.encode(attributedStrings,forKey:"attributedStrings")
    }
}

字符串
当这段代码得到运行我得到nil而不是数组的TextDocumentClass和我不知道为什么,我已经尝试使用以下类在aDecoder.decodeObject([TextDocumentClass.self],NSArray.self和TextDocumentClass.self],但他们都给予相同的我已经使我自己的ValueTransformer,因为我需要支持一些类,我不认为是包括与默认的NSSecureUnarchiveFromDataTransformer

@objc(MyTestClassValueTransformer)
final class MyClassValueTransformer: NSSecureUnarchiveFromDataTransformer {

    static let name = NSValueTransformerName(rawValue: String(describing: MyClassValueTransformer.self))
    
    public static func register() {
        let transformer = MyClassValueTransformer()
        ValueTransformer.setValueTransformer(transformer, forName: name)
    }
    
    override static var allowedTopLevelClasses: [AnyClass] {
        return super.allowedTopLevelClasses + [WrapperClass.self, UIFont.self, NSObject.self, NSParagraphStyle.self]
    }
    
    override public class func transformedValueClass() -> AnyClass {
            return WrapperClass.self
        }

        override public class func allowsReverseTransformation() -> Bool {
            return true
        }
    
    override public func transformedValue(_ value: Any?) -> Any? {
        guard let data = value as? Data else {
            return nil
        }

        do {
            let box = try NSKeyedUnarchiver.unarchivedObject(
                ofClass: WrapperClass.self,
                from: data
            )
            return box
        } catch {
            return nil
        }
    }
    
    override public func reverseTransformedValue(_ value: Any?) -> Any? {
        guard let box = value as? WrapperClass else {
            return nil
        }

        do {
            let data = try NSKeyedArchiver.archivedData(
              withRootObject: box,
              requiringSecureCoding: true
          )
            return data
        } catch {
            return nil
        }
    }
}


类TextDocumentClass包含以下变量NSAttributedString、String、Bool和Int

public class TextDocumentClass:NSObject, NSCoding, NSSecureCoding {
    public static var supportsSecureCoding: Bool = true
    
    var attributedText: NSAttributedString!
    var uuid:String!
    var isDeleted:Bool!
    var isChanged:Bool!
    var prevPageUUID:String!
    var nextPageUUID:String!
    var order:Int!
    var lastUpdatedDate:String!
    var isPDF: Bool!
    var pdfURL: String!
    var pdfPageNumber: Int!
    
    init (pAttributedText:NSAttributedString, pUUID:String, pPrevPageUUID:String, pNextPageUID:String, pOrder:Int, pIsDeleted: Bool, pIsChanged: Bool, pLastUpdatedDate: String, isPDF: Bool = false, pdfURL: String = "", pdfPageNumber: Int = -1) {
        self.attributedText = pAttributedText
        self.uuid = pUUID
        self.isDeleted = pIsDeleted
        self.isChanged = pIsChanged
        self.prevPageUUID = pPrevPageUUID
        self.nextPageUUID = pNextPageUID
        self.order = pOrder
        self.lastUpdatedDate = pLastUpdatedDate
        self.isPDF = isPDF
        self.pdfURL = pdfURL
        self.pdfPageNumber = pdfPageNumber
    }
    
    required public init?(coder aDecoder: NSCoder){
        self.attributedText = (aDecoder.decodeObject(of: NSAttributedString.self, forKey: "attributedText"))! 
        self.prevPageUUID = (aDecoder.decodeObject(of: NSString.self, forKey: "prevPageUUID") as? String)! 
        self.nextPageUUID = (aDecoder.decodeObject(of: NSString.self, forKey: "nextPageUUID") as? String)! 
        self.uuid = (aDecoder.decodeObject(of: NSString.self, forKey: "uuid") as? String)! 
        self.isChanged = (aDecoder.decodeObject(of: NSNumber.self, forKey: "isChanged")?.boolValue)! 
        self.isDeleted = (aDecoder.decodeObject(of: NSNumber.self, forKey: "isDeleted")?.boolValue)! 
        self.order = (aDecoder.decodeObject(of: NSNumber.self, forKey: "order")?.intValue)! 
        self.lastUpdatedDate = (aDecoder.decodeObject(of: NSString.self, forKey: "lastUpdatedDate") as? String)! 
        self.isPDF = aDecoder.decodeObject(of: NSNumber.self, forKey: "isPDF")?.boolValue ?? false 
        self.pdfURL = (aDecoder.decodeObject(of: NSString.self, forKey: "pdfURL") as? String ?? "") 
        self.pdfPageNumber = aDecoder.decodeObject(of: NSNumber.self, forKey: "PDFpageNumber")?.intValue ?? -1 
    }
    
    public func encode(with aCoder: NSCoder) {
        aCoder.encode(attributedText,forKey:"attributedText")
        aCoder.encode(prevPageUUID,forKey:"prevPageUUID")
        aCoder.encode(nextPageUUID,forKey:"nextPageUUID")
        aCoder.encode(uuid,forKey:"uuid")
        aCoder.encode(isChanged,forKey:"isChanged")
        aCoder.encode(isDeleted,forKey:"isDeleted")
        aCoder.encode(order,forKey:"order")
        aCoder.encode(lastUpdatedDate,forKey:"lastUpdatedDate")
        aCoder.encode(isPDF, forKey: "isPDF")
        aCoder.encode(pdfURL, forKey: "pdfURL")
        aCoder.encode(pdfPageNumber, forKey: "PDFpageNumber")
    }
    
    override public init() {
        self.attributedText = NSAttributedString()
        self.uuid = ""
        self.isDeleted = false
        self.isChanged = false
        self.prevPageUUID = ""
        self.nextPageUUID = ""
        self.order = -1
        self.lastUpdatedDate = ""
        self.isPDF = false
        self.pdfURL = ""
        self.pdfPageNumber = -1
    }
}


一点额外的信息不确定它是否有帮助,我弄清楚如何得到一个错误,为什么它的nil

//this code line
let temp = try aDecoder.decodeTopLevelObject(of: [BaselineClass.self], forKey: "attributedStrings")
//gives this print in console
Allowed classes are:
 {(
    "'demo.BaselineClass' (0x100f6fc40) [/private/var/containers/Bundle/Application/EF7BF8EB-A98B-4B52-AE8F-1FB05F31ECC3/demo.app]"
)}" UserInfo={NSDebugDescription=value for key 'attributedStrings' was of unexpected class 'NSArray' (0x1fc4b89e0) [/System/Library/Frameworks/CoreFoundation.framework].
Allowed classes are:
 {(
    "'demo.BaselineClass' (0x100f6fc40) [/private/var/containers/Bundle/Application/EF7BF8EB-A98B-4B52-AE8F-1FB05F31ECC3/demo.app]"
)}}
//if i change the code line to use NSArray.self so it looks like this
let temp = try aDecoder.decodeTopLevelObject(of: NSArray.self, forKey: "attributedStrings")
//i get this in console instead
Allowed classes are:
 {(
    "'NSArray' (0x1fc4b89e0) [/System/Library/Frameworks/CoreFoundation.framework]"
)}" UserInfo={NSDebugDescription=value for key 'NS.objects' was of unexpected class 'demo.BaselineClass' (0x103363c40) [/private/var/containers/Bundle/Application/1E080145-14D1-4D03-9520-E87C39B8F2C7/demo.app].
Allowed classes are:
 {(
    "'NSArray' (0x1fc4b89e0) [/System/Library/Frameworks/CoreFoundation.framework]"
)}}


任何帮助解决这个问题是感激。

xpszyzbs

xpszyzbs1#

在与苹果来回沟通后,以下解决了我的问题。

self.attributedStrings = aDecoder.decodeObject(of: [TextDocumentClass.self], forKey: "attributedStrings") as? [TextDocumentClass]

字符串
应该改成这样。

self.attributedStrings = aDecoder.decodeObject(of: [NSArray.self, TextDocumentClass.self], forKey: "attributedStrings") as? [TextDocumentClass]


然后我在我的TextDocumentClass公共初始化器中还有一些其他的小错误,其中bool和int的decodeObject应该使用decodeBool和decodeInt32。

相关问题