Reputation: 3107
How do I implement the Core Data validation methods (these take the form validate<Key>:error:
in Obj-C) in Swift?
In Obj-C, I would write the following in my model class to validate the name attribute:
-(BOOL)validateName:(id *)name error:(NSError **)outError {
}
And I could cast the name to NSString by doing: ((NSString *)(*name))
In Swift, I assume this looks something like:
func validateName(name: AutoreleasingUnsafeMutablePointer<AnyObject?>, error: NSErrorPointer) -> Bool {
}
Is this the correct form?
How do I cast then the name to Swift's String?
Upvotes: 1
Views: 637
Reputation: 3107
The documentation on this is very sparse and there isn't much information about it around the web. Adding here as reference for others what I was able to find via some digging and trail and error.
This seems to be working:
// Validating the 'name' attribute of NSManagedObject's descendant
func validateName(name: AutoreleasingUnsafeMutablePointer<AnyObject?>, error: NSErrorPointer) -> Bool {
if let name = name.memory as? String {
// do validation here when name is not nil
} else {
// when name is nil
return false
}
}
Upvotes: 3