一.字典
NSDictionary是不可变的,并且不可以放基本类型
不可以相同的一个key对应多个values
可以多个key对应相同的values
下面看一下字典的一些操作:
-
//
-
// main.m
-
// 字典
-
//
-
// Created by 金海洋 on 15-4-14.
-
// Copyright (c) 2015年 金海洋. All rights reserved.
-
//
-
-
#import <Foundation/Foundation.h>
-
#import "Student.h"
-
-
void fun(){
-
//字典的初始化
-
//NSDictionary *di = [NSDictionary dictionary];
-
NSDictionary *di = [NSDictionary dictionaryWithObject:
-
@"a" forKey:@"b"];
-
-
NSLog(@"%@",di);
-
-
//最常用的
-
di = [NSDictionary dictionaryWithObjectsAndKeys:
-
@"a",@"b",
-
@"a1",@"b1",
-
@"a2",@"b2",nil];
-
-
NSLog(@"%@",di);
-
-
NSArray *a1 = [NSArray arrayWithObjects:@"a", @"a1",nil];
-
NSArray *b1 = [NSArray arrayWithObjects:@"b", @"b1",nil];
-
di = [NSDictionary dictionaryWithObjects:a1 forKeys:b1];
-
-
NSLog(@"%@",di);
-
-
}
-
-
void fun1(){
-
//字典的基本用法
-
-
NSDictionary *di = [NSDictionary dictionaryWithObjectsAndKeys:
-
@"a",@"b",
-
@"a1",@"b1",
-
@"a2",@"b2",nil];
-
-
NSDictionary *di1 = [NSDictionary dictionaryWithObjectsAndKeys:
-
@"a",@"b",
-
@"a1",@"b1",
-
@"a2",@"b2",nil];
-
-
//打印你的键值对
-
NSLog(@"%d",di.count);
-
-
//比较键值是否一样
-
if([di isEqualToDictionary:di1]){
-
NSLog(@"一样");
-
-
}
-
-
//取值
-
id o1 = [di objectForKey:@"b"];
-
NSLog(@"b--%@",o1);
-
-
NSString *path = @"/Users/jinhaiyang/Desktop/aa";
-
//将字典写入文件中
-
[di writeToFile:path atomically:YES];
-
-
//把文件中的东西读取给字典
-
NSDictionary *di2 = [NSDictionary dictionaryWithContentsOfFile:@"/Users/jinhaiyang/Desktop/aa"];
-
NSLog(@"%@",di2);
-
-
}
-
-
void fun2(){
-
//字典基本用法2
-
-
NSDictionary *di = [NSDictionary dictionaryWithObjectsAndKeys:
-
@"a",@"b",
-
@"a",@"b1",
-
@"a2",@"b2",nil];
-
-
//返回所有的key
-
NSArray *n1 = [di allKeys];
-
-
NSLog(@"%@",n1);
-
-
//返回对象对应的所有key
-
NSArray *n2 = [di allKeysForObject:@"a"];
-
NSLog(@"%@",n2);
-
-
//返回所有的values
-
NSArray *n3 = [di allValues];
-
NSLog(@"%@",n3);
-
-
//根据多个key取出对应的多个values
-
//notFoundMarker:<#(id)#> 如果找不到键值的话,就用他来替代
-
NSArray *n4 = [di objectsForKeys: [NSArray arrayWithObjects:@"b1",@"b2", @"af",nil] notFoundMarker:@"ccc"];
-
NSLog(@"%@",n4);
-
-
-
}
-
-
void fun3(){
-
//遍历字典1
-
-
NSDictionary *di = [NSDictionary dictionaryWithObjectsAndKeys:
-
@"a",@"b",
-
@"a",@"b1",
-
@"a2",@"b2",nil];
-
-
//遍历字典的所有KEY
-
for(id key in di){
-
-
id v = [di objectForKey:key];
-
NSLog(@"%@--%@",key,v);
-
-
}
-
-
}
-
-
void fun4(){
-
//遍历字典2
-
//迭代器
-
NSDictionary *di = [NSDictionary dictionaryWithObjectsAndKeys:
-
@"a",@"b",
-
@"a",@"b1",
-
@"a2",@"b2",nil];
-
-
//key迭代器
-
NSEnumerator *e = [di keyEnumerator];
-
-
id key = nil;
-
-
while (key = [e nextObject]) {
-
id a = [di objectForKey:key];
-
NSLog(@"%@-%@",key,a);
-
}
-
-
//对象迭代器
-
e = [di objectEnumerator];
-
-
-
}
-
-
void fun5(){
-
//字典遍历3
-
//block
-
NSDictionary *di = [NSDictionary dictionaryWithObjectsAndKeys:
-
@"a",@"b",
-
@"a",@"b1",
-
@"a2",@"b2",nil];
-
-
-
[di enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
-
NSLog(@"%@-%@",key,obj);
-
}];
-
-
-
}
-
-
void fun6(){
-
//内存管理
-
-
Student *s1 = [Student StudentWithName:@"ac"];
-
Student *s2 = [Student StudentWithName:@"adc"];
-
Student *s3 = [Student StudentWithName:@"vc"];
-
//对象当做key或者values的话就会做一次retain操作
-
NSDictionary *di = [NSDictionary dictionaryWithObjectsAndKeys:
-
s1,@"b",
-
s2,@"b1",
-
s3,@"b2",nil];
-
-
//当字典被销毁时,里面的Key和values会被release
-
-
-
}
-
-
int main(int argc, const char * argv[]) {
-
@autoreleasepool {
-
-
//fun();
-
//fun1();
-
//fun2();
-
//fun3();
-
//fun4();
-
//fun5();
-
fun6();
-
-
}
-
return 0;
-
}
二.可变字典
下面看一下代码:
-
//
-
// main.m
-
// 可变字典
-
//
-
// Created by 金海洋 on 15-4-15.
-
// Copyright (c) 2015年 金海洋. All rights reserved.
-
//
-
-
#import <Foundation/Foundation.h>
-
#import "Student.h"
-
-
void fun1(){
-
//基本用法
-
-
//创建
-
NSMutableDictionary *n1 = [NSMutableDictionary dictionary];
-
NSDictionary *n = [NSDictionary dictionaryWithObject:@"11" forKey:@"f"];
-
-
Student * s1 = [Student StudentWithName:@"ad"];
-
Student * s2 = [Student StudentWithName:@"aa"];
-
-
//添加
-
//s1 的计数器 会加1
-
[n1 setObject:s1 forKey:@"1"];
-
//删除所有元素
-
[n1 removeAllObjects];
-
//删除指定的key对应的元素
-
[n1 removeObjectForKey:@"1"];
-
//方法和字典的一些方法一样
-
-
//添加字典到 现有字典里
-
[n1 addEntriesFromDictionary:n ];
-
-
NSLog(@"%@",n1);
-
-
}
-
-
-
int main(int argc, const char * argv[]) {
-
@autoreleasepool {
-
-
fun1();
-
-
}
-
return 0;
-
}
二.NSNumber
NSNumber可以把基本类型包装成类,但是不能包装结构体
看下代码:
-
//
-
// main.m
-
// NSNumber
-
//
-
// Created by 金海洋 on 15-4-15.
-
// Copyright (c) 2015年 金海洋. All rights reserved.
-
//
-
-
#import <Foundation/Foundation.h>
-
-
void fun(){
-
-
//包装成一个NSNumber对象
-
NSNumber *n = [NSNumber numberWithInt:45];
-
NSLog(@"%@",n);
-
NSMutableArray *n1 = [NSMutableArray array];
-
//添加到数组中
-
[n1 addObject:n];
-
-
//取出来还是NSNumber对象,不支持自动解包
-
NSNumber *n2 = [n1 lastObject];
-
//转换成int类型
-
int i = [n2 intValue];
-
-
}
-
-
int main(int argc, const char * argv[]) {
-
@autoreleasepool {
-
-
fun();
-
-
-
}
-
return 0;
-
}
三.NSValue
NSNumber是NSvalue的子类,NSvalue能包装任何类型的数据
看下代码:
-
#import <Foundation/Foundation.h>
-
-
typedef struct{
-
-
int age;
-
int no;
-
-
} Stu;
-
-
-
void fun(){
-
//包装一个结构体
-
CGPoint p = CGPointMake(4, 2);
-
-
NSValue *n1 =[NSValue valueWithPoint:p];
-
-
NSMutableArray *n2 = [NSMutableArray array];
-
-
[n2 addObject:n1];
-
//取出值
-
NSValue *n3 = [n2 lastObject];
-
-
//取出结构体
-
CGPoint p1 = [n3 pointValue];
-
-
if( CGPointEqualToPoint(p, p1) ){
-
NSLog(@"成功");
-
-
}
-
-
}
-
-
void fun1(){
-
-
Stu s={1,22};
-
//根据类型产生字符串
-
char *type = @encode(Stu);
-
-
NSValue *n1 = [NSValue value:&s withObjCType:type];
-
-
NSMutableArray *n2 = [NSMutableArray array];
-
-
[n2 addObject:n1];
-
-
Stu ss;
-
//取出包装好的结构体
-
[n1 getValue:&ss];
-
-
NSLog(@"%d",ss.age);
-
-
//取出包装类型的字符串
-
const char *t1 = [n1 objCType];
-
-
-
}
-
-
-
-
int main(int argc, const char * argv[]) {
-
@autoreleasepool {
-
-
//fun();
-
fun1();
-
-
-
}
-
return 0;
-
}
四.NSNull
作用是,为了能把一个空值放入到对象
代码如下:
-
#import <Foundation/Foundation.h>
-
-
int main(int argc, const char * argv[]) {
-
@autoreleasepool {
-
-
//是个单例,只有一个静态方法
-
//单例:所有的实例对象都是相同的对象
-
NSNull *n = [NSNull null];
-
NSNull *n1 = [NSNull null];
-
-
if(n == n1)
-
NSLog(@"是个单例");
-
-
//如果字典里面有null 就不会解析 相当于没有
-
-
}
-
return 0;
-
}
五.NSDate
代码如下:
-
#import <Foundation/Foundation.h>
-
-
void fun(){
-
-
//返回的是当前时间
-
NSDate *d1 = [NSDate date];
-
NSLog(@"%@",d1);
-
-
//比当前时间快xxx秒
-
d1 = [NSDate dateWithTimeIntervalSinceNow:10];
-
NSLog(@"%@",d1);
-
-
//从1970年1月1日 开始算的时间
-
d1 = [NSDate dateWithTimeIntervalSince1970:10];
-
NSLog(@"%@",d1);
-
-
//随机返回一个比较遥远的未来时间
-
d1 = [NSDate distantFuture];
-
NSLog(@"%@",d1);
-
-
//随机返回一个比较遥远的过去时间
-
d1 = [NSDate distantPast];
-
NSLog(@"%@",d1);
-
-
-
-
}
-
-
void fun1(){
-
-
NSDate *d1 = [NSDate date];
-
NSDate *d2 = [NSDate date];
-
-
//返回1970 1 1开始返回的毫秒数
-
NSTimeInterval ti = [d1 timeIntervalSince1970];
-
//和其他时间进行对比
-
[d1 timeIntervalSinceDate:d2];
-
-
//返回一个比较早的那个时间
-
[d1 earlierDate:d2];
-
-
//返回一个比较晚的那个时间
-
[d1 laterDate:d2];
-
-
-
}
-
-
-
void fun2(){
-
//格式化
-
NSDate *d1 = [NSDate date];
-
NSDateFormatter *d2 = [[[NSDateFormatter alloc]init]autorelease];
-
-
//给他格式
-
//2000-00-00 00:00:00
-
//HH24进制 hh12进制
-
d2.dateFormat = @"yyyy-MM-dd HH:mm:ss";
-
//设置时区
-
d2.locale =[[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"]autorelease];
-
-
-
//赋值 string---date
-
NSString *s=[d2 stringFromDate:d1];
-
-
//date -- string
-
NSDate *d3 = [d2 dateFromString:@"2015-4-15 09:26:01"];
-
-
NSLog(@"%@",s);
-
NSLog(@"%@",d3);
-
-
}
-
-
int main(int argc, const char * argv[]) {
-
@autoreleasepool {
-
-
//fun();
-
//fun1();
-
fun2();
-
-
-
}
-
return 0;
-
}
阅读(667) | 评论(0) | 转发(0) |