标签:
七 | 字符串与基本数据类型转换 | 获取字符串的每个字符/字符串和其他数据类型转换 |
八 | NSMutableString | 基本概念/常用方法 |
九 | NSArray | NSArray基本概念/创建方式/注意事项/常用方法简写形式/遍历/排序/文件读写/NSArray 与字符串 |
十 | NSMutableArray | 概念/基本用法/错误用法 |
十一 | NSDictionary | 基本概念/创建/遍历/文件操作 |
十二 | NSMutableArray | NSMutableDictionary 基本概念/常用方法/简写/NSDictionary和NSArray对比 |
十三 | OC中的结构体 | NSPoint和CGPoint/NSSize和CGSize/NSRect和CGRect/常见的结构体使用注意 |
十四 | NSNumber | NSNumber基本概念/创建/从NSNumber对象中的到基本类型数据 |
十五 | NSValue | NSValue基本概念/常见结构体的包装/任意数据的包装 |
十六 | NSDate | NSDate基本概念/格式化日期/日期时间对象 |
- (NSUInteger)length;
- (unichar)characterAtIndex:(NSUInteger)index;
NSString *str1 = @"110"; NSString *str2 = @"10"; int res = str1.intValue + str2.intValue; NSLog(@"res = %i", res); NSString *str1 = @"110"; NSString *str2 = @"10.1"; double res = str1.doubleValue + str2.doubleValue; NSLog(@"res = %f", res);
NSString *str = @"abc"; //用常量接收,所以用const const char *cStr = [str UTF8String]; NSLog(@"cStr = %s", cStr);
char *cStr = "abc"; NSString *str = [NSString stringWithUTF8String:cStr]; NSLog(@"str = %@", str);
NSMutableString类继承NSString类,那么NSString提?t?供的方法在NSMutableString中基本都可以使用,NSMutableString好比一个字符串链表,它可以任意的动态在字符串中添加字符串/删除字符串 /指定位置插入字符串,使用它来操作字符串会更加灵活。
NSMutableString和NSString的区别
// 改变了指针的指向, 并没有修改字符串 NSString *str = @"lnj"; str = @"lmj"; // 生成了一个新的字符串, 并没有修改字符串 NSString *newStr = [str substringFromIndex:1]; NSLog(@"str = %@", str); NSLog(@"newStr = %@", newStr);
//strM是空的字符串 NSMutableString *strM = [NSMutableString string]; NSLog(@"strM = %@", strM); // 修改原有字符串, 没有生成新的字符串 [strM appendString:@"lnj"]; NSLog(@"strM = %@", strM); [strM appendString:@" v587"]; NSLog(@"strM = %@", strM);
NSMutableString *strM = [NSMutableString string]; NSLog(@"strM = %@", strM); [strM appendString:@"lnj"]; NSLog(@"strM = %@", strM);
NSMutableString *strM = [NSMutableString string]; NSLog(@"strM = %@", strM); [strM appendFormat:@"lnj"]; NSLog(@"strM = %@", strM);
NSMutableString *strM = [NSMutableString stringWithString:@"http://www.520it.com"]; //查找http://的位置 NSRange range = [strM rangeOfString:@"http://"]; //删除查找到的http:// [strM deleteCharactersInRange:range]; NSLog(@"strM = %@", strM);
NSMutableString *strM = [NSMutableString stringWithString:@"www.520it.com"]; [strM insertString:@"http://" atIndex:0]; NSLog(@"strM = %@", strM);
NSMutableString *strM = [NSMutableString stringWithString:@"http://www.520it.com/hmj.png"]; NSRange range = [strM rangeOfString:@"hmj"]; [strM replaceOccurrencesOfString:@"hmj" withString:@"jjj" options:0 range:range]; NSLog(@"strM = %@", strM);
NSMutalbeString *s1 = @”hmj”; // 会报错 [strM insertString:@"my name is " atIndex:0];
将3个520it拼接在一起,中间用空格隔开
NSString *subStr = @"520it"; NSMutableString *strM = [NSMutableString string]; for (int i = 0; i < 3; ++i) { // 1.添加一个520it [strM appendString:subStr]; // 2.添加一个空格 [strM appendString:@" "]; } [strM deleteCharactersInRange:NSMakeRange(strM.length - 1, 1)]; NSLog(@"strM = |%@|", strM);
什么是NSArray?
NSArray的使用注意
+ (instancetype)arrayWithArray:(NSArray *)array;根据数组创建数组
+ (id)arrayWithContentsOfFile:(NSString *)path;
NSArray直接使用NSLog()作为字符串输出时是小括号括起来的形式。
NSArray中不能存储nil,因为NSArray认为nil是数组的结束(nil是数组元素结束的标记)。nil就是0。0也是基本数据类型,不能存放到NSArray中。
NSArray *arr = [NSArray arrayWithObjects:@"hmj", nil ,@"hyh",@"hxj", nil]; NSLog(@"%@", arr); 输出结果: ( hmj )
- (NSUInteger)count;
- (id)objectAtIndex:(NSUInteger)index;
- (BOOL)containsObject:(id)anObject;
- (id)lastObject;
- (id)firstObject;
- (NSUInteger)indexOfObject:(id)anObject;
- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range;
自从2012年开始,Xcode的编译器多了很多自动生成代码的功能,使得OC代码更加精简
数组的创建
+ 之前
[NSArray arrayWithObjects:@"Jack", @"Rose", @"Jim", nil];
+ 现在
@[@"Jack", @"Rose", @"Jim"];
+ 之前
[array objectAtIndex:0];
+ 现在
array[0];
NSArray *arr = @[p1, p2, p3, p4, p5]; for (int i = 0; i < arr.count; ++i) { Person *p = arr[i]; [p say]; }
NSArray *arr = @[p1, p2, p3, p4, p5]; for (Person *p in arr) { [p say]; }
[arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSLog(@"obj = %@, idx = %lu", obj, idx); Person *p = obj; [p say]; }];
// 让数组中所有对象执行这个方法 // 注意: 如果数组中的对象没有这个方法会报错 // [arr makeObjectsPerformSelector:@selector(say)]; [arr makeObjectsPerformSelector:@selector(eat:) withObject:@"bread"];
NSArray *arr = @[@(1), @(9), @(5), @(2)]; NSArray *arr = @[@1, @9, @5, @2]; NSArray *newArr = [arr sortedArrayUsingSelector:@selector(compare:)];
NSArray *arr = @[p1, p2, p3, p4, p5]; // 默认按照升序排序 NSArray *newArr = [arr sortedArrayWithOptions:NSSortConcurrent usingComparator:^NSComparisonResult(Person *obj1, Person *obj2) { // return obj1.age > obj2.age;//升序 return obj1.age < obj2.age;//降序 }]; NSLog(@"%@", newArr);
NSArray *arr = @[@"hmj", @"hyh", @"good"]; NSString *str = [arr componentsJoinedByString:@"*"]; NSLog(@"str = %@", str);
NSArray *arr = @"hmj**hyh**good"; NSString *str = [arr componentsSeparatedByString:@"**"]; NSLog(@"str = %@", str);
NSArray *arr = @[@"lnj", @"lmj", @"jjj", @"xcq"]; BOOL flag = [arr writeToFile:@"/Users/MJ-Hee/Desktop/persons.plist" atomically:YES]; NSLog(@"%i", flag);
NSArray *newArr = [NSArray arrayWithContentsOfFile:@"/Users/MJ-Hee/Desktop/persons.xml"]; NSLog(@"%@", newArr);
NSArray *arr = @[@"lnj", @"lmj", @"jjj", @"xcq"]; NSString *res = [arr componentsJoinedByString:@"*"]; NSLog(@"res = %@", res); 输出结果: lnj*lmj*jjj*xcq
NSString *str = @"lnj-lmj-jjj-xcq"; NSArray *arr = [str componentsSeparatedByString:@"-"]; NSLog(@"%@", arr); 输出结果: ( lnj, lmj, jjj, xcq )
NSMutableArray *arr = [NSMutableArray array];
NSMutableArray *arr2 = [[NSMutableArray alloc] initWithCapacity:5];
NSMutableArray *arr3 = [NSMutableArray arrayWithObjects:@"1",@"2", nil];
NSMutableArray *arr4 = [[NSMutableArray alloc] initWithObjects:@"1",@"2", nil];
- (void)addObject:(id)object;
- (void)addObjectsFromArray:(NSArray *)array;
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;
- (void)indexSetWithIndexesInRange:(NSRange *)range;
- (void)removeLastObject;
- (void)removeAllObjects;
- (void)removeObjectAtIndex:(NSUInteger)index;
- (void)removeObject:(id)object;
- (void)removeObjectsInRange:(NSRange)range;
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
- (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2;
NSMutableArray *array = @[@"hmj", @"hyh", @"hhh"]; // 报错, 本质还是不可变数组 [array addObject:@“Peter”];
+ \+ (instancetype)dictionary;
+ \+ (instancetype)dictionaryWithObject:(id)object forKey:(id <NSCopying>)key;
+ \+ (instancetype)dictionaryWithObjectsAndKeys:(id)firstObject, ...;
+ \+ (id)dictionaryWithContentsOfFile:(NSString *)path;
+ \+ (id)dictionaryWithContentsOfURL:(NSURL *)url;
+ \- (void)dictionaryWithObject:(NSString *) forKey:(NSString *);
* 单个数据
+ \- (void)dictionaryWithObjects:(NSArray *) forKey:(NSA rray *);
* 多个数据
+ 以前
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"lnj", @"name", @"12345678", @"phone", @"天朝", @"address", nil];
+ 现在
NSDictionary *dict = @{@"name":@"lnj", @"phone":@"12345678", @"address":@"天朝"};
+ 以前
[dict objectForKey:@"name”];
+ 现在
dict[@"name”];
- (NSUInteger)count;
- (id)objectForKey:(id)aKey;
如果获取字典中key和value的个数,键值对
for (int i = 0, i< dict.count, ++i){ //获取所有的key NSArray *keys = [dict allKeys]; //取出当前位置对应的key NSString *key = keys[i]; NSString *value = dict[key]; NSLog(@"key = %@, value = %@", key, value); } for (NSString *key in dict){ NSString *value = dict[key]; NSLog(@"key = %@, value = %@", key, value); } [dict enumrateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop){ NSLog(@"key = %@, value = %@", key, obj); }];
NSDictionary *dict = @{@"name":@"lnj", @"phone":@"12345678", @"address":@"天朝"}; for (NSString *key in dict) { NSLog(@"key = %@, value = %@", key, dict[key]); }
[dict enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) { NSLog(@"key = %@, value = %@", key, obj); }];
将字典写入文件中
示例
NSDictionary *dict = @{@"name":@"lnj", @"phone":@"12345678", @"address":@"天朝"}; BOOL flag = [dict writeToFile:@"/Users/LNJ/Desktop/dict.plist" atomically:YES]; NSLog(@"flag = %i", flag);
NSDictionary *newDict = [NSDictionary dictionaryWithContentsOfFile:@"/Users/LNJ/Desktop/dict.plist"]; NSLog(@"newDict = %@", newDict);
- (void)setObject:(id)anObject forKey:(id )aKey;
- (void)removeObjectForKey:(id)aKey;
- (void)removeAllObjects;
[dict setObject:@"Jack" forKey:@"name”];
dict[@"name"] = @"Jack";
NSArray和NSDictionary的区别
NSArray的用法
@[@"Jack", @"Rose"] (返回是不可变数组)
id d = array[1];
array[1] = @"jack";
NSDictionary的用法
@{ @"name" : @"Jack", @"phone" : @"10086" } (返回是不可变字典)
id d = dict[@"name"];
dict[@"name"] = @"jack";
添加
注意
typedef CGPoint NSPoint; CGPoint的定义 struct CGPoint { CGFloat x; CGFloat y; }; typedef struct CGPoint CGPoint; typedef double CGFloat;
typedef CGSize NSSize; CGSize的定义 struct CGSize { CGFloat width; CGFloat height; }; typedef struct CGSize CGSize;
typedef CGRect NSRect; CGRect的定义 struct CGRect { CGPoint origin; CGSize size; }; typedef struct CGRect CGRect;
NSArray\NSDictionary中只能存放OC对象,不能存放int\float\double等基本数据类
如果真想把基本数据(比如int)放进数组或字典中,需要先将基本数据类型包装成OC对象
NSNumber可以将基本数据类型包装成对象,这样就可以间接将基本数据类型存进NSArray\NSDictionary中
以前
现在
NSNumber是NSValue的子类, 但NSNumber只能包装数字类型
NSValue可以包装任意值
将结构体包装成NSValue对象
从NSValue对象取出之前包装的结构体
NSValue提供了下列方法来包装任意数据
从NSValue中取出所包装的数据
NSDate可以用来表示时间, 可以进行一些常见的日期\时间处理
一个NSDate对象就代表一个时间
[NSDate date]返回的就是当前时间(零时区的时间)
NSDate *now = [NSDate date]; NSLog(@"now = %@", now); // 设置转换后的目标日期时区 NSTimeZone *zone = [NSTimeZone systemTimeZone]; // 得到源日期与世界标准时间的偏移量 NSInteger interval = [zone secondsFromGMTForDate: date]; NSLog(@"interval = %lu", interval); // 在当前时间基础上追加时区差值 now = [now dateByAddingTimeInterval:interval]; NSLog(@"%@", date);
// 1.创建时间 NSDate *now = [NSDate date]; // 2.创建时间格式化 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; // 3.指定格式 formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss"; // 4.格式化时间 NSString *str = [formatter stringFromDate:now]; NSLog(@"%@", str);
NSString *str = @"2015-06-28 19:53:24"; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss"; NSDate *date = [formatter dateFromString:str]; NSLog(@"%@", date);
注意
获得NSCalendar对象
NSCalendar *calendar = [NSCalendar currentCalendar];
- (NSDateComponents *)components:(NSCalendarUnit)unitFlags fromDate:(NSDate *)date;
NSDate *date = [NSDate date]; // 1.创建日历对象 NSCalendar *calendar = [NSCalendar currentCalendar]; // 2.利用日历对象获取年月日时分秒 NSCalendarUnit type = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; NSDateComponents *cmps =[calendar components:type fromDate:date]; NSLog(@"year = %lu", cmps.year); NSLog(@"month = %lu", cmps.month); NSLog(@"day = %lu", cmps.day); NSLog(@"hour = %lu", cmps.hour); NSLog(@"minute = %lu", cmps.minute); NSLog(@"second = %lu", cmps.second); NSLog(@"date = %@", date);
// 1.确定时间 NSString *time1 = @"2015-06-23 12:18:15"; NSString *time2 = @"2015-06-28 10:10:10"; // 2.将时间转换为date NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss"; NSDate *date1 = [formatter dateFromString:time1]; NSDate *date2 = [formatter dateFromString:time2]; // 3.创建日历 NSCalendar *calendar = [NSCalendar currentCalendar]; NSCalendarUnit type = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; // 4.利用日历对象比较两个时间的差值 NSDateComponents *cmps = [calendar components:type fromDate:date1 toDate:date2 options:0]; // 5.输出结果 NSLog(@"两个时间相差%ld年%ld月%ld日%ld小时%ld分钟%ld秒", cmps.year, cmps.month, cmps.day, cmps.hour, cmps.minute, cmps.second);
标签:
原文地址:http://www.cnblogs.com/HMJ-29/p/4706204.html