-1

I am using File Storage system for saving some data models confirming to Codable Protocol.

My Save function is as below:

func save<T: Encodable>(value: T, for key: String, on path: URL) throws {
        let url = path.appendingPathComponent(key, isDirectory: false)
        do {
            try ANFileManager.createDirectoryAtPath(path: url.deletingLastPathComponent())
            let archiver = NSKeyedArchiver(requiringSecureCoding: true)
            archiver.outputFormat = .binary
            try archiver.encodeEncodable(value, forKey: NSKeyedArchiveRootObjectKey)
            archiver.finishEncoding()
            // then you can use encoded data
            try archiver.encodedData.write(to: url)
            
        } catch {
            throw StorageError.cantWrite(error)
        }
    }

My fetch function is as below:

 func fetchValue<T: Decodable>(for key: String, from path: URL) throws -> T {
        let url = path.appendingPathComponent(key)
        
        let data = try Data(contentsOf: url)
        let unarchiver = try NSKeyedUnarchiver(forReadingFrom: data)
        unarchiver.decodingFailurePolicy = .setErrorAndReturn
        guard let decoded = unarchiver.decodeDecodable(T.self, forKey:
            NSKeyedArchiveRootObjectKey) else {
                throw StorageError.notFound
        }
        
        unarchiver.finishDecoding()
        
        if let error = unarchiver.error {
            throw StorageError.cantRead(error)
        }
        else {
            return decoded
        }
        
    }

Save and fetch are working fine but at runtime seeing some below warning in xcode console.

    *** -[NSKeyedUnarchiver validateAllowedClass:forKey:] allowed unarchiving safe plist type ''NSString' (0x7fff863014d0) [/Applications/Xcode_13.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/Foundation.framework]' for key 'NS.keys', even though it was not explicitly included in the client allowed classes set: '{(
    "'NSDictionary' (0x7fff862db9a0) [/Applications/Xcode_13.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/CoreFoundation.framework]",
    "'NSDate' (0x7fff862db798) [/Applications/Xcode_13.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/CoreFoundation.framework]"
)}'. This will be disallowed in the future.

What should be done to suppress the warning ?

the monk
  • 389
  • 4
  • 14
  • May one ask why you are spending so much effort passing thru NSKeyedUnarchiver when a Codable can be so simply saved directly as property list or json? – matt Sep 28 '21 at 21:49
  • Want to save file which is unreadable text at first. – the monk Sep 28 '21 at 22:03
  • Also, My models were migrated from NSCoding to Codable. – the monk Sep 28 '21 at 22:09
  • I'm sorry but that is ridiculous. The encoding for keyed archiver and for Codable property list is identical. There is nothing less "readable" in what you're doing. And they are Codable now so why live in the past? – matt Sep 28 '21 at 22:15
  • Ok, I get that. I was using KeyArchiver for my Models Confirming to NSCoding. And continue with same storage function after converting the Models to Codable. I think I should change it now. – the monk Sep 28 '21 at 22:23

1 Answers1

1

The problem is the failure to require secure coding on the unarchiver:

https://developer.apple.com/documentation/foundation/nskeyedunarchiver/1410824-requiressecurecoding

But more broadly it is very odd to pass through a keyed archiver when Codable is already saveable directly.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • unarchiver.requiresSecureCoding = false, Added this but still seeing the same warning. – the monk Sep 28 '21 at 22:05
  • 1
    Because it needs to be true! – matt Sep 28 '21 at 22:13
  • I tried removing all the code related to Keyed archiver but still, the app was seeing this warning. I think its the issue with firebase library because removing the Firebase initialization seems to remove the warning. – the monk Oct 05 '21 at 20:38