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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值