This method should be implemented in subclasses that want to validate user changes to a string in a field, where the user changes are not necessarily at the end of the string, and preserve the selection (or set a different one, such as selecting the erroneous part of the string the user has typed).
Language
- Swift
- Objective-C
SDKs
- iOS 2.0+
- macOS 10.0+
- tvOS 9.0+
- watchOS 2.0+
Declaration
- (BOOL)isPartialStringValid:(NSString * _Nonnull *)partialStringPtr proposedSelectedRange:(NSRangePointer)proposedSelRangePtr originalString:(NSString *)origString originalSelectedRange:(NSRange)origSelRange errorDescription:(NSString * _Nullable *)error;
Parameters
partialStringPtrThe new string to validate.
proposedSelRangePtrThe selection range that will be used if the string is accepted or replaced.
origStringThe original string, before the proposed change.
origSelRangeThe selection range over which the change is to take place.
If the user change is a deletion,
origSelRangecontains the range of the deleted characters.errorIf non-
nil, if validation fails contains anNSStringobject that describes the problem.
Return Value
Discussion
In a subclass implementation, evaluate partialString according to the context. Return YES if partialStringPtr is acceptable and NO if partialStringPtr is unacceptable. If you return NO and assign a new string to partialStringPtr and a new range to proposedSelRangePtr, the string and selection range are changed, otherwise, if no values are assigned to partialStringPtr or proposedSelRangePtr, the change is rejected. If you return NO, you can also return by indirection an NSString object (in error) that explains the reason why the validation failed; the delegate (if any) of the NSControl object managing the cell can then respond to the failure in control:didFailToValidatePartialString:errorDescription:.