ios 相关代码段

   获取日历,时间,分钟,秒

 

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   



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值