Where do I register a ValueTransformer in Swift?
You are right, you can register your value transformers in the AppDelegate
. If you want something that closer resembles ObjectiveC's +initialize
you can use lazy initialization of a class variable. E.g:
class AppDelegate: NSObject, NSApplicationDelegate {
static let doInitialize: Void = {
// register transformers here
}()
override init() {
super.init()
AppDelegate.doInitialize
}
}
This pattern should also work for classes other than the AppDelegate
if you want to keep the transformers things closer to the classes that actually use them.
Where do I register a ValueTransformer in Swift?
You are right, you can register your value transformers in the AppDelegate
. If you want something that closer resembles ObjectiveC's +initialize
you can use lazy initialization of a class variable. E.g:
class AppDelegate: NSObject, NSApplicationDelegate {
static let doInitialize: Void = {
// register transformers here
}()
override init() {
super.init()
AppDelegate.doInitialize
}
}
This pattern should also work for classes other than the AppDelegate
if you want to keep the transformers things closer to the classes that actually use them.
Custom NSValueTransformer in xcode 6 with swift
After you initialise newTransformer you should also include the line:
NSValueTransformer.setValueTransformer(newTransformer, forName: "myTransformer")
Then in your Interface Builder you should use myTransformer instead of newTransformer under the Value Transformer dropdown.
In a SwiftUI lifecycle app, where exactly should I register a CoreData transformerValue?
Put it into init
, like
class PersistenceController {
static let shared = PersistenceController()
init() {
UIColorValueTransformer.register() // << here !!
// ... other init code
}
// ... other code
}
CoreData Transformable: Custom Transformer never gets called - NSKeyedArchiver is used instead
You need to register your ValueTransformer before you use it.
ValueTransformer.setValueTransformer(MyTransformer(), forName: NSValueTransformerName(rawValue: "MyTransformer"))
let fetch: NSFetchRequest<Entity> = Entity.fetchRequest()
For more info refer docs.
CoreData: ValueTransformer functions are not called
You haven't overridden the correct methods of ValueTransformer
. Your methods are:
func transformedValue(value: String?) -> NSData?
func reverseTransformedValue(value: NSData?) -> String?
The correct methods are:
func transformedValue(_ value: Any?) -> Any?
func reverseTransformedValue(_ value: Any?) -> Any?
The big hint that you're implementing the wrong methods is that you didn't need to add the override
keyword.
BTW, this expression:
encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!
can be replaced with:
encoding: .uf8
It would also likely be better to replace your return "nil"
with return nil
; it's a String?
, so it can be nil
if things go wrong.
Core Data not automatically calling value transformer when getting / setting attribute directly in code
(From the comment above:) The inverse transformer is called when you save the context,
not when you set an attribute.
Related Topics
Does Cocoa Connection Binding to Nstoolbaritem Prevent Deinitializing
Combined Watch Os and iOS Framework
Trying to Access Error Code in Alamofire
How to Write to a Variable from Within The Firebase Getdocument Function (Swift)
Accessing Bundle of Main Application While Running Xctests
Modifying Struct Instance Variables Within a Dispatch Closure in Swift
Navigationlink Inside .Searchable Does Not Work
Uitableview Only Displays One Section
Rxswift: Ondisposed Activated Before Alamofire Return Data
Edit Templates Authentication Firebase
Swift Wkwebview: Can't Find Variable Error When Calling Method
How to Access The Model Component of Reality Composer in Realitykit
Access Id Does Not Work When Testing a Textfield with 'Issecuretextentry = True'
Ambiguous Use of 'Filter' When Converting Project to Swift 4