OC基础第四讲--字符串、数组、字典、集合的常用方法

字符串、数组、字典、集合有可变和不可变之分。以字符串为例,不可变字符串本身值不能改变,必须要用相应类型来接收返回值;而可变字符串调用相应地方法后,本身会改变;可变字符串是不可变字符串的子类。以下是常见的方法,其他方法可通过苹果帮助文档(API)查询使用。

1.1不可变字符串NSString

//  使用实例方法创建NSString对象

NSString *string1 = [[NSString alloc] initWithFormat:@"姓名41564564"];

NSLog(@"%@", string1);

NSString *string2 = [[NSString alloc] initWithFormat:@"机构名称: %@  成立时间: %d", @"蓝鸥", 2001];

NSLog(@"%@", string2);

//  使用类方法创建一个NNString对象

NSString *string3 = [NSString stringWithFormat:@"姓名 :%@ 年龄 :%d", @"小王", 20];

NSLog(@"%@", string3);

//  直接赋值

NSString *string4 = @"hello WORLD my baby";

NSLog(@"%@", string4);

//  获取字符串长度

NSUInteger string1Length = [string1 length];

NSLog(@"%ld", string1Length);

//  判断字符串是否有指定前缀

BOOL result1 = [string1 hasPrefix:@"姓名"];

NSLog(@"%d", result1);

//  后缀

BOOL result2 = [string2 hasSuffix:@"2011"];

NSLog(@"%d", result2);

//  查找字符串位置

NSRange range1 = [string2 rangeOfString:@"蓝鸥"];

NSLog(@"location : %ld  length : %ld", range1.location, range1.length);

//  分割字符串

NSString *subStirng1 = [string2 substringFromIndex:3];

NSLog(@"%@", subStirng1);

NSString *subString2 = [string2 substringToIndex:3];

NSLog(@"%@", subString2);

NSRange range2 = NSMakeRange(10, 2);// 建一个NSRange的结构体,注意越界

NSString *subString3 = [string2 substringWithRange:range2];

NSLog(@"%@", subString3);

//  拼接

NSString *string5 = [string2 stringByAppendingString: @"hello"];

NSLog(@"%@", string5);

//  替换

NSString *string6 = [string2 stringByReplacingOccurrencesOfString:@"蓝鸥" withString:@"海鸥"];

NSLog(@"%@", string6);

NSRange range3 = NSMakeRange(3, 3);

NSString *string7 = [string2 stringByReplacingCharactersInRange:range3 withString:@"HAHA 你中招了"];

NSLog(@"%@", string7);

//  比较

//  比较是否相同

BOOL isEqual = [string4 isEqualToString:@"hello"];

NSLog(@"%d", isEqual);

//  比较大小

NSComparisonResult result3 = [string2 compare:string4];

NSLog(@"%ld", result3);

//  大写<->小写

NSString *string8 = [string4 lowercaseString];

NSLog(@"%@", string8);

NSString *string9 = [string4 uppercaseString];

NSLog(@"%@", string9);

NSString *string10 = [string4 capitalizedString];

NSLog(@"%@", string10);

//  字符串和数值类型转换

NSInteger intStr = [@"124" integerValue];

NSLog(@"%ld", intStr);

double doubleStr = [@"1000.00432" doubleValue];

NSLog(@"%lf", doubleStr);

       1.2 可变字符串NSMutableString

//   创建可变字符串

NSMutableString *mutableStr1 = [[NSMutableString alloc] initWithFormat:@"蓝鸥科技有限公司"];

NSMutableString *mutableStr2 = [NSMutableString stringWithFormat:@"蓝鸥科技有限公司"];

NSLog(@"%@, %@", mutableStr1, mutableStr2);

//  字符串拼接(在原串的基础上)

[mutableStr1 appendString:@".txt"];

NSLog(@"%@", mutableStr1);

//  插入(在下标的后一位插入)

[mutableStr1 insertString:@"3G" atIndex:2];

NSLog(@"%@", mutableStr1);

//  删除

[mutableStr1 deleteCharactersInRange:NSMakeRange(2, 2)];

NSLog(@"%@", mutableStr1);

   2.1 不可变数组NSArray

NSArray数组类(不可变)

//  1.使用实例方法创建数组

NSArray *array1 = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", nil];//  nil不能省

NSLog(@"%@", array1); // 可以直接打印数组

//  2.使用类方法创建数组

NSArray *array2 = [NSArray arrayWithObjects:@"4", @"5", @"6", nil];

NSLog(@"%@", array2);

//  3.获取数组元素个数

NSUInteger count = [array1 count];

NSLog(@"%ld", count);

//  4.根据索引值获取对象(根据方法的返回值来确定用什么类型来接收)

NSString *p = [array1 objectAtIndex:1];

NSLog(@"%@", p);

//  5.获取对象在数组中的索引值

NSUInteger index = [array1 indexOfObject:@"3"];

NSLog(@"index : %ld", index);

1.2可变数组 NSMutableArray

//  1. 使用实例方法创建

NSMutableArray *mutableArray1 = [[NSMutableArray alloc] initWithObjects:@"aa",@"bb", @"cc", nil];

NSLog(@"%@", mutableArray1);

//  2. 使用类方法创建

NSMutableArray *mutableArray2 = [NSMutableArray arrayWithObjects:@"aaa",@"bbb", @"ccc", nil];

NSLog(@"%@", mutableArray2);

//  3. 添加元素

[mutableArray1 addObject:@"dd"];

NSLog(@"%@", mutableArray1);

//  4. 插入元素

[mutableArray1 insertObject:@"haha" atIndex:1];

NSLog(@"%@", mutableArray1);

//  5. 删除元素(通常根据索引或者对象值)

[mutableArray1 removeObject:@"haha"];

[mutableArray1 removeObjectAtIndex:1];

NSLog(@"%@", mutableArray1);

//  6. 替换元素

[mutableArray1 replaceObjectAtIndex:2 withObject:@"DD"];

NSLog(@"%@", mutableArray1);

//  7. 根据索引值交换指定位置的两个元素

[mutableArray1 exchangeObjectAtIndex:1 withObjectAtIndex:2];

NSLog(@"%@", mutableArray1);

//  8. 很据对象交换两个元素的位置(先找到对象的索引值)

[mutableArray1 exchangeObjectAtIndex: [mutableArray1 indexOfObject:@"cc"] withObjectAtIndex:[mutableArray1 indexOfObject:@"DD"]];

NSLog(@"%@", mutableArray1);

//  9. 遍历数组对象

使用for循环遍历

for (int i = 0; i < [mutableArray1 count]; i ++) {

NSString *str = [mutableArray1 objectAtIndex:i];

NSLog(@"遍历结果:%@", str);

}

使用for in快速遍历

for (NSString *str in mutableArray1) {

NSLog(@"快速遍历:%@", str);

}

数组中使用枚举器(for in 快速遍历的实质)

NSArray *arr = [NSArray arrayWithObjects:@"abc", @"hdf", @"hshah", nil];

NSEnumerator *enumerator2 = [arr objectEnumerator];

NSString *str2 = nil;

while (str2 = [enumerator2 nextObject]) {

NSLog(@"str2: %@", str2);

}

3.NSNumber (基本数据类型不能放在数组中,解决这个问题,可以把数字封装成对象)

//  1.创建一个数组对象

NSMutableArray *array = [NSMutableArrayarrayWithObjects:@"1", @"2", nil];

//  2.用NSNumber对Integer进行包装

NSNumber *intNumber = [NSNumber numberWithInteger:3];

//  3.将包装后烦人NSNumber对象放到数组中

[array addObject:intNumber];

NSLog(@"%@", array);

//  4.根据下标取出对象,用NSNumber类型进行接收

NSNumber *a = [array objectAtIndex:2];

NSLog(@"%@", a);

//  5.把NSNunber转成Integer类型

NSInteger integerA = [a integerValue];

NSLog(@"%ld", integerA);

4.1 NSDictionary 不可变字典

//  key通过哈希算法算出一个数作为一个索引下标把值存到相应的的位置在内存中是散列结构,即无序

//  使用实例方法创建(键值对的形式) 键和值都必须是对象

NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", @"v3", @"k3", @"v5", @"k5", @"v4", @"k4", @"value6", @"key6", @"value7", @"ke7", nil];

NSLog(@"%@", dictionary);

//  使用类方法创建

NSDictionary *dictionary2 = [NSDictionary dictionaryWithObjectsAndKeys:@"v1", @"k1", @"v2", @"k2", nil];

NSLog(@"%@", dictionary2);

NSDictionary *dictionary3 = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];

NSLog(@"%@", dictionary3);

NSArray *keyArray = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil]; //  存放键的数组

NSArray *valueArray = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil];//  存放值的数组

NSDictionary *dictionary4 = [NSDictionary dictionaryWithObjects: keyArray forKeys:valueArray];

NSLog(@"%@", dictionary4);

// 创建空字典

NSDictionary *dic = [NSDictionary dictionary];

//  使用一个文件创建字典对象 新建文件步骤:command + N -> Resource -> Property List

NSDictionary *dictionary5 = [NSDictionary dictionaryWithContentsOfFile:@"/Users/lanou3g/Desktop/OC/lesson5-20140425/lesson5-20140425/dictionary.plist"];

NSLog(@"%@", dictionary5);

//  返回字典中键值对的个数

NSLog(@"%ld", [dictionary5 count]);

//  字典取值 获取指定key对应的value

NSString *value1 = [dictionary5 objectForKey:@"ly"];

NSLog(@"%@", value1);

//  返回所有的key数组

NSArray *allKeysArray = [dictionary5 allKeys];

NSLog(@"%@", allKeysArray);

//  返回所有的value数组

NSArray *allValuesArray = [dictionary5 allValues];

NSLog(@"%@", allValuesArray);

//  使用key枚举器(获取所有key)

NSEnumerator *enumerator = [dictionary5 keyEnumerator];

NSString *str = nil;

while (str = [enumerator nextObject]) {

NSLog(@"%@", str);

}

 4.2创建可变字典 NSMutableDictionary

NSMutableDictionary *dic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"v1", @"k1", @"v2", @"k2", nil];

NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"v3", @"k3", @"v4", @"k4", @"v5", @"k5", nil];

//  用于整理对象的拼接

[dic1 addEntriesFromDictionary:dic2];

NSLog(@"%@", dic1);

//  删除字典中某个对象

[dic1 removeObjectForKey:@"k1"];

NSLog(@"%@", dic1);

//  删除字典全部对象

[dic1 removeAllObjects];

NSLog(@"%@", dic1);

//  设置字典

[dic1 setDictionary:dic2];

NSLog(@"%@", dic1);

 5.1NSSet集合对象 容器类

//  1. 使用类方法创建对象

NSSet *set1 = [NSSet set];  //  创建一个空的集合对象

NSSet *set2 = [NSSet setWithObject:@"abc"];

NSSet *set3 = [NSSet setWithObjects:@"abc", @"aaa", @"bbb", nil];

NSLog(@"%@", set3);

NSArray *array = [NSArray arrayWithObjects:@"a",@"b", @"c", nil];

NSSet *set4 = [NSSet setWithArray:array];   //  使用数组创建

NSLog(@"%@", set4);

NSSet *set5 = [NSSet setWithSet:set4];      //  使用集合创建

NSLog(@"%@", set5);

//  2.使用实例方法创建

NSSet *set6 = [[NSSet alloc] init];

NSLog(@"%@", set6);

NSSet *set7 = [[NSSet alloc] initWithObjects:@"hello", @"hhaa", @"bbjdh", nil];

NSLog(@"%@", set7);

NSSet *set8 = [[NSSet alloc] initWithArray:array];

NSLog(@"%@", set8);

NSSet *set9 = [[NSSet alloc] initWithSet:set7];

NSLog(@"%@", set9);

//  3.返回几个元素个数

NSLog(@"%ld", [set7 count]);

//  4.枚举器访问集合元素

NSEnumerator *enumerator = [set7 objectEnumerator];

NSString *str = nil;

while (str = [enumerator nextObject]) {

NSLog(@"%@", str);

}

//  5.判断两个几个是否有交集

BOOL ifhasIntersection = [set2 intersectsSet:set3];

NSLog(@"%d", ifhasIntersection);

//  6.判断两个集合是否相等

NSLog(@"%d", [set2 isEqualToSet:set3]);

//  7.判断当前集合是否是子集

NSLog(@"%d", [set2 isSubsetOfSet:set3]);

5.2可变集合 NSMutableSet

//  创建指定元素个数的一个集合对象

NSMutableSet *mutableSet = [NSMutableSet setWithCapacity:4];

[mutableSet addObject:@"aaa"];

NSLog(@"%@", mutableSet);

//  类方法创建可变集合

NSMutableSet *mutableSet1 = [NSMutableSet setWithObjects:@"aaa", @"bbb", @"ccc", nil];

NSMutableSet *mutableSet2 = [NSMutableSet setWithObject:@"aaa"];

//  添加一个对象到集合

[mutableSet2 addObject:@"ddd"];

NSLog(@"%@", mutableSet2);

//  从集合中删除一个对象

[mutableSet2 removeObject:@"ddd"];

NSLog(@"%@", mutableSet2);

//  把数组对象添加到集合对象中

NSArray *arr = [NSArray arrayWithObjects:@"eee", @"fff", nil];

[mutableSet1 addObjectsFromArray:arr];

NSLog(@"%@", mutableSet1);

//  得到两个集合的交集 注意intersectSet和intersectsSet的区别,后者是判断是否有交集的方法, 返回的是bool值

[mutableSet1 intersectSet:mutableSet2];

NSLog(@"%@", mutableSet1);

//  从一个集合中减去另一个集合

[mutableSet1 minusSet:mutableSet2];

NSLog(@"%@", mutableSet1);

//  从一个元素中删除所有元素

[mutableSet2 removeAllObjects];

NSLog(@"%@", mutableSet2);

//  取两个集合的并集

[mutableSet1 unionSet:mutableSet2];

NSLog(@"%@", mutableSet1);

NSLog(@"%@", mutableSet1);

//  设置给集合赋值

[mutableSet1 setSet:mutableSet2];

NSLog(@"%@", mutableSet1);

关于数组排序

NSArray *array = [NSArrayarrayWithObjects:@"12", @"454", @"7878", @"77122", @"555", @"9", nil];

NSLog(@"排序前:%@", array);

//  这样排序是按照字符串大小来排序的不是数值排序

array = [array sortedArrayUsingSelector:@selector(compare:)];

NSLog(@"排序后:%@", array);

//  进行数值排序需要用以下的方法

array = [array sortedArrayUsingFunction:intSort context:NULL];

NSLog(@"排序后:%@", array);

以下是intSort 的方法实现:

NSInteger intSort(id num1, id num2, void *context)

{

int v1 = [num1 intValue];

int v2 = [num2 intValue];

if (v1 < v2)

returnNSOrderedAscending;

else if (v1 > v2)

returnNSOrderedDescending;

else

returnNSOrderedSame;

}

关于字典与数组

/// 字典里可以存数组,数组可以存字典

NSArray *arr = [NSArray arrayWithObjects: @"1", @"2", @"3", nil];

//  把数组arr放在字典dic里

NSDictionary *dic = [NSDictionary dictionaryWithObject:arr forKey:@"array"];

NSLog(@"%@", dic);

//  把字典dic和数组arr 放在数组arr2里

NSArray *arr2 = [NSArray arrayWithObjects:arr, dic, nil];

NSLog(@"%@", arr2);

posted on 2014-04-26 20:31 和果子 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/liuyu521/p/3692029.html

OC基础第四讲--字符串、数组、字典、集合的常用方法相关推荐

  1. Javascript基础与面向对象基础~第四讲 Javascript中的类对象

    今天来说JS中如何实现类(class),事实上本应该昨天晚上写的,可我失言了,在些说一声"抱歉"!JS中的类是JS面向对象的基础,也是我最拿手的东西,你写的代码能否提高一个层次,一 ...

  2. c语言字符串字典序,字符串排序数组C语言 C语言 字符串数组字典排序

    C语言 字符串数组字典排序 #include #include int main() { int j,k,i,t,n; char s[10][10],b[10][10]; for(i=0;i c语言, ...

  3. 【Java基础】前端传一个数组或者集合后台怎么接受(案例详解)

    [辰兮要努力]:hello你好我是辰兮,很高兴你能来阅读,昵称是希望自己能不断精进,向着优秀程序员前行! 博客来源于项目以及编程中遇到的问题总结,偶尔会有读书分享,我会陆续更新Java前端.后台.数据 ...

  4. 小白都能学会的Python基础 第四讲:Python函数与模块

    1.华小智系列 - Python基础(案例版) <Python基础>目录 第四讲:Python函数与模块 1.函数的定义与调用 2.函数参数与返回值 2.1 参数再研究 2.2 返回值 2 ...

  5. Java字符串数组转集合方法

    数组转集合方法 使用 Arrays.asList(数组) 代码如下: //定义一个字符串String zhuan = "1,2,3,4,5,6,7,8,9";//分割字符串Stri ...

  6. [转载] python字符串数组字典_Python:字符串、列表、元组、字典

    参考链接: Python字符串| ascii_uppercase 字符串: 所有方法都修改不了字符串的值,字符串还是原值:但可以重新赋值:使用字符串方法有返回值 循环字符串的用法: 字符串方法: Na ...

  7. JavaScript基础(四)字符串操作

    字符串操作 length.charAt.fromCharCode 小案例:验证QQ号 字符串拼接.查询索引 字符串截取 slice substring 案例-查找关键字高亮显示 length.char ...

  8. 线代基础第四讲——方程组

    4.1线性方程组基础知识结构 主要任务就是求解方程组 4.2线性方程组和向量组其实是一回事 aij所组成的矩阵 m行就是所给方程的个数,n列就是未知量的个数. 增广矩阵的定义: 这里将向量组和方程组做 ...

  9. HTML基础第四讲---图像

    转自:https://blog.csdn.net/likaier/article/details/326735 图像,也就是images,在html语法中用img来表示,其基本的语法是:    < ...

最新文章

  1. PAT 1015__部分正确__已解决
  2. 20155117 王震宇 2006-2007-2 《Java程序设计》第三周学习总结
  3. 计算机网络【10】—— Cookie与Session
  4. 【数学和算法】协方差矩阵、方差
  5. SpringBoot配置swagger2(亲测有效,如果没有配置成功,欢迎在下方留言)
  6. Python标准库 urllib
  7. fortran快速入门
  8. 只要一页纸,就能管好所有项目
  9. 图像处理一之-摄像头二值化处理-(什么是二值化)
  10. Reviewboard使用介绍
  11. 为什么let this=that
  12. xp重启计算机的快捷键,xp电脑关机重启快捷键如何使用
  13. 计算机桌面视频录制,电脑上怎么录制屏幕上的视频?
  14. J.U.C之AQS:CLH同步队列
  15. 什么是淘宝店铺SKU
  16. 我那个工资3000的朋友,一年存了60万:聪明人从不靠死工资续命
  17. C++优化之使用emplace、emplace_back
  18. 全国翻译资格考试 介绍
  19. DS1307时钟模块
  20. sql 查询_嵌套查询

热门文章

  1. 裸centos安装PCRE时报错解决
  2. 谈Linux的安全设置
  3. iOS下拉tableView实现上面的图片放大效果
  4. android之android.intent.category.DEFAULT的用途和使用
  5. 《深度学习:Java语言实现》一一2.6小结
  6. eclipse中如何将java项目转为java Web项目
  7. UIButton 的不同设置和UITextField 的默认值(修改默认值)
  8. 查询在应用程序运行得很慢, 但在SSMS运行得很快的原因探究
  9. android读取大图片并缓存
  10. 设计模式读书笔记-单件模式