• Fouandation(NSString ,NSArray,NSDictionary,NSSet) 中常见的理解错误区


    Fouandation 中常见的理解错误区

     

    1.NSString  

    //快速创建(实例和类方法) 存放的地址是 常量区

     NSString * string1 = [NSString alloc]initWithString:@“123”];

    NSString * string3 = [NSString stringWithString:@“123”];

    //格式化方法创建   存放地址是堆区

     NSString * string2 = [NSString alloc]initWithFormat:@“%d”,123];

     NSString * string4 = [NSString stringWithFormat:@“%d”,123];

     

    所以当判断两个字符串内容是否相同时

    if(string1 isEqualToString:string3) //string2与string4也一样

    {

     

     }//结果内容是相同的

     

    但是

    if(string? == string ? )

    {

     

     }//当是1 与 3 时 是是同一个对象,因为内存的指向同一个地址(常量区)

     //当是 2 与 4 时 不是同一个对象 ,因为内容相同,但是指向的内存地址不一样(堆区)

     

    注意点:NSMutableString 用快速创建的一样是 存放在堆区

     

    2.比较 字符串的大小

     NSComparisonResult  result =【string1 caseInsensitiveCompare:string2】;//返回的是基本数据类型:-1 降序 0 相等  1 升序

     

    3.字符串长度 : 【string1 length】;

     

    4.字符串转换:

    4.1 字符串首字母大写(有空格的单词首个字母都大写):[ string  capitalizedString] 

      4.2字符串所有字母大写:[ string  uppercaseString]

    4.3.字符串所有字母小写:[ string  lowercaseString] 

     

    5 字符串转换成基本数据类型 

     float i = 【string floatValue】;(boolValue , intValue….)

     

    6.字符串转换成数组(有规律的字符串)

    NSString * MD = [NSString  stringWithString:@“abc 123 xyz”];

    NSArray *array = [MD componentsSeparatedByString:@“ “];//空格作为分割

     

    7.字符串的截取:

     NSString  *subString1 = [string subStringToIndex:2];//从开头到 第X位 不包括X位

     

    NSString  *subString2 = [string subStringFromIndex:2];//从第X位 到最后 包括第X位

     

    NSRange range ;

    range.location = 4;

    range.length = 2;

    第二种定义range

    NSRange *range = {4,2};

    NSString  *subString2 = [string subStringWithRange : range];

     

    8.字符串的拼加:

    1.通过格式化字符串

    NSString *string1 = [NSString  stringWithString:@“123”];

    NSString *string1 = [NSString  stringWthString:@“abc”];

     

    NSSting *appengString = [NSString alloc]]initWithFormat:@“%@%@“,string1,string2];

     

    2.NSString *appString  = [string1 stringByAppendingString:string2];

    3.NSString *appString 3 = [string1 stringByAppendingFormat:@“%@“,string2];

     

    9.查询字符串

    NSString *string1 = [NSString  stringWithString:@“123abc_maxxyz”];

    NSRange range = [string1 rangeOfString:@“abc_max”];

    if(range.location != NSNotFound)

     {

      

     } NSNotFound = NSIntergeMax; 查询range 这段范围 的起始点是否找得到

     

    //注意点:要查看range的location and length 可以通过 NSStringFormrange(range);

     

    9.NSMutableString继承 Nstring  

      常用的插入 ,替换,删除

    NSMutableString *mutableString = [NSMutableString alloc]]initWithString:@“abc”];

    9.1 插入

    [mutableString insertString:@“…xyz” atIndex:3];

     

    9.2 替换

    [mutableString replaceCharactersInRange:NSMakeRange:(0,3) withString:@“efg”];

     

    9.3删除

    [mutableString deleteCharactersInRange:NSMakeRange:(0,3)]; 

     

    ///////////////////////////////////////////////

    NSArray  的知识点

    1.数组的创建(类方法和实例方法)

    NSArray * array1 = [NSArray arrayWithObject:@“one”];//单个

    NSArray * array2 = [NSArray arrayWithObjects:@“one”,@“two”,nil];//创建多个对象

    NSArray * array3 = [NSArray arrayWithArray:array1]; //通过数组直接创建

     

    2.数组中元数的个数

    [array1 count];

     

    3,增加数组中的元素

    NSArray *array4 = [array2 arrayByAddingObject:@“end”];

     

    4.将数组转换成字符串

    NSString *string  = [array4 componentsJoinsByString:@“," ];

     

     

    5.根据对象返回索引下标,根据索引下标返回对象

    NSString *string  = [array2 objectAtIndex:2];//根据索引下标返回对象

     

    NSInteger a  = [array2 indexOfObject:@“one”];//根据对象返回索引下标

     

    NSString string = [array2 lastObject];//返回数组最后一个元素

     

    6.数组中是否包含某个元素

     BOOL isContains = [array2 containsObject:@“one”];

     

    7.NSMutableArray 是继承NSArray 

    NSMutableArray *mutableArray = [NSMutableArray array];

    NSMutableArray *mutableArray2 = [NSMutaleArray arrayWithCapacity:5];//这个仅仅起到标志内存,可以超出和少于

     

    NSMutabelArray *mutableArray3 = [NSMutableArray arrayWithObjects:@“aaa”,@“bbb”,nil];

     

    //添加

    [mutableArray3 addObject:@“ccc”];//在数组最后添加一个元素

     

    //插入

    [mutableArray3 insertObject:@“” atIndex: 0]; //在指定的位置插入一个元素

     

    //删除

    [mutableArray3 removeLastObject];//移除最后一个元素

     

    [mutableArray3 removeObject:@“bbb”];//指定元素移除

     

    [mutableArray3 removeObjectAtIndex:@“0”];//根据下标移除元素

     

    [mutableArray3 removeObjectsInArray:array2];//根据数组移除元素

     

    [mutableArray3 removeAllObjects];//移除所有元素

     

    //替换

    [mutableArray3  replaceObjectAtIndex:0 withObject:@“”replace];

     

    //遍历数组

    常规遍历

     

    NSArray *array  =[NSArray arrayWithObjects:@“One”,@“Two”,nil ];

     

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

    {

      NSString *string = [array objectAtIndex:index];

      NSLog();

     }

     

    快速枚举

    for(NSString *string in array)

    {

      NSLog();

     }

    for(id  string in array)

    {

     NSLog();

     }//id 包含了* ,当不知道类型的时候使用id

     

     

    /////////////////////////////////////////////////////

    NSDictionary 

    1.NSDictionary 的创建(实例和类方法)

    NSDictionary *dic1 = [NSDictionary dictionaryWithObject:@“value" forKey:@“k1”];

     

    NSDictionary *dic2 = [NSDictionary dictionaryWithObjectAndKeys:@“v1" ,@“k1”,@“v2" ,@“k2”,@“v3" ,@“k3”,nil];//创建多个

     

    NSDictionary *dic3 = [NSDictionary dictionaryWithDictionary:dic1];//通过字典创建

     

    //获取字典的Value 

    NSString *string =【dic2 objectForKey:@“key”];

     

    //获取字典的所有Key 

    [dic2 allKeys];

     

    //获取字典的所有Value

    [dic2 allValues];

     

    NSMutableDictionary 继承 NSDictionary 

    //增加整个字典 

    [dic2 addEntriesFromDictionary:dic4];

    //增加一个键值对

    [dic2 setValue:@“Object” forKey:@“key”];

     

    //创建一个空的字典

    NSMutableDictionary *dicc = [NSMutableDictionary dictionary];

    [dicc setDictionary:dic2];//特别注意的是:如果dicc 如果有值则会被覆盖

     

    //移除

    [dic2 removeObjectForKey:@“k1”];//移除一个对应键值对 

     

    [dic2 removeObjectForKeys:arrayKey];//移除多个键值对

     

    遍历字典

    NSArray *key  = [dic allKeys];

    for(int index = 0; index < [key count];index ++)

    {

      id  k = [key objectAtIndex:index];

      id V = [dic objectForKey:k];

    NSLog();

     }

     

    //快速遍历

    for(id key in dic1)

    {

      NSString *string = [dic1 objectForKey:key];

     NSLog();

    }

     

    //枚举遍历

    NSEnumerator *enumerator = [dic1 keyEnumerator];

    id key = [enumerator nextObject];

    while(key)

    {

       id Value = [dic1 objectForKey:key];

    NSLog();

    id key = [enumerator nextObject];

     

    }

     

    /////////////////////////////////////

    NSSet

    创建(实例和类方法)初始化

    NSSet *set1 = [NSSet alloc]]initWitObjects:@“1”,@“2”,nil];

    NSSet * set2 = [NSSet setWithObjects:@“one”,@“two”,nil];

     

    NSArray *array = [NSArray arrayWithObjects:@“x”,@“y”,nil];

     

    NSSet *set3 = [NSSet setWithArray:array];//通过数组创建集合

     

    NSSet *set4 = [NSSet setWithSet:set1];//通过集合创建集合

     

    //计算集合有几个元素

    int a = [set1 count];

     

    //获取所有的集合元素

    NSArray *objects = [set allObjects];

     

    //获取集合中任意一个元素

    id object = [set anyObjects];

     

    //集合中 是否包含某个元素

    BOOL isContains = [set1 containsObject:@“2”];

     

    //集合与集合间是否存在相同的元素

    BOOL isIntersect = [set1 intersectsSet:set2];

     

    //集合与集合是否完全匹配

    BOOL isEqual = [set1 isEqual set2];

     

    //集合是否是另一个集合的子集

    BOOL isSubset = [set1 isSubsetOfSET:set5];

     

    //对已有集合增加一元素创建新的集合

    NSSet *set = [set1 setByAddingObject:@“12”];

     

    //对已有集合增加集合创建新的集合

    NSSet *set = [set1 setByAddingObjectsFromSet:set2];

     

    //对已有集合增加数组创建新的集合

    NSSet *set = [set1 setByAddingObjectsFromArray:array];

     

    /////////////////

    NSMutableSet((类方法和实例)初始化

    NSMutableSet *set1 = [NSMutableSet setWithObjects:@“1”,@“2”,nil];

    NSMutableSet *set2 = [NSMutableSet setWithObjects:@“d”,@“2”,nil];

     

    //“减去”相同的

    [set1 minusSet:set2];

     

    //集合中相同的(交集)

    [set1 intersectSet:set2];//记得与intersectsSet 的差别

     

    //并集

    [set1 unionSet: set2];

     

    //根据元素移除

    [set1 removeObjects:@“1”];

     

    //移除集合所有的元素

    [set1 removeAllObjects];

     

    //根据数组增加集合元素

    [set1 addObjectsFromArray:array];

  • 相关阅读:
    计算机最小单位
    api接口调用
    STM32SystemInit函数
    关于 Verilog 的 TimeScale
    破获ARM64位CPU下linux crash要案之神技能:手动恢复函数调用栈
    芯片后仿
    破获ARM64位CPU下linux crash要案之神技能:手动恢复函数调用栈
    HardFault定位方法和步骤
    BSP和SDK的区别
    armCPSR寄存器
  • 原文地址:https://www.cnblogs.com/meixian/p/5371002.html
Copyright © 2020-2023  润新知