Subtle difference of NSKeyedArchiver binary format on application target and widget extension

I met one strange phenomenon about NSKeyedArchiver and NSKeyedUnarchiver.

I found that data consisted of HTTPCookie archived on application cannot be unarchived on widget extension. Code to create sample cookie object and archive it on application is below.

Code Block
let cookie = HTTPCookie(properties: [
.domain: "apple.com",
.path: "/",
.name: "name",
.value: "value",
.secure: "TRUE",
.expires: Date()
])
let data = try NSKeyedArchiver.archivedData(withRootObject: cookie!, requiringSecureCoding: false)
UserDefaults(suiteName: "***")?.setValue(data, forKey: "cookie")


Data is successfully created and data is stored in UserDefaults using AppGroups to share data between application and widget extension. Code to unarchive data on widget extension is below.

Code Block
let data = UserDefaults(suiteName: "***")!.data(forKey: "cookie")!
let cookie = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data)


Then return value stored in cookie is nil and no error thrown on widget extension. On the other hands, completely same code on application target works fine (return original cookie value).

I checked this phenomenon occures on newly created project by Xcode 12.1.



I investigated that why this data cannot be unarchived on widget extension and finally I found subtle difference of binary data archived on application target and on widget extension. I exported each archived data and see contents by plutil.

Archived cookie data on application target:
Code Block
{
"$archiver" => "NSKeyedArchiver"
"$objects" => [
0 => "$null"
1 => {
"$class" => <CFKeyedArchiverUID 0x7f902ef04460 [0x7fff8e833cc0]>{value = 19}
"properties" => <CFKeyedArchiverUID 0x7f902ef04440 [0x7fff8e833cc0]>{value = 2}
}
2 => {
"$class" => <CFKeyedArchiverUID 0x7f902ef04880 [0x7fff8e833cc0]>{value = 18}
"NS.keys" => [
0 => <CFKeyedArchiverUID 0x7f902ef045a0 [0x7fff8e833cc0]>{value = 3}
1 => <CFKeyedArchiverUID 0x7f902ef045c0 [0x7fff8e833cc0]>{value = 4}
...
}
"$version" => 100000
}


Archived cookie data on widget extension:
Code Block
{
"$archiver" => "NSKeyedArchiver"
"$objects" => [
0 => "$null"
1 => {
"$0" => <CFKeyedArchiverUID 0x7fd9b8505b50 [0x7fff8e833cc0]>{value = 2}
"$class" => <CFKeyedArchiverUID 0x7fd9b8505b70 [0x7fff8e833cc0]>{value = 19}
}
2 => {
"$class" => <CFKeyedArchiverUID 0x7fd9b8505f90 [0x7fff8e833cc0]>{value = 18}
"NS.keys" => [
0 => <CFKeyedArchiverUID 0x7fd9b8505cb0 [0x7fff8e833cc0]>{value = 3}
1 => <CFKeyedArchiverUID 0x7fd9b8505cd0 [0x7fff8e833cc0]>{value = 4}
...
}
"$version" => 100000
}


Subtle difference is on line 6 and 7 (properties and $0).

I thought this defference may also affect to result of unarchive but I'm not sure this difference is cause of result of unarchive or not and why this defference occurs.

My current question is "Why this subtle difference exist? This phenomenon occurs at other environment? Is there way to fix this phenomenon?".

Thanks :)