Core Data Property-Level Model Validation in Swift 4

ON IN SWIFT CORE-DATA

I ran into an issue while writing property validation methods for a Core Data stack where the methods simply weren’t being called. After a bit of head scratching I realised it was because the methods needed to be annotated with @objc. This wasn’t needed in Swift 3 because @objc was inferred on all methods of subclasses of NSObject; however, the behaviour of @objc inference changed in Swift 4. Now, subclasses of NSObject must explicitly mark methods that need to be accessible from Objective-C. This took longer to realise than it should have since the Core Data documentation’s sample code for property-level validation hasn’t been updated for Swift 4.