2014年7月22日星期二

NSDateFormatter

寫程式, 不可能不遇到日期格式, 所以要學一下如何格式化輸出.

首先要看這個 URL:
https://developer.apple.com/library/Mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html

對應到 Anroid:


滿特別的, 大家都可以指定 AM & PM symbol.



ex1:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
 
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:118800];
 
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormatter setLocale:usLocale];
 
NSLog(@"Date for locale %@: %@",
      [[dateFormatter locale] localeIdentifier], [dateFormatter stringFromDate:date]);
// Output:
// Date for locale en_US: Jan 2, 2001
 
NSLocale *frLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"];
[dateFormatter setLocale:frLocale];
NSLog(@"Date for locale %@: %@",
      [[dateFormatter locale] localeIdentifier], [dateFormatter stringFromDate:date]);
// Output:
// Date for locale fr_FR: 2 janv. 2001

ex2:
Discussion
Different locales have different conventions for the ordering of date components. You use this method to get an appropriate format string for a given set of components for a specified locale (typically you use the current locale—see currentLocale).
The following example shows the difference between the date formats for British and American English:
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
 
NSString *dateFormat;
NSString *dateComponents = @"yMMMMd";
 
dateFormat = [NSDateFormatter dateFormatFromTemplate:dateComponents options:0 locale:usLocale];
NSLog(@"Date format for %@: %@",
    [usLocale displayNameForKey:NSLocaleIdentifier value:[usLocale localeIdentifier]], dateFormat);
 
dateFormat = [NSDateFormatter dateFormatFromTemplate:dateComponents options:0 locale:gbLocale];
NSLog(@"Date format for %@: %@",
    [gbLocale displayNameForKey:NSLocaleIdentifier value:[gbLocale localeIdentifier]], dateFormat);
 
// Output:
// Date format for English (United States): MMMM d, y
// Date format for English (United Kingdom): d MMMM y

How to write an Objective-C Completion Block

completion block, is a very funny and useful develop tool, I think every programmer should learn about it.

相較於python的 completion block, Objective-C 的寫的比較難懂和複雜,這個功能超級好用的,必學!


ex1:
1
2
3
4
5
6
7
8
9
10
//宣告整數型態變數aNumber
int aNumber = 5;
//宣告不需要回傳以及不需要輸入參數的output block
void (^output)(void) = ^{
NSLog(@"Print out aNumber: %d", aNumber);
};
//執行output block
output();
執行結果: "Print out aNumber: 5"



ex2:
1
2
3
4
5
6
7
8
9
10
11
- (void)doSomething completion:(void (^)(void))completion error:(void (^)(void))error {
    if (somtething) {
        error();
        return;
    }
    //do something
    //...
    completion();
}


ex3:
1
2
3
4
5
6
7
8
9
void (^completion)(void) = ^{
    NSLog(@"Finish");
};
void (^error)(void) = ^{
    NSLog(@"Error");
};
[self doSomething completion:completion error:error];
- See more at: http://furnacedigital.blogspot.tw/2010/12/blocksios4-coding.html

As a local variable:

returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...};

As a property:

@property (nonatomic, copy) returnType (^blockName)(parameterTypes);

As a method parameter:

- (void)someMethodThatTakesABlock:(returnType (^)(parameterTypes))blockName;

As an argument to a method call:

[someObject someMethodThatTakesABlock: ^returnType (parameters) {...}];

As a typedef:

typedef returnType (^TypeName)(parameterTypes);
TypeName blockName = ^returnType(parameters) {...};