Post

Replies

Boosts

Views

Activity

Reply to NSKeyedUnarchiver unarchiveTopLevelObjectWithData deprecated - Array
Bingo - found the problem. I broke out the archiving code that was not working and put it in a new simple app (see below). I created a simple class with a Swift Array in it, wrote the archive out with the non NSSecureCoding code. Then changed the code so the class uses NSSecureCoding and changed the de-archiving code to read that file back in using: try newTest = NSKeyedUnarchiver.unarchivedObject(ofClasses: [Test.self], from: fileData as Data) as? Test It failed silently like the main app failed. Then as suggested by Polyphonic I added NSArray.self to the ofClasses: parameter - ran the app again and a litany of good info was spewed to the console when the test app tried to decode the Array - the real app did not send anything to the console - it just failed silently returning a nil for the Array. Here's the console message: NSCoderTest[11222:509325] [general] *** -[NSKeyedUnarchiver validateAllowedClass:forKey:] allowed unarchiving safe plist type ''NSNumber' (0x2140200a8) [/System/Library/Frameworks/Foundation.framework]' for key 'NS.objects', even though it was not explicitly included in the client allowed classes set: '{( "'NSCoderTest.Test' (0x100eb0700) , "'NSArray' (0x213ff2440) [/System/Library/Frameworks/CoreFoundation.framework]" )}'. This will be disallowed in the future. Then I added NSNumber.self to the ofClasses: parameter and all works as it should. The main app where the code was failing was created three years ago with whatever was the current Xcode version at the time. I suspect there is something mangled in the project file that keeps it from emitting some error / warning messages to the console. Great. The laconic documentation for unarchivedObject(ofClasses:from:) says "A set of classes, at least one of which the root object should conform to". I think that means to add every class that your class needs to de-archive. Polyphonic has an excellent explanation about why this is necessary in his reply above. Polyphonic - thanks so much for the nudge in the right direction. Hey Apple, maybe a little more work on the documentation and a little less work on emojis maybe? Simple test code - class Test: NSObject, NSSecureCoding { static var supportsSecureCoding: Bool = true var i: Int64 = 55 var theArray: Array<Bool>? = [true, true, true, true] func encode(with coder: NSCoder) { coder.encode(i, forKey: "var_i") coder.encode(theArray, forKey: "var_a") } override init() { } required init?(coder: NSCoder) { if (coder.containsValue(forKey: "var_i")) { i = coder.decodeInt64(forKey: "var_i") } if (coder.containsValue(forKey: "var_a")) { theArray = coder.decodeObject(forKey: "var_a") as? Array<Bool> } } } func applicationDidFinishLaunching(_ aNotification: Notification) { var newTest: Test? let fileURL = URL(fileURLWithPath: "/Users/username/Downloads/testData", isDirectory: false) //let t1 = Test() //t1.i = 100 //t1.a[1] = false //t1.a[2] = false do { // let theData = try NSKeyedArchiver.archivedData(withRootObject: t1, requiringSecureCoding: false) // try theData.write(to: fileURL, options: NSData.WritingOptions.atomic) let fileData = try NSData(contentsOf: fileURL, options: []) // try newTest = NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(fileData as Data) as? Test try newTest = NSKeyedUnarchiver.unarchivedObject( ofClasses: [Test.self, NSArray.self], from: fileData as Data) as? Test } catch { print("well that didn't work.") } }
Topic: App & System Services SubTopic: General Tags:
Aug ’23
Reply to NSKeyedUnarchiver unarchiveTopLevelObjectWithData deprecated - Array
Thanks for the quick answers and explanation. In trying to suss this out I had added NSArray.self to the ofClasses: parameter and no joy. var newParts: Parts? newParts = try NSKeyedUnarchiver.unarchivedObject(ofClasses: [Parts.self, NSString.self, NSArray.self], from: newData) as? Parts The Swift Array in question is a member of the class Parts() and were encoded without NSSecureCoding - would that be a factor? (these archives were created from an earlier version of the app that I'm trying to migrate to Apple's deprecation de jour world) var pageUsesClearBackground: Array<Bool> = [true, true, true, true] coder.encode(pageUsesClearBackground, forKey: "CB_KEY") The plot thickens. Further down the line as things are being decoded I'm finding things that were encoded() are not decoding() - they return nil as well. macOS 14.0 is one sharp corner I must say.
Topic: App & System Services SubTopic: General Tags:
Aug ’23
Reply to Sudden error being logged continuously in XCode console
This just started happening out of the blue on a M1 Studio Mac with Monterrey 12.6.7 and XCode 14.1. The project is at least 3 years old and this warning has never shown up. One thing that is interesting is I move the project between the Studio Mac and an intel iMac every now and then. While trying to figure out what's gone wrong now - I happened to look up at the menu bar in XCode and it was saying to run the app under Rosetta. Changing that from My Mac (Rosetta) to My Mac solved the problem. No idea how that got changed.
Topic: Graphics & Games SubTopic: SpriteKit Tags:
Jul ’23
Reply to NSKeyedUnarchiver unarchiveTopLevelObjectWithData deprecated - Array
Bingo - found the problem. I broke out the archiving code that was not working and put it in a new simple app (see below). I created a simple class with a Swift Array in it, wrote the archive out with the non NSSecureCoding code. Then changed the code so the class uses NSSecureCoding and changed the de-archiving code to read that file back in using: try newTest = NSKeyedUnarchiver.unarchivedObject(ofClasses: [Test.self], from: fileData as Data) as? Test It failed silently like the main app failed. Then as suggested by Polyphonic I added NSArray.self to the ofClasses: parameter - ran the app again and a litany of good info was spewed to the console when the test app tried to decode the Array - the real app did not send anything to the console - it just failed silently returning a nil for the Array. Here's the console message: NSCoderTest[11222:509325] [general] *** -[NSKeyedUnarchiver validateAllowedClass:forKey:] allowed unarchiving safe plist type ''NSNumber' (0x2140200a8) [/System/Library/Frameworks/Foundation.framework]' for key 'NS.objects', even though it was not explicitly included in the client allowed classes set: '{( "'NSCoderTest.Test' (0x100eb0700) , "'NSArray' (0x213ff2440) [/System/Library/Frameworks/CoreFoundation.framework]" )}'. This will be disallowed in the future. Then I added NSNumber.self to the ofClasses: parameter and all works as it should. The main app where the code was failing was created three years ago with whatever was the current Xcode version at the time. I suspect there is something mangled in the project file that keeps it from emitting some error / warning messages to the console. Great. The laconic documentation for unarchivedObject(ofClasses:from:) says "A set of classes, at least one of which the root object should conform to". I think that means to add every class that your class needs to de-archive. Polyphonic has an excellent explanation about why this is necessary in his reply above. Polyphonic - thanks so much for the nudge in the right direction. Hey Apple, maybe a little more work on the documentation and a little less work on emojis maybe? Simple test code - class Test: NSObject, NSSecureCoding { static var supportsSecureCoding: Bool = true var i: Int64 = 55 var theArray: Array<Bool>? = [true, true, true, true] func encode(with coder: NSCoder) { coder.encode(i, forKey: "var_i") coder.encode(theArray, forKey: "var_a") } override init() { } required init?(coder: NSCoder) { if (coder.containsValue(forKey: "var_i")) { i = coder.decodeInt64(forKey: "var_i") } if (coder.containsValue(forKey: "var_a")) { theArray = coder.decodeObject(forKey: "var_a") as? Array<Bool> } } } func applicationDidFinishLaunching(_ aNotification: Notification) { var newTest: Test? let fileURL = URL(fileURLWithPath: "/Users/username/Downloads/testData", isDirectory: false) //let t1 = Test() //t1.i = 100 //t1.a[1] = false //t1.a[2] = false do { // let theData = try NSKeyedArchiver.archivedData(withRootObject: t1, requiringSecureCoding: false) // try theData.write(to: fileURL, options: NSData.WritingOptions.atomic) let fileData = try NSData(contentsOf: fileURL, options: []) // try newTest = NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(fileData as Data) as? Test try newTest = NSKeyedUnarchiver.unarchivedObject( ofClasses: [Test.self, NSArray.self], from: fileData as Data) as? Test } catch { print("well that didn't work.") } }
Topic: App & System Services SubTopic: General Tags:
Replies
Boosts
Views
Activity
Aug ’23
Reply to NSKeyedUnarchiver unarchiveTopLevelObjectWithData deprecated - Array
Thanks for the quick answers and explanation. In trying to suss this out I had added NSArray.self to the ofClasses: parameter and no joy. var newParts: Parts? newParts = try NSKeyedUnarchiver.unarchivedObject(ofClasses: [Parts.self, NSString.self, NSArray.self], from: newData) as? Parts The Swift Array in question is a member of the class Parts() and were encoded without NSSecureCoding - would that be a factor? (these archives were created from an earlier version of the app that I'm trying to migrate to Apple's deprecation de jour world) var pageUsesClearBackground: Array<Bool> = [true, true, true, true] coder.encode(pageUsesClearBackground, forKey: "CB_KEY") The plot thickens. Further down the line as things are being decoded I'm finding things that were encoded() are not decoding() - they return nil as well. macOS 14.0 is one sharp corner I must say.
Topic: App & System Services SubTopic: General Tags:
Replies
Boosts
Views
Activity
Aug ’23
Reply to Sudden error being logged continuously in XCode console
This just started happening out of the blue on a M1 Studio Mac with Monterrey 12.6.7 and XCode 14.1. The project is at least 3 years old and this warning has never shown up. One thing that is interesting is I move the project between the Studio Mac and an intel iMac every now and then. While trying to figure out what's gone wrong now - I happened to look up at the menu bar in XCode and it was saying to run the app under Rosetta. Changing that from My Mac (Rosetta) to My Mac solved the problem. No idea how that got changed.
Topic: Graphics & Games SubTopic: SpriteKit Tags:
Replies
Boosts
Views
Activity
Jul ’23
Reply to Expand details in NSPrintPanel by default?
Anyone ever figure this one out?
Topic: UI Frameworks SubTopic: AppKit Tags:
Replies
Boosts
Views
Activity
Jul ’22
Reply to Error 2003332927 (who?) in CoreMediaIO module
I'm getting the same error when playing the moveToTrash.aiff sound with AVAudioPlayer(contentsOf: url). It worked before Monterrey. Maybe after Apple gets done with the next round of emojis they could look into this.
Topic: Media Technologies SubTopic: Audio Tags:
Replies
Boosts
Views
Activity
Jul ’22