Class: NSFormatter

Inherits:
NSObject show all

Overview

NSFormatter is an abstract class that declares an interface for objects that create, interpret, and validate the textual representation of cell contents. The Foundation framework provides two concrete subclasses of NSFormatter to generate these objects: NSNumberFormatter and NSDateFormatter.

Direct Known Subclasses

NSByteCountFormatter, NSDateFormatter, NSNumberFormatter

Instance Method Summary (collapse)

Methods inherited from NSObject

#!, #!=, #!~, #, #==, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, alloc, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #eql?, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, #init, initialize, #initialize_clone, #initialize_copy, #initialize_dup, #inspect, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, #to_s, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version

Constructor Details

This class inherits a constructor from NSObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class NSObject

Instance Method Details

- (NSAttributedString) attributedStringForObjectValue(anObject, withDefaultAttributes:attributes)

The default implementation returns nil to indicate that the formatter object does not provide an attributed string. When implementing a subclass, return an NSAttributedString object if the string for display should have some attributes. For instance, you might want negative values in a financial application to appear in red text. Invoke your implementation of stringForObjectValue: to get the non-attributed string, then create an NSAttributedString object with it (see initWithString:). Use the attributes default dictionary to reset the attributes of the string when a change in value warrants it (for example, a negative value becomes positive) For information on creating attributed strings, see Attributed Strings Programming Guide.

Parameters:

  • anObject (Object)

    The object for which a textual representation is returned.

  • attributes (Hash)

    The default attributes to use for the returned attributed string.

Returns:

- (String) editingStringForObjectValue(anObject)

The default implementation of this method invokes stringForObjectValue:. When implementing a subclass, override this method only when the string that users see and the string that they edit are different. In your implementation, return an NSString object that is used for editing, following the logic recommended for implementing stringForObjectValue:. As an example, you would implement this method if you want the dollar signs in displayed strings removed for editing.

Parameters:

  • anObject (Object)

    The object for which to return an editing string.

Returns:

  • (String)

    An NSString object that is used for editing the textual representation of anObject.

- (Boolean) getObjectValue(anObject, forString:string, errorDescription:error)

The default implementation of this method raises an exception. When implementing a subclass, return by reference the object anObject after creating it from string. Return YES if the conversion is successful. If you return NO, also return by indirection (in error) a localized user-presentable NSString object that explains the reason why the conversion failed; the delegate (if any) of the NSControl object managing the cell can then respond to the failure in control:didFailToFormatString:errorDescription:. However, if error is nil, the sender is not interested in the error description, and you should not attempt to assign one.The following example (which is paired with the example given in stringForObjectValue:) converts a string representation of a dollar amount that includes the dollar sign; it uses an NSScanner instance to convert this amount to a float after stripping out the initial dollar sign.

Parameters:

  • anObject (Object)

    If conversion is successful, upon return contains the object created from string.

  • string (String)

    The string to parse.

  • error (Pointer)

    If non-nil, if there is a error during the conversion, upon return contains an NSString object that describes the problem.

Returns:

  • (Boolean)

    YES if the conversion from string to cell content object was successful, otherwise NO.

- (Boolean) isPartialStringValid(partialString, newEditingString:newString, errorDescription:error)

Returns a Boolean value that indicates whether a partial string is valid. This method is invoked each time the user presses a key while the cell has the keyboard focus—it lets you verify and edit the cell text as the user types it.In a subclass implementation, evaluate partialString according to the context, edit the text if necessary, and return by reference any edited string in newString. Return YES if partialString is acceptable and NO if partialString is unacceptable. If you return NO and newString is nil, the cell displays partialString minus the last character typed. 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:. The selection range will always be set to the end of the text if replacement occurs.This method is a compatibility method. If a subclass overrides this method and does not override isPartialStringValid:proposedSelectedRange:originalString:originalSelectedRange:errorDescription:, this method will be called as before (isPartialStringValid:proposedSelectedRange:originalString:originalSelectedRange:errorDescription: just calls this one by default).

Parameters:

  • partialString (String)

    The text currently in a cell.

  • newString (Pointer)

    If partialString needs to be modified, upon return contains the replacement string.

  • error (Pointer)

    If non-nil, if validation fails contains an NSString object that describes the problem.

Returns:

  • (Boolean)

    YES if partialString is an acceptable value, otherwise NO.

- (Boolean) isPartialStringValid(partialStringPtr, proposedSelectedRange:proposedSelRangePtr, originalString:origString, originalSelectedRange:origSelRange, errorDescription:error)

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). In a subclass implementation, evaluate partialString according to the context. Return YES if partialStringPtr is acceptable and NO if partialStringPtr is unacceptable. Assign a new string to partialStringPtr and a new range to proposedSelRangePtr and return NO if you want to replace the string and change the selection range. 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:.

Parameters:

  • partialStringPtr (Pointer)

    The new string to validate.

  • proposedSelRangePtr (NSRangePointer)

    The selection range that will be used if the string is accepted or replaced.

  • origString (String)

    The original string, before the proposed change.

  • origSelRange (NSRange)

    The selection range over which the change is to take place.

  • error (Pointer)

    If non-nil, if validation fails contains an NSString object that describes the problem.

Returns:

  • (Boolean)

    YES if partialStringPtr is acceptable, otherwise NO.

- (String) stringForObjectValue(anObject)

The default implementation of this method raises an exception. When implementing a subclass, return the NSString object that textually represents the cell’s object for display and—if editingStringForObjectValue: is unimplemented—for editing. First test the passed-in object to see if it’s of the correct class. If it isn’t, return nil; but if it is of the right class, return a properly formatted and, if necessary, localized string. (See the specification of the NSString class for formatting and localizing details.)The following implementation (which is paired with the getObjectValue:forString:errorDescription: example above) prefixes a two-digit float representation with a dollar sign:

Parameters:

  • anObject (Object)

    The object for which a textual representation is returned.

Returns:

  • (String)

    An NSString object that textually represents object for display. Returns nil if object is not of the correct class.