OC学习笔记之FoundationKit

来源:互联网 发布:数据挖掘 推荐系统 编辑:程序博客网 时间:2024/06/16 10:13

OC学习笔记之FoundationKit

 

 

FoundationKit

 

 

 

1不可变字符串NSString

 

 

NSString静态的不可改变是指它一经赋值,值的内容便不可改变而NSMutableString动态)却与之相反。NSMutableString动态)是NSString的子类拥有NSString的所有方法。

 

NSString常用的方法

1.字符串的比较

1.1  [thing1 isEqualToString:thing2]与“==”的差别isEqualToString是比较字符串的内容而“==”比较的是指针的大小如果要比较两者是否为同一对象可以采用“==

格式:

        //isEqualToString:thing2==的比较

       NSString *thing1=@"hello 5";

       NSString *thing2=[NSString stringWithFormat:@"hello %d",5];

//        if ([thing1 isEqualToString:thing2]) {

//            NSLog(@"1Ther are the same");

//            

//        }

//        else{

//            NSLog(@"1not");

//        }

//        if (thing1==thing2) {

//            NSLog(@"2Ther are the same");

//            

//        }else{

//            NSLog(@"2");

//        }

        

 

1.2compare比较方法可以按照大小写以及字符个数来比较大小

格式:      //compare比较方法

        //NSCaseInsensitiveSearch不区分大小写

        //NSNumericSearch按字符个数进行排序

//        if ([thing1 compare:thing2 options:NSCaseInsensitiveSearch|NSNumericSearch]==NSOrderedSame) {

//            NSLog(@"They match");

//        }

 

   

2.查看字符串中是否含有指定字符(可用在文件查找中)

 // hasPrefix从前往后查

  //hasSuffix从后往前查

   //rangeOfString不区分位置查

实例:

 

 

//        NSString *fileName=@"draft-chapter.pages";

//        if ([fileName hasPrefix:@"draft"]) {

//            NSLog(@"文件头包含draft");

//        }

//        if ([fileName hasSuffix:@"pages"]) {

//            NSLog(@"文件尾包含pages");

//        }

3.查看字符在文件中位置的方法NSRange会返回两个值location(标识在字符串中的位置)以及length标识符的长度

 NSRange range=[fileName rangeOfString:@"ch"];

NSLog(@"位置在:%d,长度为:%d",range.location,range.length);

 

2可变字符串

NSMutableArry改变了NSString一经赋值不能改变的短板

2.1stringWithCapacity:42的值只是一个参考而已就像大人叫小孩要按时回家一样

2.2 appendString:赋值

tring appendFormat:格式化赋值与string中的stringformat类似

2.3 deleteCharactersInRange删除特定位置的字符(与NSRange结合使用)

 

 

//声明可变字符串

        NSMutableString *string=[NSMutableString stringWithCapacity:42];

        [string appendString:@"Hello append"];

        [string appendFormat:@" hello append %d",5];

    NSLog(string);

       //删除Jack姓名

    NSMutableString *friends=[NSMutableString stringWithCapacity:50];

    [friends appendString:@"James BethLynn Jack Evan"];

    NSRange jackRange=[friends rangeOfString:@"Jack"];

    jackRange.length++;

    [friends deleteCharactersInRange:jackRange];

 

 

 

 

 

 

集合家族

 

1.集合

2.1静态数组(注意他的赋值语句建议采用第二种)

2.2Count方法可以计数

      NSArray *arry=[NSArray arrayWithObjects:@"one",@"two",@"three",nil];

    //字面量语法创建数组不用加nil

    //objectAtIndex获取特定索引处对象

NSArray *arry2=@[@"one",@"two",@"three" ];

 

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

        NSLog(@"index %d has %@.",i,[arry objectAtIndex:i]);

}

 

 

 

 

 

 

 

 

2可变数组(解决了数组必须声明大小的问题)

    相应方法如下arrayWithCapacity:16的意义与可变字符串相同

    NSMutableArray *mArry=[NSMutableArray arrayWithCapacity:16];

    //利用循环向数组添加对象

    for (int i=0; i<4; i++) {

        Tire *tire=[Tire new];

        [mArry addObject:tire];

    }

    //删除特定处索引对象

    [mArry removeObjectAtIndex:1];

    

   3 字典  

3.1静态字典

为甚要采用字典来存储数据是应为如果采用数组的话每次查找非常麻烦要不断的遍历而字典只需要根据特定的关键值就可以取出来

    ///字典的申明及赋值

    

    NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:t1,@"front-left",t2,@"front-right",t3,@"back-left",t4,@"back-right" ,nil];

    //也可以使用(推荐用法)

    NSDictionary *dictTires=@{@"front-left":t1,@"front-right":t2,@"back-left":t3,@"back-right":t4};

    

}

 

 

3.2动态字典

与动态字符串动态数组具有共同性质

 

0 0
原创粉丝点击