<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];
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的值
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];
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
}];CGPoint p = CGPointMake(10, 10); NSValue* v = [NSValue valueWithPoint:p] CGPoint pt = [v pointValue];
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]; ...
<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];
原文:http://blog.csdn.net/xufeng0991/article/details/43435383