使用NSUserDefaults,即使程式退出运行也会保存信息 * NSUserDefault只能保存NSString型,NSNumber型,NSArray型,NSDictionary型,NSData型,其他型的话可先转成NSData型,
取得userDefaults,登录到Dictionary: NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];//取得 NSMutableDictionary *mDic= [NSMutableDictionary dictionary]; 初始化key和value,然后登记到Dictionary里: [defaults setObject: @"99" forKey:@"KEY_I"]; // key的初始值是99 [defaults setObject:@"hello" forKey:@"KEY_H"]; //key的初始值是hello [ud registerDefaults:mDic]; // *只会登记不存在的key的值
设定key和value,保存并更新: NSUserDefaults *ud = [NSUserDefaults standardUserDefaults]; //取得 [ud setFloat:1.23 forKey:@"KEY_F"]; //保存 [ud setObject:@"你好" forKey:@"KEY_S"]; //保存 [ud synchronize]; // *不存在的key新規,已存在的key更新其值 取得信息: NSUserDefaults *ud = [NSUserDefaults standardUserDefaults]; //取得 float f = [ud floatForKey:@"KEY_F"]; NSString s = [ud stringForKey:@"KEY_S"]; 删除信息: NSUserDefaults *ud = [NSUserDefaults standardUserDefaults]; //取得 [ud removeObjectForKey:@"KEY_I"]; 作成通知: NSNotification *n = [NSNotification notificationWithName:@"Today" object:self userInfo:dic]; 通知开始执行: [[NSNotificationCenter defaultCenter] postNotification:cn];
收信方: 取得通知: NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; 去通知中心要求得到通知同时呼叫需执行的方法: [nc addObserver:self selector:@selector(hello:) name:@"Today" object:nil]; -(void)hello:(NSNotificationCenter*)center{ //从通知得到的值center NSString *value = [[centeruserInfo] objectForKey:@"KEY"];
timer是否已经开始在进行中: BOOL b = [tm isValid];
自己设定的形式来取得日期: NSDateFormatter *df = [[NSDateFormatter alloc] init]; df.dateFormat = @"yyyy/MM/dd HH:mm:ss"; 或 [dfsetDateFormat:@"yyyy/MM/dd HH:mm:ss"];
取得现在日期时间:(连接上面设定形式) NSString *str = [df stringFromDate:[NSDate date]]; 设定日期:(连接上面设定形式) NSDate *aDate = [dfdateFromString: @"2000/03/01 00:00:00"];
从某时间开始经过某秒后的日期时间: bDate = [aDate initWithTimeInterval:3*60 sinceDate:aDate];//从aDate过3分钟 指定某月的末日:(使用前一个月的第一天来取得) NSDate *aDate = [inputDateFormatter dateFromString:@"2000/03/01 00:00:00"]; //给定3月1日零点日期 NSDate *bDate = [orgDate initWithTimeInterval:-1*24*60*60 sinceDate:a];// 1日前 NSLog(@"2000年2月的末日-> %@", bDate);
取得某两个时间相隔多久: since = [dateA timeIntervalSinceDate: dateB];
只取得日期不要时间: NSDateFormatter *df= [[NSDateFormatter alloc] init]; [dfsetTimeStyle: NSDateFormatterNoStyle]; [dfsetDateStyle: NSDateFormatterMediumStyle]; NSString *now |
|
来自: plumbiossom > 《代码》