OC字典 OC数组 OC字符串

来源:互联网 发布:java好学吗 编辑:程序博客网 时间:2024/05/19 14:16

OC字典

#import <Foundation/Foundation.h>

int main()
{
    //不可变字典对象的创建。
    //字典是无序的,
    //在创建字典对象的时候,键值对一定要成对出现,否则会崩溃。
    //字典里面key不允许重复。value可以重复。
    NSDictionary * dic1=[[NSDictionary alloc]initWithObjectsAndKeys:@"one",@"21",@"two",@"2",@"three",@"3",@"four",@"4", nil];
    NSLog(@"%@",dic1);
    NSDictionary * dic2=[[NSDictionary alloc]initWithDictionary:dic1];
    NSLog(@"%@",dic2);
    NSDictionary * dic3=[NSDictionary dictionaryWithObjectsAndKeys:@"one",@"21",@"two",@"2",@"three",@"3",@"four",@"4", nil];
    NSLog(@"%@",dic3);
    NSDictionary * dic4=[NSDictionary dictionaryWithDictionary:dic3];
    NSLog(@"%@",dic4);
    NSArray * keysArray=[NSArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e", nil];
    NSArray * valuesArray=[NSArray arrayWithObjects:@"one",@"two",@"three",@"four",@"five", nil];
    NSDictionary * dic5=[NSDictionary dictionaryWithObjects:valuesArray forKeys:keysArray];
    NSLog(@"%@",dic5);
    //获取字典中健值对的个数。
    NSLog(@"%lu",[dic5 count]);
    //获取字典所有的键,或者获取字典对象的值
    NSLog(@"%@",[dic5 allKeys]);
    NSLog(@"%@",[dic5 allValues]);
    //字典的查找功能:
    //字典的查找速度很快。字典采用hash(哈希)结构,离散算法。
    NSString * value=[dic5 objectForKey:@"a"];
    NSLog(@"%@",value);
    //字典的遍历
    //1 快速枚举
    for (id key in dic5) {
        NSLog(@"key:%@ value:%@",key,[dic5 objectForKey:key]);
    }
    for (int i=0; i<dic5.count; i++) {
        NSString * key=[[dic5 allKeys]objectAtIndex:i];
        NSString * value=[dic5 objectForKey:key];
        NSLog(@"key:%@ value:%@",key,value);
    }
    //依然是对比内容。
    NSDictionary * dic6=[NSDictionary dictionaryWithObjectsAndKeys:@"one",@"1",@"two",@"2",@"three",@"3",@"two",@"4",@"two",@"5", nil];
    NSLog(@"dic6:%@",dic6);
    NSArray * allTwoKeys=[dic6 allKeysForObject:[NSMutableString stringWithString:@"two"]];
    NSLog(@"%@",allTwoKeys);
    //可变字典,其他参考NSDictionary的创建。
    NSMutableDictionary * dic7=[NSMutableDictionary dictionaryWithDictionary:dic6];
    NSLog(@"%@",dic7);
    //可变字典的增删改:
    //增:
    //setObject:forKey:当传递的key在原字典中不存在时,会增加一个健值对;否则修改key对应的value值。
    [dic7 setObject:@"six" forKey:@"6"];
    NSLog(@"%@",dic7);
    NSDictionary * dic8=[NSDictionary dictionaryWithObjectsAndKeys:@"seven",@"7", nil];
    [dic7 addEntriesFromDictionary:dic8];
    NSLog(@"%@",dic7);
    //删 remove
    [dic7 removeObjectForKey:[NSMutableString stringWithString:@"1"]];
    NSLog(@"%@",dic7);
    NSArray * deleteKeys=[NSArray arrayWithObjects:@"4",@"5",@"8", nil];
    //removeObjectsForKeys:只会删除字典里面存在数组中的key;数组里面有的但是字典没有的key;字典不会删除。
    [dic7 removeObjectsForKeys:deleteKeys];
    NSLog(@"%@",dic7);
    return 0;

}

OC数组

#import <Foundation/Foundation.h>
int main()
{
    //OC数组,用来存放各种OC类的对象的引用。
    //创建不可变OC数组
    NSArray * arr1=[[NSArray alloc]initWithObjects:@"one",@"two",@"three",@"eight",@"four",@"five", nil];
    NSLog(@"%@",arr1);
    NSArray * arr2=[[NSArray alloc]initWithArray:arr1];
    NSLog(@"%@",arr2);
    NSArray * arr3=[NSArray arrayWithObjects:@"one",@"two",@"three",@"eight",@"four",@"five", nil];
    NSLog(@"%@",arr3);
    NSArray * arr4=[NSArray arrayWithArray:arr3];
    NSLog(@"%@",arr4);
    //获取数组元素个数 count
    unsigned long arrCount=[arr4 count];
    NSLog(@"%lu",arrCount);
    //通过下标获取数组元素。
    NSString * oneObject=[arr4 objectAtIndex:0];
    NSLog(@"%@",oneObject);
    //交换数组元素
    [arr7 exchangeObjectAtIndex:j withObjectAtIndex:j + 1];
    //在数组中查找对象。
    //数组查找的依据:根据对象内容对比查找。
    NSLog(@"%p",@"one");
    NSMutableString * oneCopy=[NSMutableString stringWithString:@"one"];
    NSLog(@"oneCopy:%p",oneCopy);
    NSMutableString * oneCopy2=[NSMutableString stringWithString:@"one"];
    NSLog(@"oneCopy2:%p",oneCopy2);
    NSArray * arr5=[NSArray arrayWithObjects:@"1",oneCopy, nil];
    unsigned long index=[arr5 indexOfObject:oneCopy2];
    NSLog(@"%lu",index);
    //数组的遍历;
    for (int i=0; i<arr4.count; i++) {
        NSString * oneObj=[arr4 objectAtIndex:i];
        NSLog(@"%@",oneObj);
    }
    //快速枚举遍历
    for (NSString * oneObj in arr4) {
        NSLog(@"%@",oneObj);
    }
    //字符串分割
    NSString * string =@" i am lilei i am   12 ";
    NSArray * resultArray=[string componentsSeparatedByString:@" "];
    NSLog(@"resultArray:%@",resultArray);
    //数组元素的拼接:拼接的是里面每个对象的description
    NSString * resultString=[resultArray componentsJoinedByString:@"*"];
    NSLog(@"%@",resultString);
    //创建可变OC数组
    NSMutableArray * arr6=[NSMutableArray arrayWithObjects:@"one",@"two",@"three",@"four",@"five", nil];
    NSLog(@"%@",arr6);
    //可变数组的增删改
    //增,add insert append(x)
    [arr6 addObject:@"six"];
    NSLog(@"%@",arr6);
    [arr6 insertObject:@"seven" atIndex:1];
    NSLog(@"%@",arr6);
    //删,delete remove
    [arr6 removeLastObject];
    NSLog(@"%@",arr6);
    [arr6 removeObject:[NSMutableString stringWithString:@"seven"]];
    NSLog(@"%@",arr6);
    [arr6 removeObject:@"three" inRange:NSMakeRange(2, 2)];
    NSLog(@"%@",arr6);
    //改
    [arr6 replaceObjectAtIndex:1 withObject:@"six"];
    NSLog(@"%@",arr6);
    [arr6 exchangeObjectAtIndex:1 withObjectAtIndex:3];
    NSLog(@"%@",arr6);
    return 0;
}

OC字符串

#import <Foundation/Foundation.h>


int main(int argc, const char * argv[]){
    //不可变OC字符串的创建:
    NSString * str1 = @"hello world!";
    NSString * str2 = [[NSString alloc] initWithString:@"hello world"];
    NSString * str3 = [[NSString alloc] initWithUTF8String:"hello world"];
    NSString * str4 = [[NSString alloc] initWithFormat:@"I am %@ and %d ages", @"Li lei", 12];
    NSString * str5 = [NSString stringWithString:str1];
    NSString * str6 = [NSString stringWithUTF8String:"hello world"];
    NSString * str7 = [NSString stringWithFormat:@"I am %@ and %d ages", @"Li lei", 12];
    NSLog(@"%@%@%@%@%@%@%@", str1, str2, str3, str4, str5, str6, str7);
    //获取字符串的长度
    NSString * str8=@"12我3456789";
    NSLog(@"str11的长度:%lu",[str8 length]);//str8.length
    //获取字符。
    unichar oneChar1=[str8 characterAtIndex:2];
    NSLog(@"获取字符:%C",oneChar1);
    //判断字符串相等equal
    NSString * str9=@"12345";
    NSString * str10=@"1234";
    BOOL ret=[str9 isEqualTo:str10];
    if (ret) {
        NSLog(@"str9与str10相等");
    }else{
        NSLog(@"str9与str10不相等");
    }
    //字符串比较 compare
    NSComparisonResult comparisonResult=[str9 compare:str10];
    if (NSOrderedAscending==comparisonResult) {
        NSLog(@"str9<str10");
    }else if(NSOrderedSame==comparisonResult){
        NSLog(@"str9==str10");
    }else {
        NSLog(@"str9>str10");
    }
    //不区分大小写比较
    NSString * str11=@"ios";
    NSString * str12=@"IOS";
    NSComparisonResult comparisonResult1=[str11 compare:str12 options:NSCaseInsensitiveSearch];
    if (NSOrderedAscending==comparisonResult1) {
        NSLog(@"str12<str12");
    }else if(NSOrderedSame==comparisonResult1){
        NSLog(@"str12==str12");
    }else{
        NSLog(@"str12>str12");
    }
    //字符串的查找
    NSString * str19=@"see you aga again!";
    NSRange range=[str19 rangeOfString:@"aga"];
    NSLog(@"location:%lu length:%lu",range.location,range.length);
    //判断没有找到的两种方法:a,range.location==NSNotFound;b, range.length==0;
    if (range.location==NSNotFound) {
        NSLog(@"没有找到");
    }
    NSRange range2=[str19 rangeOfString:@"aga" options:NSBackwardsSearch];
    NSLog(@"location:%lu length:%lu",range2.location,range2.length);
    //判断字符串前缀,后缀
    NSString * str20=@"http://www.baidu.com";
    BOOL ret2=[str20 hasPrefix:@"http"];
    NSLog(@"%d",ret2);
    BOOL ret3=[str20 hasSuffix:@".com"];
    NSLog(@"%d",ret3);
    //数字,字符串相互转换
    int intNum=10;
    float floatNum=12.2;
    NSString * intString=[NSString stringWithFormat:@"%d",intNum];
    NSLog(@"%@",intString);
    NSString * floatString=[NSString stringWithFormat:@"%.1f",floatNum];
    NSLog(@"%@",floatString);
    NSString * str21=@"123";
    NSString * str22=@"12.34";
    int int21=[str21 intValue];
    NSLog(@"%d",int21);
    float float22=[str22 floatValue];
    NSLog(@"%f",float22);
    //大小写转换
    NSString * str23=@"iosStringSSKKK";
    NSString * str24=[str23 uppercaseString];
    NSLog(@"%@",str24);
    NSString * str25=[str23 lowercaseString];
    NSLog(@"%@",str25);
    //字符串的提取;
    NSString * str26=@"long time no see!";
    NSString * str27=[str26 substringFromIndex:10];
    NSLog(@"%@",str27);//10下标对应的字符在结果里面。
    NSString * str28=[str26 substringToIndex:10];
    NSLog(@"%@",str28);//10下标对应的字符不在结果里面。
    NSString * str29=[str26 substringWithRange:NSMakeRange(10, 2)];
    NSLog(@"%@",str29);//10下标对应的字符在结果里面,结果一共有两个字符
    //可变OC字符串的创建:(必须先初始化)
    //增
    NSMutableString * str30=[NSMutableString stringWithString:@"123456"];
    NSLog(@"%@",str30);
    //add(x) append insert
    [str30 appendString:@"789"];
    NSLog(@"%@",str30);
    [str30 appendFormat:@"%d",110];
    NSLog(@"%@",str30);
    [str30 insertString:@"abc" atIndex:1];
    NSLog(@"%@",str30);
    //删 delete
    [str30 deleteCharactersInRange:NSMakeRange(1, 3)];
    NSLog(@"%@",str30);
    //改
    [str30 replaceCharactersInRange:NSMakeRange(1, 3) withString:@"A"];
    NSLog(@"%@",str30);
    [str30 setString:@"new land"];
    NSLog(@"%@",str30);
    return 0;
}

0 0
原创粉丝点击