Instance Method

validateValue:forKey:error:

Validates a property value for a given key.

Declaration

- (BOOL)validateValue:(id  _Nullable *)value forKey:(NSString *)key error:(NSError * _Nullable *)error;

Parameters

value

A pointer to an object.

key

The name of one of the receiver's properties.

error

If value is not a valid value for key (and cannot be coerced), upon return contains an instance of NSError that describes the problem.

Return Value

YES if value is a valid value for key (or if value can be coerced into a valid value for key), otherwise NO.

Discussion

This method is responsible for two things: coercing the value into an appropriate type for the object, and validating it according to the object’s rules.

The default implementation provided by NSManagedObject consults the object’s entity description to coerce the value and to check for basic errors, such as a null value when that isn’t allowed and the length of strings when a field width is specified for the attribute. It then searches for a method of the form validate<Key>:error: and invokes it if it exists.

You can implement methods of the form validate<Key>:error: to perform validation that is not possible using the constraints available in the property description. If it finds an unacceptable value, your validation method should return NO and in error an NSError object that describes the problem. For more details, see Managed Object Validation. For inter-property validation (to check for combinations of values that are invalid), see validateForUpdate: and related methods.

See Also

Managing Data Validation

- validateForDelete:

Determines whether the managed object can be deleted in its current state.

- validateForInsert:

Determines whether the managed object can be inserted in its current state.

- validateForUpdate:

Determines whether the managed object's current state is valid.

Validation Error Codes

Error codes related to validation of managed object properties.

NSValidationKeyErrorKey

The error key for the attribute that failed to validate.

NSValidationObjectErrorKey

The error key for the object that failed to validate.

NSValidationPredicateErrorKey

The error key for the predicate that failed to validate.

NSValidationValueErrorKey

The error key for the value that failed to validate.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software