NSDateFormatter

Formatter Styles:

 

NSDateFormatterNoStyle,

NSDateFormatterShortStyle,

NSDateFormatterMediumStyle,

NSDateFormatterLongStyle,

NSDateFormatterFullStyle.

 

 

// assume default behavior set for class using
// [NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *dateFormatter =
        [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
NSDate *date =
        [NSDate dateWithTimeIntervalSinceReferenceDate:118800];
NSString *formattedDateString = [dateFormatter stringFromDate:date];
NSLog(@"formattedDateString for locale %@: %@",
        [[dateFormatter locale] localeIdentifier], formattedDateString);
// Output: formattedDateString for locale en_US: Jan 2, 2001

 

 

 

Associating a Formatter With a Cell(给你个单元指定格式)

 

To create a formatter object programmatically and attach it to a cell, you allocate an instance of the formatter and set its format or style as you wish. You then use the NSCell setFormatter: method to associate the formatter instance with a cell. For example, the following Objective-C code creates an instance of NSNumberFormatter, sets its formatting for positive, zero, and negative values, and applies it to the cell of an NSTextField object using the setFormatter: method.

 

NSNumberFormatter *numberFormatter =
    [[[NSNumberFormatter alloc] init] autorelease];
[numberFormatter setFormat:@"$#,###.00;0.00;($#,##0.00)"];
[[textField cell] setFormatter:numberFormatter];

 

 

Similarly you can create an instance of NSDateFormatter (or NSGregorianDateFormatter for Java) object programmatically. The following example creates a date formatter with the format string %b %1d %Y (to give a date format like “Jan 2 2001”) and then associates the formatter with the cells of a form (contactsForm).

 

 

NSDateFormatter *dateFormat = [[NSDateFormatter alloc]
         initWithDateFormat:@"%b %1d %Y" allowNaturalLanguage:NO];
[[contactsForm cells] makeObjectsPerformSelector:@selector(setFormatter:)
         withObject:dateFormat]

 

 

 

 

Note that instances of formatter objects are immutable. In addition, when a cell with a formatter object is copied, the new cell retains the formatter object instead of copying it.

The value (that you retrieve using the method objectValue) of a cell (NSCell) is represented by an object, which in the number formatter code example above is a number object. In Java, it would be a java.lang.Number object, and in Objective-C, using Mac OS X version 10.0 behavior it would be a NSDecimalNumber object, using Mac OS X v10.4 behavior it would be by default a NSNumber object. In the case of the NSDateFormatter code example above, the cell’s value would be, either NSCalendarDate and NSGregorianDate, for Objective-C and Java respectively

When the cell needs to display or edit its value, it passes its object to the formatter which returns the formatted string. When the user enters a string, or when a string is programmatically written in a cell (using setStringValue), the cell obtains the corresponding object from the formatter.

你可能感兴趣的:(C++,c,Objective-C,OS,C#)