Objective-C 数据类简介
来源:互联网 发布:淘宝服务市场无线端 编辑:程序博客网 时间:2024/06/05 11:09
Objective-C 数据类简介
首先申明下,本文为笔者学习《Objective-C 基础教程》的笔记,并加入笔者自己的理解和归纳总结。
1、字符串类
NSString是不可变的字符串,而NSMutableString是可变的,可以对它进行各种操作。NSMutableString是NSString的子类,可以调用NSString的方法。
(1) NSString类
@符号和双引号可以直接定义一个字符串。
stringWithFormat方法可以输出一个字符串。加号(+)表明是一个类方法。
+ (NSString*) stringWithFormat: (NSString*) format, ...;length:返回字符串中的字符个数。
- (NSInteger) length;isEqualToString:比较两个字符串,返回BOOL值(YES或NO)。
- (BOOL) isEqualToString: (NSString*) aString;compare:比较字符串,返回枚举类型NSComparisonResult(NSOrderedAscending、NSOrderedSame和NSOrderedDescending)。
- (NSComparisonResult) compare: (NSString*) string;compare可以不区分大小写。
options参数是一个掩位码。
NSCaseInsensitiveSearch:不区分大小写。
NSLiteralSearch:进行完全比较,区分大小写。
NSNumericSearch:比较字符串的字符个数,而不是字符串值。
- (NSComparisonResult) compare: (NSString*) stringoptions: (NSStringCompareOptions) mask;hasPrefix:是否以一个字符串开头
- (BOOL) hasPrefix: (NSString*) str;hasSuffix:是否以一个字符串结尾
- (BOOL) hasSuffix: (NSString*) str;rangeOfString:是否包含其他字符串
- (NSRange) rangeOfString: (NSString*) searchString;componentsSeparatedByString;切分字符串
- (NSArray*) componentsSeparatedByString: (NSString*) separator;stringByExpandingTildeInPath:将路径展开为全路径
- (NSString*) stringByExpandingTildeInPath;简单例子
int main(int argc, const char* argv[]) {NSString* string1 = @"Hello World";NSString* string2 = [NSString stringWithFormat: @"Hello World"];NSString* string3 = @"hello world";NSString* string4 = @"99";NSString* string5 = @"100";NSLog(@"%@ len is %lu", string1, [string1 length]);if ([string1 isEqualToString: string2]) {NSLog(@"equalToString");}if ([string1 compare: string2] == NSOrderedSame) {NSLog(@"same");}NSLog(@"compre result = %d", [string4 compare: string5]);NSLog(@"CaseInsensitive compre result = %d", [string1 compare: string3 options: NSCaseInsensitiveSearch]);NSLog(@"Numeric compre result = %d", [string4 compare: string5 options: NSNumericSearch]);if ([string3 hasPrefix: @"hello"]) {NSLog(@"hasPrefix");}if ([string3 hasSuffix: @"world"]) {NSLog(@"hasSuffix");}return 0;}(2) NSMutableString类
stringWithCapicity:创建一个新NSMutableString
+ (NSMutableString*) stringWithCapacity: (NSUInteger)capacity;appendString:添加新字符串
appendFormat:添加新格式化字符串
- (void) appendString: (NSString*)aString;- (void) appendFormat: (NSString*)format, ...;deleteCharactersInRange:删除字符串中的字符
- (void) deleteCharactersInRange: (NSRange)range简单例子
int main(int argc, const char* argv[]) {NSMutableString* string1 = [NSMutableString stringWithCapacity: 17];[string1 appendString: @"Hello World "];[string1 appendFormat: @"%@", @"Welcome to Objective-C"];NSLog(@"%@", string1);NSRange range = [string1 rangeOfString: @"World"];if (range.location != NSNotFound) {[string1 deleteCharactersInRange: range];}NSLog(@"%@", string1);return 0;}
2、数组
NSArray是不可变数组,同样NSMutableArray是可变数组。两种关系如同NSString和NSMutableString。NSArray只能存储对象,不能存储基础数据(如int, float等),也不能存储nil(nil是结束标记)。(1) NSArray类
@符号直接创建一个数组。
arrayWithObjects方法创建数组。
+ (NSArray*) arrayWithObjects:...;count:数组对象个数
- (NSUInteger) count;objectAtIndex:获取特点索引处的对象,也可以通过中括号[]访问。
- (id) objectAtIndex: (NSUInteger) index;componentsJoinedByString合并NSArray中的元素,创建字符串。
- (NSString*) componentsJoinedByString: (NSString*) seperator;简单例子
int main(int argc, const char* argv[]) {NSArray* array1 = @[@"one", @"two", @"three", @"four"];NSArray* array2 = [NSArray arrayWithObjects:@"one",@"two", @"three", @"four", nil];for (int index = 0; index < [array1 count]; index++) {NSLog(@"%@", [array1 objectAtIndex: index]);}for (int index = 0; index < [array2 count]; index++) {NSLog(@"%@", array2[index]);}NSLog(@"%@", [array1 componentsJoinedByString: @"--"]);return 0;}(2) 可变数组NSMutableArray
arrayWithCapacity创建可变数组
+ (id) arrayWithCapacity: (NSUInteger) numItems;addObject:在数组末尾添加对象
- (void) addObject: (id) anObject;removeObjectAtIndex:删除指定索引对象
- (void) removeObjectAtIndex: (NSUInteger) index;简单例子
int main(int argc, const char* argv[]) {NSMutableArray* array = [NSMutableArray arrayWithCapacity: 17];[array addObject: @"one"];[array addObject: @"two"];[array addObject: @"three"];for (int index = 0; index < [array count]; index++) {NSLog(@"%@", [array objectAtIndex: index]);}[array removeObjectAtIndex: 1];for (int index = 0; index < [array count]; index++) {NSLog(@"%@", [array objectAtIndex: index]);}return 0;}
3、枚举
(1) NSEnumerator类NSArray对数组中每个元素执行相同操作,可以使用循环读取每个索引处的对象,也可以使用枚举NSEnumerator。
- (NSEnumerator*) objectEnumerator;nextObject:读取下一个对象
- (id) nextObject;in关键字可以快速遍历数组的每一个元素
for (id obj in array)简单例子
int main(int argc, const char* argv[]) {NSArray* array1 = @[@"one", @"two", @"three", @"four"];NSEnumerator* enumerator = [array1 objectEnumerator];id string;while(string = [enumerator nextObject]) {NSLog(@"%@", string);}for (NSString* string in array1) {NSLog(@"%@", string);}return 0;}
4、字典
NSDictionary类是不可变字典,同样NSMutableDictionary是可变字典。两种关系如同NSString和NSMutableString。(1) NSDictionary类
@符号直接创建字典。key在前,value在后。
@{key:value, ...};dictionaryWithObjectsAndKeys创建字典。value在前,key在后。
+ (id) dictionaryWithObjectsAndKeys: (id) firstObject, ...;objectForKey:读取指定关键字对象,也可以通过中括号[]访问。
- (id) objectForKey: (id) aKey;简单例子
int main(int argc, const char* argv[]) {NSDictionary* dict1 = @{@"Mike":@"Mike Jordan", @"Tom":@"Tom Lee"};NSDictionary* dict2 = [NSDictionary dictionaryWithObjectsAndKeys:@"Mike Jordan", @"Mike", @"Tom Lee", @"Tom", nil];NSLog(@"%@", [dict1 objectForKey: @"Mike"]);NSLog(@"%@", dict2[@"Tom"]);for (id key in dict1) {NSLog(@"%@ : %@", key, dict1[key]);}return 0;}(2) NSMutableDictionary类
dictionaryWithCapacity创建可变字典
+ (id) dictionaryWithCapacity: (NSUInteger) numItems;setObject:为字典添加元素。
- (void) setObject: (id) anObject forKey:(id) aKey;removeObjectForKey:删除关键字。
- (void) removeObjectForKey: (id) aKey;简单例子
int main(int argc, const char* argv[]) {NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithCapacity: 17];[dict setObject: @"Mike Jordan" forKey: @"Mike"];[dict setObject: @"Tom Lee" forKey: @"Tom"];for (id key in dict) {NSLog(@"%@ : %@", key, dict[key]);}[dict removeObjectForKey: @"Tom"];for (id key in dict) {NSLog(@"%@ : %@", key, dict[key]);}return 0;}
5、NSNumber类
NSNumber类用来封装基本类型。+ (NSNumber*) numberWithChar: (char) value;+ (NSNumber*) numberWithInt: (int) value;+ (NSNumber*) numberWithFloat: (float) value;+ (NSNumber*) numberWithBool: (BOOL) value;也可以使用@符号创建
NSNumber* number;number = @'x'; // 字符型number = @1234; // 整型number = @1.23; // 浮点型number = @YES; // 布尔型基本数据封装在NSNumber中后,可以使用方法重新获取。
- (char) charValue;- (int) intValue;- (float) floatValue;- (BOOL) boolValue;
6、NSDate类
date返回当前时间+ (id) date;dateWithTimeIntervalSinceNow返回距离当前时间一段时差的时间。
+ (id) dateWithTimeIntervalSinceNow: (int) sec;initWithTimeInterval:sinceDate返回距离指定时间一段时差的时间。
- (id) initWithTimeInterval: (int)sec sinceDate: (NSDate)date;laterDate用于和另一时间相比,是否更晚。
- (BOOL) laterDate: (NSDate) date;earlierDate用于和另一时间相比,是否更早。
- (BOOL) earlierDate: (NSDate) date;NSDateFormatter用来格式化NSDate
- (void) setDateFormat: (NSString*) format; // 指定格式- (NSString) stringFromDate: (NSDate*) date; // 格式化输出简单例子
int main(int argc, const char* argv[]) {NSDate* currentDate = [NSDate date];NSDate* earlierDate = [NSDate dateWithTimeIntervalSinceNow: -60];NSDate* laterDate = [[NSDate alloc] initWithTimeInterval: 60sinceDate: currentDate];if ([currentDate laterDate: laterDate]) {NSLog(@"laterDate");}if ([currentDate earlierDate: earlierDate]) {NSLog(@"earlierDate");}NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];[dateFormatter setDateFormat: @"YYYY-MM-dd hh:mm:ss"];NSLog(@"%@", [dateFormatter stringFromDate: currentDate]);}
7、NSNull类
NSArray和NSDictionary中不能存放nil值,使用NSNull表示没有值。null方法创建NSNull。
+ (NSNUll*) null;
阅读全文
0 0
- Objective-C 数据类简介
- Objective-C 数据类总结
- Objective-C 类集合简介(文档)
- Objective-C简介
- Objective-C学习简介
- 简介Objective-C语言
- Objective-C简介
- 简介Objective-C语言
- Objective-C简介
- 简介Objective-C语言
- [Objective-C] selector 简介
- [Objective-C] Protocol 简介
- Objective-C入门简介
- Objective-C简介
- Objective-C简介
- 1.Objective-C简介
- Objective-C简介
- 1、Objective-C简介
- 适配器模式(Adapter)(对象结构型模式)
- recycleview万能分割线
- Kotlin学习笔记——标准库kotlin.collections
- 开机自动打开浏览器并定时重启
- 空气净化器工作原理
- Objective-C 数据类简介
- 美团外卖、饿了么竞品分析
- bingfa
- 收集一些不错的GitHub轮子
- 安卓studio环境错误,无法打开
- SolrCloud原理介绍
- 在一个循环有序数组里查找一个给定的值是否存在
- 内存泄漏的问题之av_bitstream_filter_filter
- java 定时任务 Timer TimerTask