Originally created by @noppefoxwolf on GitHub (Oct 23, 2021).
I made a FieldCoderType because I want to save an array of NSCoding.
Is the following implementation safe?
I want to receive feedbacks about this. (I'm a beginner for CoreData.)
Originally created by @noppefoxwolf on GitHub (Oct 23, 2021).
I made a FieldCoderType because I want to save an array of NSCoding.
Is the following implementation safe?
I want to receive feedbacks about this. (I'm a beginner for CoreData.)
```swift
import Foundation
import CoreStore
extension FieldCoders {
public struct NSArray<C: Foundation.NSObject & Foundation.NSCoding>: FieldCoderType {
public typealias FieldStoredValue = Array<C>
public static func encodeToStoredData(_ fieldValue: FieldStoredValue?) -> Data? {
guard let fieldValue = fieldValue else {
return nil
}
return try! NSKeyedArchiver.archivedData(
withRootObject: fieldValue,
requiringSecureCoding: self.requiresSecureCoding
)
}
public static func decodeFromStoredData(_ data: Data?) -> FieldStoredValue? {
guard let data = data else {
return nil
}
return try! NSKeyedUnarchiver.unarchivedObject(
ofClasses: [Foundation.NSArray.self, FieldStoredValue.Element.self],
from: data
) as? FieldStoredValue
}
private static var requiresSecureCoding: Bool {
switch FieldStoredValue.Element.self {
case let valueType as NSSecureCoding.Type:
return valueType.supportsSecureCoding
default:
return false
}
}
}
}
```
No need to implement your own. CoreStore implements FieldCoders.DefaultNSSecureCoding and FieldCoders.DefaultNSCoding for NSArrays, or you can use FieldCoders.Json directly for swift Arrays:
@JohnEstropia commented on GitHub (Oct 23, 2021):
No need to implement your own. CoreStore implements `FieldCoders.DefaultNSSecureCoding` and `FieldCoders.DefaultNSCoding` for `NSArray`s, or you can use `FieldCoders.Json` directly for swift `Array`s:
```swift
@Field.Coded("values", coder: FieldCoders.Json.self)
var values: [MyCodableType] = []
```
DefaultNSSecureCoding looks not supports typed array.
How to serialize to element that cannot encode json value?
@Field.Coded("images", coder: FieldCoders.DefaultNSSecureCoding.self)
public var images: [UIImage] = []
Referencing initializer 'init(wrappedValue:_:versionHashModifier:previousVersionKeyPath:coder:customGetter:customSetter:affectedByKeyPaths:)' on 'FieldContainer.Coded' requires that '[UIImage]' conform to 'FieldOptionalType'
@noppefoxwolf commented on GitHub (Oct 24, 2021):
DefaultNSSecureCoding looks not supports typed array.
How to serialize to element that cannot encode json value?
```
@Field.Coded("images", coder: FieldCoders.DefaultNSSecureCoding.self)
public var images: [UIImage] = []
```
```
Referencing initializer 'init(wrappedValue:_:versionHashModifier:previousVersionKeyPath:coder:customGetter:customSetter:affectedByKeyPaths:)' on 'FieldContainer.Coded' requires that '[UIImage]' conform to 'FieldOptionalType'
```
Your original code used KeyedArchiver and KeyedUnarchiver, which DefaultNSSecureCoding uses internally so your code wouldn't work either if DefaultNSSecureCoding doesn't. You need to have Codable types to use the built-in coders, otherwise you have to create your own way to convert to and from Data without using KeyedArchiver/Unarchiver.
@JohnEstropia commented on GitHub (Oct 24, 2021):
Your original code used KeyedArchiver and KeyedUnarchiver, which `DefaultNSSecureCoding` uses internally so your code wouldn't work either if `DefaultNSSecureCoding` doesn't. You need to have `Codable` types to use the built-in coders, otherwise you have to create your own way to convert to and from `Data` without using KeyedArchiver/Unarchiver.
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Originally created by @noppefoxwolf on GitHub (Oct 23, 2021).
I made a FieldCoderType because I want to save an array of NSCoding.
Is the following implementation safe?
I want to receive feedbacks about this. (I'm a beginner for CoreData.)
@JohnEstropia commented on GitHub (Oct 23, 2021):
No need to implement your own. CoreStore implements
FieldCoders.DefaultNSSecureCodingandFieldCoders.DefaultNSCodingforNSArrays, or you can useFieldCoders.Jsondirectly for swiftArrays:@noppefoxwolf commented on GitHub (Oct 24, 2021):
DefaultNSSecureCoding looks not supports typed array.
How to serialize to element that cannot encode json value?
@JohnEstropia commented on GitHub (Oct 24, 2021):
Your original code used KeyedArchiver and KeyedUnarchiver, which
DefaultNSSecureCodinguses internally so your code wouldn't work either ifDefaultNSSecureCodingdoesn't. You need to haveCodabletypes to use the built-in coders, otherwise you have to create your own way to convert to and fromDatawithout using KeyedArchiver/Unarchiver.