Object-C 数组 字典 集合

#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
    @autoreleasepool {
    
	int a = 0;
        //字符串
        //1. 创建方法 初始化方法
        NSString * str = [[NSString alloc] initWithFormat:@"%d随便填啊aiuoe%d",a,a];
        //构造方法
//        NSString * str1 = [NSString stringWithFormat:@"是一个构造方法"];
//        NSLog(@"%@,%@",str,str1);
//        //字符串长度
//        NSLog(@"%lu",[str length]);
//        //字符串拼接
//        NSString * strAppend = [str stringByAppendingFormat:@"加上的字符串%d",a];
//        //stringByAppendingString不可以加%d等参数,stringByAppendingFormat可以
//        NSString * strAppend1 = [str stringByAppendingString:@"也是加上的字符串"];
//        NSString * strAppend2 = [str stringByAppendingString:str1];
//        NSLog(@"%@,%@,%@",strAppend,strAppend1,strAppend2);
        //字符求子串
//        NSString * strSubstring = [str substringFromIndex:1];
//        NSLog(@"%@",strSubstring);
//        NSString * strSubstring1 = [str substringToIndex:2];
//        NSLog(@"%@",strSubstring1);
        NSRange range = {2,4};//从第2个开始,一起4//        NSString * strSubstring2 = [str substringWithRange:range];
//        NSLog(@"%@",strSubstring2);
        //字符串替换
        NSString * replaceStr = [str stringByReplacingOccurrencesOfString:@"0" withString:@""];
        NSLog(@"%@",replaceStr);
        NSString * replaceStr1 = [str stringByReplacingCharactersInRange:range withString:@"111"];
        NSLog(@"%@",replaceStr1);
        //字符串转换为其他类型
        NSString * strNum = [NSString stringWithFormat:@"12as3"];
        int num = [strNum intValue];
        NSLog(@"%@ 转换为 %d",strNum,num);
        //字符串比较
        //判断字符串是否相等
//        BOOL b = [str isEqualToString:strNum];
        //判断字符串之间的大小(枚举值:升序为 -1,相等为 0,降序为1)
        [strNum compare:str];
        
        
        //可变字符串(Capacity为扩容的依据)
        //可以使用自己的初始化,也可以使用父类(NSString)的初始化方法
        //NSMutableString * mutableStr = [[NSMutableString alloc] initWithCapacity:1];
        NSMutableString * mutableStr = [[NSMutableString alloc] initWithFormat:@"赵云"];
        [mutableStr appendString:@"刘建"];
        NSLog(@"%@",mutableStr);
        //数组
        NSArray * arr = [[NSArray alloc] initWithObjects:@"aa",@"bb",@"cc", nil];
        NSLog(@"%@",arr);
        NSLog(@"数组元素的个数:%lu",[arr count]);
        
        //根据对象 获得这个对象在数组中的下标
        NSLog(@"%lu",[arr indexOfObject:@"bb"]);
        //数组增加一个元素(一次只能增加一个元素)
        NSArray * arrAdd = [arr arrayByAddingObject:@"dd"];
        NSLog(@"%@",arrAdd);
        
        //可变数组
        NSMutableArray * mutableArr = [[NSMutableArray alloc] initWithObjects:@"11",@"55",@"66", nil];
        NSLog(@"%@",mutableArr);
        //可变数组增加元素  是在自身的基础上添加
        [mutableArr addObject:@"77"];
        [mutableArr addObject:@"77"];
        [mutableArr removeObject:@"77"];//删除数组中所有77
        [mutableArr removeObjectAtIndex:1];
        [mutableArr insertObject:@"88" atIndex:2];
        NSLog(@"%@",mutableArr);
        
        //不可变字典(一定是2的倍数个)
        NSDictionary * dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"anan'body",@"anan",@"value",@"key", nil];
        NSLog(@"%@",dic);
        
        NSLog(@"%lu",[dic count]);
        NSLog(@"%@",[dic objectForKey:@"anan"]);
        NSLog(@"%@",[dic allKeys]);
        
        NSDictionary * dicS = [NSDictionary dictionaryWithObjectsAndKeys:@"aa",@"AA",@"bb"@"BB", nil];
        
        for (NSString * key in dicS) {
            NSString * str = [dicS objectForKey:key];
            NSLog(@"%@",str);
        }
        
        
        //可变字典
        NSMutableDictionary * mutableDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"anan's body",@"anan",@"value",@"key", nil];
        //添加
        [mutableDic setObject:@"another" forKey:@"one"];
        NSLog(@"%@",[mutableDic allKeys]);
        //删除
        [mutableDic removeObjectForKey:@"anan"];
        NSLog(@"%@",[mutableDic allKeys]);
        
        
        
        //集合(集合中不会出现相同的元素)
        
        NSSet * set = [[NSSet alloc]initWithObjects:@"11",@"11",@"55",@"66", nil];
        
        NSLog(@"%@",set);
        NSLog(@"%@",[set anyObject]);
        //取的所有集合中的元素放在一个数组里
        NSArray * ar = [set allObjects];
        NSLog(@"%@",ar);
        
        //可变集合
        
        NSMutableSet * set1 = [[NSMutableSet alloc]initWithObjects:@"11",@"11",@"55",@"66", nil];
        NSLog(@"%@",set1);
        
        
        NSArray * arrs = [NSArray arrayWithObjects:@"qq",@"ww",@"ee",@"ff", nil];
        
        for (int i = 0; i < [arrs count]; i++) {
            
            NSLog(@"元素为: %@",[arrs objectAtIndex:i]);
            
        }
        //对于这种内部元素类型一致的容器,推荐使用forin循环
        //1. 第一个参数:临时生成的对象
        //2. 第二个参数:容器对象
        for (NSString * strTemp in arrs) {
            NSLog(@"元素为: %@",strTemp);
        }
        
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值