using NSKeyedArchiver with UITextView

I am using NSKeyedArchiver to archive and retrieve a UITextView. This works in the I can retrieve most of UITextView. However I cannot save and retrieve info about the border which is stored in a CALayer.

             let tvdata = try NSKeyedArchiver.archivedData(withRootObject: textview! as UITextView, requiringSecureCoding: false)

            let tvtmp = try! (NSKeyedUnarchiver.unarchiveTopLevelObjectWithData((self.tvdata?.regularFileContents)!) as? UITextView)!

            let myAtmp = UITextView.unsecureUnarchived(from: tvdata)

            print("textview.layer.borderWidth = \(textview!.layer.borderWidth)")

            print("myAtmp.layer.borderWidth = \(myAtmp!.layer.borderWidth)")

 

            let tvWrapper:FileWrapper = FileWrapper.init(regularFileWithContents:tvdata as Data)

            let tmptv = UITextView.unsecureUnarchived(from: (tvWrapper.regularFileContents!))

            

Both tvTmp and myAtmp contains all of the usual UITextView values. Neither has the value for borderWidth. The printed values are :

textview.layer.borderWidth = 3.0

myAtmp.layer.borderWidth = 0.0

tmptv.layer.borderWidth = 0.0

How can I archive and unarchive the borderWidth value?

Answered by Claude31 in 731249022

So layer is handled differently, as an "extra" property and not considered by KeyArchiver.

In fact layer is just used for rendering view’s Core Animation. And properties are accessed via the layer delegate, not directly in the view. May that be the reason ?

Anyway, if problem solved, don't forget to close the thread. Good continuation.

There must be some missing words here: "This works in the I can retrieve most of UITextView. "

You could save borderWidth value under a separate key

Thanks. Saving borderwidth under a separate key solved my problem. I still don’t understand why the borderwidth was not archived with UITextView though

Accepted Answer

So layer is handled differently, as an "extra" property and not considered by KeyArchiver.

In fact layer is just used for rendering view’s Core Animation. And properties are accessed via the layer delegate, not directly in the view. May that be the reason ?

Anyway, if problem solved, don't forget to close the thread. Good continuation.

using NSKeyedArchiver with UITextView
 
 
Q