Last active
October 18, 2016 23:52
-
-
Save zorgiepoo/ac3092b1a8c235b6f7625a10eeb569d9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This uses a legacy version of swift | |
import Foundation | |
class Model: NSObject, NSSecureCoding { | |
let bar: String | |
init(bar: String) { | |
self.bar = bar | |
} | |
required init?(coder decoder: NSCoder) { | |
// This is the problematic line mentioned in the error report | |
// ** Amended note: passing [NSString.self] instead of Set(arrayLiteral: [NSString.self]) fixes this program's bug ** | |
self.bar = decoder.decodeObjectOfClasses(Set(arrayLiteral: [NSString.self]), forKey: "bar") as! String | |
// This line would work fine, but not the line above which causes an exception to be raised | |
//self.bar = decoder.decodeObjectOfClass(NSString.self, forKey: "bar") as! String | |
} | |
func encodeWithCoder(coder: NSCoder) { | |
coder.encodeObject(self.bar, forKey: "bar") | |
} | |
static func supportsSecureCoding() -> Bool { | |
return true | |
} | |
} | |
print("Hello, World!") | |
let model = Model(bar: "meow") | |
// Encode some data | |
let encodedData = NSMutableData() | |
let keyedArchiver = NSKeyedArchiver(forWritingWithMutableData: encodedData) | |
keyedArchiver.requiresSecureCoding = true // make sure secure coding is enabled | |
keyedArchiver.encodeObject(model, forKey: "model") | |
keyedArchiver.finishEncoding() | |
// Decode data | |
let keyedUnarchiver = NSKeyedUnarchiver(forReadingWithData: encodedData) | |
// Make sure secure coding is enabled | |
// If this line is commented out, the program works properly but without secure validation... | |
keyedUnarchiver.requiresSecureCoding = true | |
// An exception on this line occurs, see below for the report.. | |
let unarchivedTool = keyedUnarchiver.decodeObjectOfClass(Model.self, forKey: "model")! | |
keyedUnarchiver.finishDecoding() | |
// Test data | |
assert(unarchivedTool.bar == "meow") | |
print("YAY") | |
/* | |
Hello, World! | |
2016-10-18 16:43:12.269508 ArchiveTest[32621:1949468] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSKeyedUnarchiver decodeObjectOfClasses:forKey:] object in classes '( | |
NSString | |
)' is not an Objective-C class!' | |
*** First throw call stack: | |
( | |
0 CoreFoundation 0x00007fffa22b352b __exceptionPreprocess + 171 | |
1 libobjc.A.dylib 0x00007fffb698bcad objc_exception_throw + 48 | |
2 CoreFoundation 0x00007fffa2331a0d +[NSException raise:format:] + 205 | |
3 Foundation 0x00007fffa3c2c6f4 -[NSKeyedUnarchiver decodeObjectOfClasses:forKey:] + 549 | |
4 ArchiveTest 0x000000010002ac09 _TFE10FoundationCSo7NSCoder21decodeObjectOfClassesfTGSqCSo5NSSet_6forKeySS_GSqPs9AnyObject__ + 505 | |
5 ArchiveTest 0x00000001000024b9 _TFC11ArchiveTest5ModelcfT5coderCSo7NSCoder_GSqS0__ + 377 | |
6 ArchiveTest 0x000000010000260d _TToFC11ArchiveTest5ModelcfT5coderCSo7NSCoder_GSqS0__ + 45 | |
7 | |
libc++abi.dylib: terminating with uncaught exception of type NSException | |
(lldb) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment