Is it possible to store the array safely? #377

Closed
opened 2025-12-29 18:26:15 +01:00 by adam · 3 comments
Owner

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.)

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
            }
        }
    }
}
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 } } } } ```
adam closed this issue 2025-12-29 18:26:15 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Oct 23, 2021):

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:

@Field.Coded("values", coder: FieldCoders.Json.self)
var values: [MyCodableType] = []
@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] = [] ```
Author
Owner

@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'
@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' ```
Author
Owner

@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.

@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.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore-JohnEstropia#377