func validateValue (AutoreleasingUnsafeMutablePointer<AnyObject?>, forKeyPath : String)
A pointer to a new value for the property identified by in
. This method may modify or replace the value in order to make it valid.
Availability
Technology
func validateValue(_ ioValue: AutoreleasingUnsafeMutablePointer
<AnyObject?>,
forKey inKey: String
) throws
ioValue
A pointer to a new value for the property identified by in
. This method may modify or replace the value in order to make it valid.
inKey
The name of one of the receiver's properties. The key must specify an attribute or a to-one relationship.
A Boolean set to true
if the value pointed at by io
is valid for the property identified by in
, or if the method is able to modify the value at io
to make it valid; otherwise false
.
The default implementation of this function searches the class of the receiver for a property specific validation function with a particular signature, allowing that function to determine the outcome of the validation. For it to be found, the property specific validation function must be exposed to Objective-C, must be named according to the pattern validate<In
, must take a single, optional Any
pointer argument, and must throw. For example, for a property named some
, the validation function is:
If you define such a function, the default implementation of validate
calls it when asked to validate the corresponding property, allowing your function to either alter the input value or throw an error.
If no such function exists for a particular property, validate
returns without taking any other action. In other words, by default, the general validation call succeeds if you don't explicitly provide a validation function for the given property.
Note 注意
You typically use the validation described here only in Objective-C. In Swift, property validation is more idiomatically handled by relying on compiler support for optionals and strong type checking, while using the built-in will
and did
property observers to test any run-time API contracts, as described in the Property Observers section of The Swift Programming Language (Swift 4.1).
See Adding Validation in Key-Value Coding Programming Guide for more information.
func validateValue (AutoreleasingUnsafeMutablePointer<AnyObject?>, forKeyPath : String)