//
// main.m
// NSNumberTest
//
// Created by Unisk on 13-9-19.
// Copyright (c) 2013年 Test. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Student.h"
#import "Person.h"
#pragma mark NSNumber相关
void nsnumerTest(){
NSNumber *number=[NSNumber numberWithInt:11];
NSMutableArray *array=[NSMutableArray array];
[array addObject:number];
// NSLog(@"%@",array);
// NSNumber *number1=[array lastObject];
NSNumber *number1=array[0];
// NSLog(@"%@",number1);s
int num=[number1 intValue];
NSLog(@"%i",num);
}
#pragma mark NSValue 用法
void nsvaule(){
CGPoint point=CGPointMake(11, 12);
NSValue *value=[NSValue valueWithPoint:point];
NSMutableArray *array=[NSMutableArray array];
[array addObject:value];
NSValue *nsVault=[array lastObject];
CGPoint point1=[nsVault pointValue];
BOOL result= CGPointEqualToPoint(point, point1);
NSLog(@"%i",result);
}
typedef struct {
int year;
int month;
int day;
} Date;
#pragma mark 结构体相关
void value(){
Date date={2013,9,18};
// 根据结构体类型生成对应的类型描述字符串
char *type=@encode(Date);
NSValue *value=[NSValue value:&date withObjCType:type];
Date date1;
// 取出包装好的结构体
[value getValue:&date1];
NSLog(@"year=%i,month=%i,day=%i",date1.year,date1.month,date1.day);
}
#pragma mark NSDate
void nsDate(){
NSDate *date=[NSDate date];
// NSLog(@"%@",date);
// [NSDate distantFuture]; 将来的某个时间
// [NSDate distantPast]; 过去的某个时间
// 返回1970-1-1 到现在的秒数
// NSTimeInterval interval=[date timeIntervalSince1970];
// NSLog(@"%.0f",interval);
// NSDate *date1=[NSDate date];
// [date earlierDate:date1];
// [date laterDate:date1];
NSDateFormatter *formater=[[[NSDateFormatter alloc] init]autorelease];
formater.dateFormat=@"yyyy-MM-dd hh:mm:ss";
NSString *string=[formater stringFromDate:date];
NSLog(@"%@",string);
}
#pragma mark 相关
void classTest(){
Student *stu=[[[Student alloc] init]autorelease];
//isKindOfClass 判断对象是否属于某个类,或者子类
if ([stu isKindOfClass:[Person class]]) {
NSLog(@"是的");
}
//isMemberOfClass 判断某个类是否属于某个类,不包含子类
if ([stu isMemberOfClass:[Student class]]) {
NSLog(@"不是");
}
//直接调用
// [stu test];
//间接调用
[stu performSelector:@selector(test)];
}
#pragma mark 反射相关
void reflect(){
NSString *str=@"Person";
//将字符串转换为class类
Class class=NSClassFromString(str);
//实例化对象
Person *person=[[[class alloc] init] autorelease];
NSLog(@"%@",person);
//将类转换为string
// NSString *str=NSStringFromClass([Person class]);
// NSLog(@"%@",str);
//方法的反射
NSString *method=@"test";
// 根据字符串得到方法
SEL selector=NSSelectorFromString(method);
[person performSelector:selector];
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
reflect();
}
return 0;
}
#import <Foundation/Foundation.h>
@interface Person : NSObject
-(void)test;
@end
#import "Person.h"
@implementation Person
-(void)test{
NSLog(@"test 被调用了");
}
@end
#import "Person.h"
@interface Student : Person
-(void)test:(NSString *) string;
@end
#import "Student.h"
@implementation Student
-(void)test:(NSString *)string{
NSLog(@"test %@被调用了",string);
}
@end