PSPDFModel

Objective-C


@interface PSPDFModel : NSObject <NSCopying>

Swift

class ModelObject : NSObject, NSCopying

An abstract base class for model objects, using reflection to provide sensible default behaviors.

The default implementations of , -hash, and -isEqual: make use of the +propertyKeys method.

  • Returns a new instance of the receiver initialized using -initWithDictionary:error:.

    Declaration

    Objective-C

    + (nullable instancetype)
        modelWithDictionary:(nullable NSDictionary<NSString *, id> *)dictionaryValue
                      error:(NSError *_Nullable *_Nullable)error;
  • Initializes the receiver using key-value coding, setting the keys and values in the given dictionary.

    dictionaryValue - Property keys and values to set on the receiver. Any NSNull values will be converted to nil before being used. KVC validation methods will automatically be invoked for all of the properties given. If nil, this method is equivalent to -init. error - If not NULL, this may be set to any error that occurs (like a KVC validation error).

    Returns an initialized model object, or nil if validation failed.

    Declaration

    Objective-C

    - (nullable instancetype)
        initWithDictionary:(nullable NSDictionary<NSString *, id> *)dictionaryValue
                     error:(NSError *_Nullable *_Nullable)error;

    Swift

    init(dictionary dictionaryValue: [String : Any]?) throws
  • Returns the keys for all @property declarations, except for readonly properties without ivars, or properties on PSPDFModel itself.

    Declaration

    Objective-C

    @property (class, nonatomic, readonly) NSOrderedSet<NSString *> *_Nonnull propertyKeys;

    Swift

    class var propertyKeys: NSOrderedSet { get }
  • A dictionary representing the properties of the receiver.

    The default implementation combines the values corresponding to all +propertyKeys into a dictionary, with any nil values represented by NSNull.

    This property must never be nil.

    Declaration

    Objective-C

    @property (nonatomic, copy, readonly) NSDictionary<NSString *, id> *_Nonnull dictionaryValue;

    Swift

    var dictionaryValue: [String : Any] { get }
  • A dictionary mapping the underlying values to the keys specified in +propertyKeysToPreserveWhenDecoding.

    Declaration

    Objective-C

    @property (nonatomic, nullable) NSDictionary<NSString *, id> *preservedValues;

    Swift

    var preservedValues: [String : Any]? { get set }
  • Initializes the receiver from an archive.

    This will decode the original +modelVersion of the archived object, then invoke -decodeValueForKey:withCoder:modelVersion: for each of the receiver’s +propertyKeys.

    Returns an initialized model object, or nil if a decoding error occurred.

    Declaration

    Objective-C

    - (nullable instancetype)initWithCoder:(nonnull NSCoder *)coder;

    Swift

    init?(coder: NSCoder)
  • Archives the receiver using the given coder.

    This will encode the receiver’s +modelVersion, then the receiver’s properties according to the behaviors specified in +encodingBehaviorsByPropertyKey.

    Declaration

    Objective-C

    - (void)encodeWithCoder:(nonnull NSCoder *)coder;

    Swift

    func encode(with coder: NSCoder)
  • Defines the keys in propertyKeys that should be preserved internally in the -preservedValues.

    This is useful when decoding models that have properties that are mappings to properties on other objects, or that are computed dynamically. These properties might produce a valid value when encoding them, but when decoding them, often there would be no underlying model to store them (in the case of form fields, for instance, where the actual values are mapped to Core objects).

    Getters for the properties contained in this Set should be handled in a special way to see whether the underlying model exists to retrieve the value, or the preservedValues dictionary should be used.

    You can use the PSPDF_UNWRAP_PRESERVED_VALUE() macro to help with this.

    Declaration

    Objective-C

    @property (class, nonatomic, readonly) NSSet<NSString *> *_Nonnull propertyKeysToPreserveWhenDecoding;

    Swift

    class var propertyKeysToPreserveWhenDecoding: Set<String> { get }
  • Determines how the +propertyKeys of the class are encoded into an archive. The values of this dictionary should be boxed PSPDFModelEncodingBehavior values.

    Any keys not present in the dictionary will be excluded from the archive.

    Subclasses overriding this method should combine their values with those of super.

    Returns a dictionary mapping the receiver’s +propertyKeys to default encoding behaviors. If a property is an object with weak semantics, the default behavior is PSPDFModelEncodingBehaviorConditional; otherwise, the default is PSPDFModelEncodingBehaviorUnconditional.

    Declaration

    Objective-C

    @property (class, nonatomic, readonly) NSDictionary<NSString *, NSNumber *> *_Nonnull encodingBehaviorsByPropertyKey;

    Swift

    class var encodingBehaviorsByPropertyKey: [String : NSNumber] { get }
  • Determines the classes that are allowed to be decoded for each of the receiver’s properties when using . The values of this dictionary should be NSSets of Class objects.

    If any encodable keys (as determined by +encodingBehaviorsByPropertyKey) are not present in the dictionary, an exception will be thrown during secure encoding or decoding.

    Subclasses overriding this method should combine their values with those of super.

    Returns a dictionary mapping the receiver’s encodable keys (as determined by +encodingBehaviorsByPropertyKey) to default allowed classes, based on the type that each property is declared as. If type of an encodable property cannot be determined (e.g., it is declared as id), it will be omitted from the dictionary, and subclasses must provide a valid value to prevent an exception being thrown during encoding/decoding.

    Declaration

    Objective-C

    @property (class, nonatomic, readonly) NSDictionary<NSString *, NSSet<Class> *> *_Nonnull allowedSecureCodingClassesByPropertyKey;

    Swift

    class var allowedSecureCodingClassesByPropertyKey: [String : Set<AnyHashable>] { get }
  • Returns the decoded and boxed value, or nil if the key was not present.

    Declaration

    Objective-C

    - (nullable id)decodeValueForKey:(nonnull NSString *)key
                           withCoder:(nonnull NSCoder *)coder
                        modelVersion:(NSUInteger)modelVersion;

    Swift

    func decodeValue(forKey key: String, with coder: NSCoder, modelVersion: UInt) -> Any?
  • Override this to decode and return additional values (maybe to migrate from older model versions). These will be merged and also used to initialize the object. The default implementation will return nil. Subclasses of subclasses of PSPDFModel should make they combine their additional values with those from the superclass.

    Declaration

    Objective-C

    - (nullable NSDictionary<NSString *, id> *)
        decodeAdditionalValues:(nonnull NSCoder *)coder
                         model:(NSUInteger)model;

    Swift

    func decodeAdditionalValues(_ coder: NSCoder, model: UInt) -> [String : Any]?
  • The version of this PSPDFModel subclass.

    This version number is saved in archives so that later model changes can be made backwards-compatible with old versions.

    Subclasses should override this method to return a higher version number whenever a breaking change is made to the model.

    Returns 0.

    Declaration

    Objective-C

    @property (class, nonatomic, readonly) NSUInteger modelVersion;

    Swift

    class var modelVersion: UInt { get }