Object-C Foundation 框架汇总之-数组,字典,集合

/* 字符串类 常用方法*/

        // 1. 创建字符串 (初始化方法)

        NSString *str2 = [[[NSString alloc]initWithString:str]autorelease];

        NSLog(@"%@", str2);

        NSString *str3 = [[[NSString alloc]initWithFormat:@"%@!%@!", str2, str2]autorelease];

        NSLog(@"%@", str3);

        

        // 2. 创建字符串 (便利构造器)

        NSString *m = [NSString stringWithString:str2];

        NSString *n = [NSString stringWithFormat:@"%@!%@!d%@kjf", str2, str2, str2];

        NSLog(@"%@", m);

        NSLog(@"%@", n);

        

        // 3. 获取字符串长度

        NSLog(@"%lu", [str length]);

        

        // 4. 判断字符串的是否以指定字符串开始或结束。

        // 应用案例:判断人名是否有姓

        NSString *name1 = @"谢霆锋";

        NSString *name2 = @"谢婷婷";

        if ([name1 hasPrefix:@""]) {

            NSLog(@"%@", name1);

        }

        if ([name1 hasPrefix:@"a"]) {

            NSLog(@"%@姓谢", name2);

        }

        

        BOOL str5 = [str hasPrefix:@""];

        NSLog(@"%d", str5);

        BOOL str6 = [str hasSuffix:@""];

        NSLog(@"%d", str6);

        

        // 5. 搜索字符串范围

        

        NSString *guo = @"今天老郭被黑了";

        NSRange range = [guo rangeOfString:@"老郭"];

        NSLog(@"%lu %lu", range.location, range.length);

        NSLog(@"%@", NSStringFromRange(range));

        

        // 6. 字符串截取

        NSString *nameKai = @"罗纳尔多";

        NSString *nameDa = @"贝克汉姆";

        // 练习:分别截取名字中的姓和名

        NSLog(@":%@", [nameKai substringToIndex:4]);

        NSLog(@":%@", [nameKai substringFromIndex:4]);

        NSLog(@"中间:%@",

              [nameKai substringWithRange:NSMakeRange(2, 4)]);

        NSLog(@":%@", [nameDa substringToIndex:2]);

        NSLog(@":%@", [nameDa substringFromIndex:2]);

        NSLog(@"中间:%@",

              [nameDa substringWithRange:NSMakeRange(1, 3)]);


        // 7.字符串拼接

        NSString *nameKaiDa = [[NSString alloc]

                               initWithFormat:@"%@%@", nameKai, nameDa];

        NSLog(@"%@", nameKaiDa);

        

        NSString *nameKaiDa1 = [nameKai stringByAppendingString:nameDa];

        NSLog(@"%@", nameKaiDa1);

        

        // 8. 字符串 -> 今天作业

        // 9. 字符串比较

        NSString *zuoShuHong = @"Zuo Shu Hong";

        NSString *shiBoLiang = @"Shi Bo Liang";

        NSLog(@"%ld", [zuoShuHong compare:shiBoLiang]);

        if (1 == [zuoShuHong compare:shiBoLiang]) {

            NSLog(@"Zuo Shu Hong > Shi Bo Liang");

        } else if (0 == [zuoShuHong compare:shiBoLiang]) {

            NSLog(@"Zuo Shu Hong == Shi Bo Liang");

        } else {

            NSLog(@"Zuo Shu Hong < Shi Bo Liang");

        }

        

        // 10.字符串与基本数据类型的转换

        // 数据类型->字符串

        // 例如:

        int b = 213;

        NSString *_213 = [NSString stringWithFormat:@"%d", b];

        NSLog(@"%@", _213);

        // 字符串 -> 基本数据类型

        // 例如:@“213” -> 213

        NSLog(@"%d", [_213 intValue]);

        

        /* 可变字符串 常用方法*/

        // 1. 创建字符串

        NSMutableString *mutableString1 = [NSMutableString

                                           stringWithString:zuoShuHong];

        NSLog(@"%@", mutableString1);

        

        // 2.字符串拼接

        [mutableString1 appendString:@"是一个好女孩"];

        NSLog(@"%@", mutableString1);

        

        // 3.字符串插入

        [mutableString1 insertString:@"同桌" atIndex:3];

        NSLog(@"%@", mutableString1);

        

        // 4.字符串替换

        [mutableString1 replaceOccurrencesOfString:@""

    withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [mutableString1 length])];

        NSLog(@"%@", m


      /* 数组类 */

        /* 不可变数组 NSArray 常用方法*/

        // 1.创建数组

        // 创建空数组

        NSArray *array1 = [[[NSArray alloc] init] autorelease];

        NSArray *array2 = [NSArray array];

        

        // 创建和某数组一样的数组

        NSArray *array3 = [NSArray arrayWithArray:array1];

        NSArray *array4 = [[NSArray alloc] initWithArray:array2];

        

        // 创建一个对象的数组

        NSArray *array5 = [NSArray arrayWithObject:@"1"];

        

        // 创建多个对象的数组

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

        

        NSLog(@"%@", array5);

        NSLog(@"%@", array6);

        

        // 2.获取数组元素

        NSLog(@"%@", array6[2]);

        NSLog(@"%@", [array6 objectAtIndex:2]);

        NSLog(@"%@", [array6 firstObject]);

        NSLog(@"%@", [array6 lastObject]);

        

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

        NSLog(@"%lu", [array6 indexOfObject:@"2"]);

        

        /* 可变数组 */

        NSMutableArray *classmate = [NSMutableArray arrayWithObjects:@"比卡丘",@"可达鸭",@"蜡笔小新",@"超级玛丽",@"樱木花道",@"樱桃小丸子",@"灰太狼",nil];

        // 插入

        [classmate insertObject:@"顾佳炜" atIndex:3];

        // 删除

        [classmate removeObject:@"比卡丘"];

        // 添加

        [classmate addObject:@"比卡丘"];

        // 交换

        [classmate exchangeObjectAtIndex:0 withObjectAtIndex:[classmate count] - 1];

        // 替换

        [classmate replaceObjectAtIndex:3 withObject:@"后跟"];

        // 排序

        [classmate sortUsingSelector:@selector(compare:)];

        // 输出

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

            NSLog(@"%d: %@", i + 1, [classmate objectAtIndex:i]);

        }


        // 数组的元素时自定义类的对象

        Person *p1 = [[[Person alloc]initWithName:@"大白兔"] autorelease];

        Person *p2 = [[[Person alloc]initWithName:@"小红帽"] autorelease];

        Person *p3 = [[[Person alloc]initWithName:@"狼外婆"] autorelease];

        NSArray *array = [NSArray arrayWithObjects:p1, p2, p3, nil];

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

            [[array objectAtIndex:i] sayHi];

        }

        

        /* 数值类 */

        // 1.基本数据类型->数值对象

        // char

        NSNumber *a = [[NSNumber alloc] initWithChar:'A'];

        // short

        NSNumber *b = [[NSNumber alloc] initWithShort:'a'];

        // int

        NSNumber *c = [[NSNumber alloc] initWithInt:5];

        // float

        NSNumber *d = [[NSNumber alloc] initWithFloat:2.13];

        // BOOL

        NSNumber *e = [[NSNumber alloc] initWithBool:1];

        

        NSMutableArray *arrNumber = [NSMutableArray arrayWithObjects:a, b, c, d, e, nil];

        NSLog(@"%@", arrNumber);

        

        // 2.数值对象, 取出相应的值

        

        char x = [a charValue];

        NSLog(@"%c", x);


        /* 不可变字典 NSDictionary 常用方法 */

// 1.创建字典对象


        NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"Jay", @"name", @"value:male",@"sex",@"value:35",@"age", nil];

        NSLog(@"%@", dictionary);


        // 2.已知key求对应的value

        NSString *name = [dictionary objectForKey:@"name"];

        NSString *sex = [dictionary objectForKey:@"sex"];

        NSString *age = [dictionary objectForKey:@"age"];

        NSLog(@"\nname = %@;\n sex = %@\n age = %@\n", name, sex, age);


        // 3.求所有的key

        NSLog(@"%@", [dictionary allKeys]);

        

        // 4.求所有的value

        NSLog(@"%@", [dictionary allValues]);


/* 可变字典 NSMutableDictionary 常用方法*/

        // 1.创建字典对象

        NSMutableDictionary *dictionary2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"Jolin", @"name", @"male",@"sex",@"32",@"age", nil];

        NSLog(@"%@", dictionary2);

        

        // 2.添加键值对

        [dictionary2 setValue:@"sing" forKey:@"hobby"];

        NSLog(@"%@", dictionary2);

        

        // 3.修改已有键的值

        [dictionary2 setValue:@"嘿嘿" forKey:@"name"];

        

        // 4.删除键值对

        [dictionary2 removeObjectForKey:@"name"];

        NSLog(@"%@", dictionary2);

        

        // 5.通过for循环遍历所有键值对

        NSLog(@"%@", [dictionary2 description]);


/* 集合类 NSSet 常用方法 */

        // 1.创建

        NSSet *class1 = [NSSet setWithArray:

                         classmate];

        // 2.获取元素个数

        NSLog(@"%lu", [class1 count]);

        

        // 3.判断集合中是否包含某个对象

        // @“小李飞刀

        BOOL abc = [class1 containsObject:@"小李飞刀"];

        NSLog(@"%d", abc);

        

        

        /* 可变集合 NSMutableSet */

        // 1.创建集合对象

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

        NSLog(@"%@", class); 


       // 2.添加元素

        [class addObject:@"ddd"];

        NSLog(@"%@", class);


        // 3.删除元素

        [class removeObject:@"bbb"];

        NSLog(@"%@", class);

        



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【优质项目推荐】 1、项目代码均经过严格本地测试,运行OK,确保功能稳定后才上传平台。可放心下载并立即投入使用,若遇到任何使用问题,随时欢迎私信反馈与沟通,博主会第一时间回复。 2、项目适用于计算机相关专业(如计科、信息安全、数据科学、人工智能、通信、物联网、自动化、电子信息等)的在校学生、专业教师,或企业员工,小白入门等都适用。 3、该项目不仅具有很高的学习借鉴价值,对于初学者来说,也是入门进阶的绝佳选择;当然也可以直接用于 毕设、课设、期末大作业或项目初期立项演示等。 3、开放创新:如果您有一定基础,且热爱探索钻研,可以在此代码基础上二次开发,进行修改、扩展,创造出属于自己的独特应用。 欢迎下载使用优质资源!欢迎借鉴使用,并欢迎学习交流,共同探索编程的无穷魅力! 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值