1. NSUserDefaults
個人認為ios這個方法有點類似於c#中Properties.Settings.Default,NSUserDefaults只支援: NSString, NSNumber, NSDate, NSArray, NSDictionary.
建立一個user defaults方法有多個,最簡單得快速建立方法:
NSUserDefaults *accountDefaults = [NSUserDefaults standardUserDefaults];
添加資料到 user defaults:
[accountDefaults setObject:nameField.text forKey:UserDefaultNameKey];
也可以添加基礎資料型別 (Elementary Data Type)int, float, bool等,有相應得方法
[accountDefaults setBool:YES forKey:UserDefaultBoolKey];
從user defaults中擷取資料:
[accountDefaults objectForKey:NCUserDefaultNameKey]
[accountDefaults boolForKey: UserDefaultBoolKey];
針對同一個關鍵字對應的對象或者資料,可以對它進行重寫,重寫之後關鍵字就對應新的對象或者資料,舊的對象或者資料會被自動清理。
2. NSDateFormatter
格式化日期文字,copy一份常用參數:
a: AM/PM (上午/下午)
A: 0~86399999 (一天的第A微秒)
c/cc: 1~7 (一周的第一天, 周天為1)
ccc: Sun/Mon/Tue/Wed/Thu/Fri/Sat (星期幾簡寫)
cccc: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday (星期幾全拼)
d: 1~31 (月份的第幾天, 帶0)
D: 1~366 (年份的第幾天,帶0)
e: 1~7 (一周的第幾天, 帶0)
E~EEE: Sun/Mon/Tue/Wed/Thu/Fri/Sat (星期幾簡寫)
EEEE: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday (星期幾全拼)
F: 1~5 (每月的第幾周, 一周的第一天為周一)
g: Julian Day Number (number of days since 4713 BC January 1) 未知
G~GGG: BC/AD (Era Designator Abbreviated) 未知
GGGG: Before Christ/Anno Domini 未知
h: 1~12 (0 padded Hour (12hr)) 帶0的時, 12小時制
H: 0~23 (0 padded Hour (24hr)) 帶0的時, 24小時制
k: 1~24 (0 padded Hour (24hr) 帶0的時, 24小時制
K: 0~11 (0 padded Hour (12hr)) 帶0的時, 12小時制
L/LL: 1~12 (0 padded Month) 第幾月
LLL: Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec 月份簡寫
LLLL: January/February/March/April/May/June/July/August/September/October/November/December 月份全稱
m: 0~59 (0 padded Minute) 分鐘
M/MM: 1~12 (0 padded Month) 第幾月
MMM: Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec
MMMM: January/February/March/April/May/June/July/August/September/October/November/December
q/qq: 1~4 (0 padded Quarter) 第幾季度
qqq: Q1/Q2/Q3/Q4 季度簡寫
qqqq: 1st quarter/2nd quarter/3rd quarter/4th quarter 季度全拼
Q/QQ: 1~4 (0 padded Quarter) 同小寫
QQQ: Q1/Q2/Q3/Q4 同小寫
QQQQ: 1st quarter/2nd quarter/3rd quarter/4th quarter 同小寫
s: 0~59 (0 padded Second) 秒數
S: (rounded Sub-Second) 未知
u: (0 padded Year) 未知
v~vvv: (General GMT Timezone Abbreviation) 常規GMT時區的編寫
vvvv: (General GMT Timezone Name) 常規GMT時區的名稱
w: 1~53 (0 padded Week of Year, 1st day of week = Sunday, NB: 1st week of year starts from the last Sunday of last year) 一年的第幾周, 一周的開始為周日,第一周從去年的最後一個周日起算
W: 1~5 (0 padded Week of Month, 1st day of week = Sunday) 一個月的第幾周
y/yyyy: (Full Year) 完整的年份
yy/yyy: (2 Digits Year) 2個數位年份
Y/YYYY: (Full Year, starting from the Sunday of the 1st week of year) 這個年份未知幹嘛用的
YY/YYY: (2 Digits Year, starting from the Sunday of the 1st week of year) 這個年份未知幹嘛用的
z~zzz: (Specific GMT Timezone Abbreviation) 指定GMT時區的編寫
zzzz: (Specific GMT Timezone Name) Z: +0000 (RFC 822 Timezone) 指定GMT時區的名稱
執行個體:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setAMSymbol:@"AM"];
[formatter setPMSymbol:@"PM"];
[formatter setDateFormat:@"MM/dd/yyyy hh:mm:a"];
// [NSString stringWithFormat:@"Last Updated: %@", [formatter stringFromDate:[NSDate date]]];
3. backgroundView
UIView* backgroundView= = [[TESTCellView alloc] initWithFrame:CGRectZero];
backgroundView.opaque = YES;
backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"list-gap.png"]];
backgroundView = backgroundView;
[backgroundView release];
4. NSParameterAssert
字面意思是帶參數的斷點,條件為假時執行,主要用於程式調試
NSParameterAssert( 1== 2 );
5. Reachability
類似於一個網路狀況的探針。
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
hostReach = [[Reachability reachabilityWithHostName: @"hubj.cnblogs.com"] retain];
[hostReach startNotifier];
wifiReach=[[Reachability reachabilityForLocalWiFi]retain];
[wifiReach startNotifier];
- (void)reachabilityChanged: (NSNotification* )note {
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
NetworkStatus netStatus = [curReach currentReachabilityStatus];
switch (netStatus)
{
case NotReachable:
{
NSLog(@"Access Not Available");
break;
}
case ReachableViaWWAN:
{
NSLog(@"Reachable WWAN");
break;
}
case ReachableViaWiFi:
{
NSLog(@"Reachable WiFi");
break;
}
}
}