码迷,mamicode.com
首页 > 移动开发 > 详细

iOS复习笔记14:常用数据结构之类

时间:2015-02-05 07:03:55      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:objective-c   ios   常用类   

一 NSString/NSMutableString字符串
1 NSString
<pre name="code" class="objc">NSString* s1 = @"string";
//NSString* s2 = [[NSString alloc] initWithFormat(@"%d is one",1)];
NSString* s2 = [NSString stringWithFormat(@"%d is one",1)];


// C语言字符串和OC字符串相互转换
// NSString* os = [[NSString alloc] initWithUTF8String:"C String"];
NSString* os = [NSString stringWithUTF8String:"C String"];
const char* cs = [os UTF8String]; 



// 使用文件内容初始化字符串
// NSString* s3 = [[NSString alloc] initWithContentsOfFile:@"文件绝对路径"
//											  encoding:NSUTF8StringEncoding // 中文编码
//											  error:nil];
NSString* s3 = [NSString stringWithContentsOfFile:@"文件绝对路径"
										 encoding:NSUTF8StringEncoding // 中文编码
										    error:nil];



// URL:///资源路径
// 协议头:///资源路径
// file:///资源路径
// ftp:///资源路径
// NSURL* url = [[NSURL alloc] initWithString:@"file://路径"];
NSURL* url = [NSURL urlWithString:@"file://路径"];
// NSString* s6 = [[NSString alloc] initWithContentsOfURL:url
//											  encoding:NSUTF8StringEncoding // 中文编码
//											  error:nil];
NSString* s6 = [NSString stringWithContentsOfURL:url
										encoding:NSUTF8StringEncoding // 中文编码
										   error:nil];



// 字符串分割
NSArray* arr = [@"one|two|three|four|five" componentsSeparatedByString:@"|" ]


// 写入文件
[s1 writeToFile:@"文件路径"
	atomiccally:YES
	   encoding:NSUTF8StringEncoding
	      error:nil];


[s1 writeToURL:url
	atomiccally:YES
	   encoding:NSUTF8StringEncoding
	      error:nil];


NSString* strNum = @"10";
int n = [strNum intValue];



2 NSMutableStringNSString的子类
NSMutableString* ms1 = [NSMutableString stringWithFormat:@"my age is 10"];
// 添加字符串
[ms1 appendString:@" or 13"];// 无返回值,修改ms1本身的值
// 删除字符串
NSRange rg = [s1 rangOfString:@"age"];
[ms1 deleteCharactersInRange:rg];


NSString* ns1 = @"my age ";
NSString* ns2 = [ns1 stringByAppendingString:@"is 10"];// 返回新的字符串,不修改ns1的值




二 集合类
1 NSArray/NSMutableArray数组
 OC数组只能存放OC对象,不能存放基础数据类型和nil

NSArray:不可变数组
NSMutableArray:可变数组


// 数组创建
NSArray* arr1 = [NSArray array];
NSArray* arr2 = [arrayWithObject:@"name"];
// nil 数组元素结束标志
NSArray* arr3 = [arrayWithObjects:@"name", @"id", nil];
NSArray* arr4 = @[@"one",@"two",@"three",@"four"];


// 数组元素个数
unsigned long count1 = [arr3 count];
unsigned long count2 = arr3.count;


// 数组元素访问
NSString* e1 = [arr3 objectAtIndex:1];
NSString* e2 = arr3[0];


// 数组遍历
for (int i = 0; i < arr4.count; ++i)
{
	arr4[i];
}


for (id obj in arr4)
{
	obj;
    [arr4 indexOfObject:obj];// 获取改数组的下标
}


[arr4 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
	if (idx == 1){
		*stop = YES;// 跳出遍历
	}
}];


// 可变数组
NSMutableArray* marr = [[NSMutableArray alloc] init];;
[marr addObject:@"one"];
[marr addObject:@"two"];
[marr addObject:@"three"];


// 可变数组-删除
[marr removeObject:@"one"];
[marr removeObjectAtIndex:0];
[marr removeAllObjects];



2 NSSet/NSMutableSet集合
只能存放OC对象,不能存放基础数据类型和存放nil
NSSet* st1 = [NSSet set];
NSSet* st2 = [NSSet setWithObject:@"one"];
NSSet* st3 = [NSSet setWithObjects:@"one", @"two", nil];


// 随机拿出一个对象
NSString* ssobj = [st3 anyObject];


NSMutableSet* mst = [NSMutableSet set];
[mst addObject:@"three"];
// 删除
[mst removeObject:@"one"];
[mst removeAllObjects];


3 NSDictionary/NSMutableDictionary字典
// 创建
NSArray* keys = @[@"name", @"address"];
NSArray* objs = @[@"jun", @"china"];
NSDictionary* dict1 = [NSDictionary dictionaryWithObjests:objs forKeys:keys];
NSDictionary* dict2 = [NSDictionary dictionaryWithObjestsAndKeys:
						@"name",@"jun",
						@"address",@"china", nil];


NSDictionary* dict3 = @{@"name":@"jun",@"address":@"china"};


// 可变字典
NSMutableDictionary* msdict = [NSDictionary dictionary];


// 添加元素
[dict1 setObject:@"jun" forKey:@"name"];
[dict1 setObject:@"hanfeng" forKey:@"name"];// 覆盖之前的值


// 移除
[dict1 removeObjectForKey:@"name"];


// 访问
id obj1 = [dict1 objectForKey:@"name"];
id obj2 = dict1{@"name"}


// 获取键值对的个数
int count = dict1.count


// 获取所有的key值,然后遍历这个数组
NSArray* ks = [dict1 allKeys]
for (int i = 0; i < dict1.count; ++i)
{
	id k = keys[i];
	id v = dict1[k];//[dict1 objectForKey:k];
}


[dict1 enumerateKeysAndObjectsUsingBlock:^(id key,id obj, BOOL *stop){
	// key ,obj
}];


上面的三个类(NSArray, NSSet, NSDictionary)称之为集合类
NSArray/NSMutableArray
* 有序
* 快速创建:@[obj1,obj2,...]
* 快速访问:数组名[i]


NSSet/NSMutableSet
* 无序


NSDictionary/NSMutableDictionary
* 无序
* 快速创建:@{key1:value1,key2:value2,...}
* 快速访问:字典名{key}


三 NSValue/NSNumber数字
1 NSValue
将其他数据类型(常见结构体)包装成OC对象, 这样就可以放入OC数组和字典中了。
CGPoint p = CGPointMake(10, 10);
NSValue* v = [NSValue valueWithPoint:p]
CGPoint pt = [v pointValue];



2 NSNumber
将基础数据类型(数字)装成对象,这样就可以放入OC数组和字典中了。
NSNumber num1 = [NSNumber numberWithInt:10];
NSNumber num2 = [NSNumber numberWithLong:10];
NSNumber num3 = [NSNumber numberWithBool:true];
NSNumber num4 = [NSNumber numberWithFloat:10.0];
NSNumber num4 = @10;// 快速转换
int count = 5;
NSNumber num5 = @(count);
...


int i = [num1 intValue];
int l = [num1 longValue];
int b = [num1 boolValue];
int f = [num1 floatValue];
...




四 NSDate日期
<pre name="code" class="objc">NSDate* d1 = [NSDate date];// 返回当前时间
NSLog(@"%@", d1);// 格林尼治时间时间(0时区),跟北京(东八区)时间差8个小时


NSDate* d2 = [NSDate dateWithTimeInterval:5 sinceDate:d1];


// 时间间隔
NSTimeInterval sec1 = [d2 timeIntervalSince1970];


NSTimeInterval sec2 = [d2 timeIntervalSinceNow];


// 日期格式化
NSDateFormatter* f1 = [[NSDateFormatter alloc]init];
// y年M月d日h(12小时制)时H(24小时制)时m分s秒
f1.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSString* dateStr = [f1 stringFromDate:d1];


NSString* timeStr = @"2015/02/04 16:57";
NSDateFormatter* f2 = [[NSDateFormatter alloc]init];
f2.dateFormat = @"yyyy/MM/dd HH:mm";


NSDate* d3 = [f2 dateFromString:timeStr];


iOS复习笔记14:常用数据结构之类

标签:objective-c   ios   常用类   

原文地址:http://blog.csdn.net/xufeng0991/article/details/43435383

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!