黑马程序员10--Foundation--03--集合类2-NSDictionary、NSNumber、NSDate

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

一、NSDictionary、NSMutableDictionary

内部没有顺序,不允许有相同的key,但可以有相同的value(object)。存储的东西都是键值对:key-value
// NSDictionary    
    // 单个对象单个索引:	
		NSDictionary *dict = [NSDictionary dictionaryWithObject:id forKey:(id
    
    
     
     )];
	// 取出相应内容
    	id obj = [dict objectForKey:@"keyname"];
	// 多个对象 多个keys
		NSDictionary *dict = [NSDictionary dictionaryWithObjects:(NSArray *) forKey:(NSArray *)];	
		NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:(id)...,nil];
	// 编译器特性 Xcode特性   快速创建查找
		NSDictionary *dict = @{@"name" : @"jack", @"address" : @"北京" };  // @{key1 :obj1 ,key2 :obj2}
		id obj = dict [key];
	// 对象方法count 返回值为键值对的个数
	    [dict count];
// NSMutableDictionary 
	// 创建:
    	NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    	[dict setObject:(id)  forKey: (id)];
	// 后赋值会覆盖之前的键值对
	// 移除键值对 
	    [dict removeObjectForKey:(id)];
// 遍历
	// block方法
	    [dict enumerateKeysAndObjectsUsingBlock:
    	^(id key, id obj, BOOL *stop){

    	}]

    
    

二、NSNumber

// NSNumber
    // 创建方法,类方法
        +(NSNumber *)numberWithChar:(char)value;
        +(NSNumber *)numberWithInt:(int)value;
        +(NSNumber *)numberWithFloat:(float)value;
        +(NSNumber *)numberWithDouble:(double)value;
        +(NSNumber *)numberWithBool:(BOOL)value;
        +(NSNumber *)numberWithInteger:(NSInteger)value;
    // 取出转化成相应类型,对象方法
        -(char)charValue;
        -(int)intValue;
        -(float)floatValue;
        -(double)doubleValue;
        -(BOOL)boolValue;
    // 编译器特性快速创建   @数字  转换为NSNumber对象
        NSNumber *num = @123;
        NSNumber *num = @'A';   // 注意@"A"、@'A'两者不同,第一个为NSString对象,第二个为NSNumber对象,传入的是char
        int b = 10;
        NSNumber *num = @(b);   // @()包装变量 
// NSNumber是NSValue的一个子类
    // NSValue创建方法
        +(NSValue *)valueWithPoint:(NSPoint)point;
        +(NSValue *)valueWithSize:(NSSize)size;
        +(NSValue *)valueWithRect:(NSRect)rect;
    // 对应转换
        -(NSPoint)pointValue;
        -(NSSize)sizeValue;
        -(NSRect)rectValue;

三、NSDate

// NSDate  
    // 打印当前调用时间,打印出来的为格林尼治时间
        NSDate *date = [NSDate date];  // 2013-08-12 08:55:30 +0000
	// 延迟5秒创建时间对象	
	    NSDate *date2 = [NSDate dateWithTimeInterval:5 sinceDate:Date];
	// 从1970开始走过的秒数
	    NSTimeInterval seconds = [date2 timeIntervalSince1970];

// 日期格式化类
	// NSDate转换成字符串
	    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
	// y年  M月  d日
	// m分  s秒  H时(24小时)   h时(12时)
	    formatter.dateFormat = @"yyyy-MM-dd  HH:mm:ss";
	    NSString *str = [formatter stringFromDate:date];

	// 字符串转换成NSDate	
	    NSString *time = @"2014/12/10 18:25";
    	NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    	formatter.dateFormat = @"yyyy/MM/dd  HH:mm";
    	NSDate *date = [formatter dateFromString:time];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值