获取日历,时间,分钟,秒
01
// Get the Gregorian calendar
02
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
03
04
// Get the date
05
NSDate* now = [NSDate date];
06
07
// Get the hours, minutes, seconds
08
NSDateComponents* nowHour = [cal components:NSHourCalendarUnit fromDate:now];
09
NSDateComponents* nowMinute = [cal components:NSMinuteCalendarUnit fromDate:now];
10
NSDateComponents* nowSecond = [cal components:NSSecondCalendarUnit fromDate:now];
启动和停止NSTimer执行1个任务
// 创建1个timer 每1秒调用taskToUpdate
NSTimer timer = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(taskToUpdate) userInfo:nil repeats:YES];
// 如果运行,停止timer
// (no way to "stop it" and "run it back", you must "kill it" and "recreate it")
if([timer isValid]){
[timer invalidate];
timerAffcheur = nil;
}
3、如何使用NSOperationQueue
@interface PersonTableViewController : UITableViewController <AddPerson> {
NSOperationQueue *queue;
}
@end
@implementation PersonTableViewController
- (void)addPerson:(NSString *)username {
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(addPersonInBackground:) object:username];
[queue addOperation:operation];
[operation release];
}
@end
4、NSVALUE与一些指针,结构体以及一些基本类型(CGPoint等)互转
结构体:typedef struct {
int id,
float height,
unsigned char flag
}MyTestStruct;
NSValue *value = [NSValue valueWithBytes:&myTestStruct objCType:@encode(MyTestStruct)];
MyTestStruct theTestStruct;
[value getValue:&theTestStruct];
其他:
@interface NSValue (NSValueUIGeometryExtensions)
+ (NSValue *)valueWithPointer:(const void *)pointer;//保存对象指针
+ (NSValue *)valueWithCGPoint:(CGPoint)point;//保存CGPoint结构体
+ (NSValue *)valueWithCGSize:(CGSize)size;//保存CGSize结构体
+ (NSValue *)valueWithCGRect:(CGRect)rect;//保存CGRect结构体
+ (NSValue *)valueWithCGAffineTransform:(CGAffineTransform)transform;
+ (NSValue *)valueWithUIEdgeInsets:(UIEdgeInsets)insets;
- (void *)pointerValue;
- (CGPoint)CGPointValue;
- (CGSize)CGSizeValue;
- (CGRect)CGRectValue;
- (CGAffineTransform)CGAffineTransformValue;
- (UIEdgeInsets)UIEdgeInsetsValue;
@end