挺简单啥也不说了直接上代码(NSDate和反射)
// // main.m // NSNumberAndNSValue // // Created by WildCat on 13-7-26. // Copyright (c) 2013年 wildcat. All rights reserved. // #import <Foundation/Foundation.h> #import "Person.h" #pragma mark -NSDate的练习 void date(){ //获得的当前时间是格林治时间 NSDate *mydate=[NSDate date]; NSLog(@"当前时间是:%@",mydate); mydate=[NSDate dateWithTimeIntervalSinceNow:10]; NSLog(@"10秒后是:%@",mydate); //时间格式化 NSDateFormatter *formatter=[[[NSDateFormatter alloc] init] autorelease]; formatter.dateFormat=@"yyyy-MM-dd HH:mm:ss"; NSString *string=[formatter stringFromDate:mydate]; NSLog(@"格式化后是:%@",string); NSDate *datell=[formatter dateFromString:string]; NSLog(@"变回来是:%@",datell); } #pragma mark - 反射 //根据一个字符串来实例化一个对象 void reflect(){ //类名的反射 NSString *str=@"Person"; Class class=NSClassFromString(str); Person *p=[[class alloc] init]; NSLog(@"%@",p); //Class变成字符串 NSString *className=NSStringFromClass([Person class]); NSLog(@"className:%@",className); //方法的反射 SEL sel=NSSelectorFromString(@"test"); [p performSelector:sel];//会输出:person调用了test方法 [p release]; } int main(int argc, const char * argv[]) { @autoreleasepool { //number(); //valueUser(); //关于NSDate的操作 date(); reflect(); } return 0; }
person类:
#import <Foundation/Foundation.h> @interface Person : NSObject -(void)test; @end
#import "Person.h" @implementation Person -(void)test{ NSLog(@"person调用了test方法"); } @end