ObjectC----几个常用的类

  // Create By 郭仔  2015年03月31日20:54:20

1. NSString类


   // 求字符串长度

    NSString *str = @"HelloString";

    NSUInteger len = [str length]; // NSUIntergerlong

    NSLog(@"%ld",len);

    

    //获取子字符串

    

    

    NSString *substr = [str substringFromIndex:5];//从下标5开始取子字符串

    NSLog(@"%@",substr);

   // 从开始取子字符串到下标为5的位置截止

    NSString *substr2 = [str substringToIndex:5];

    NSLog(@"%@",substr2);

    

    NSRange rang = {2,3}; //起始位置和长度

   // 从起始位置2取长度为3的子字符串

    NSString *substr3 = [str substringWithRange:rang];

    NSLog(@"%@",substr3);

    

    //拼接字符串

    

    NSString *comStr1 = [str stringByAppendingString:@"IOS"];

    NSLog(@"%@",comStr1);

    

    NSString *comStr2 = [str stringByAppendingFormat:@"hehe%@ %@",@"hhh",@"jjj"];

    NSLog(@"%@",comStr2);

    

    

    //字符串替换

    

    NSString * newStr1 = [str stringByReplacingOccurrencesOfString:@"string" withString:@"world"];

      NSLog(@"%@",newStr1);

    

    

   // 判断字符串是否相等

    //判断字符串是否相等用 isEqualTo方法,不能用==判断

   // isEqualTo方法判断的时字符串的内容是否相等,而==判断的时两个指针是否指向同一地址

    

    NSString *cmpStr = @"Hello String";

    BOOL eq = [str isEqualToString:cmpStr];

    if (eq) {

        NSLog(@"相等");

    }

    else{

        NSLog(@"不相等");

    }

   // 判断前缀是否已给定的字符串相等,即是否已该字符串开头

    BOOL prefix = [str hasPrefix:@"Hello"];

    if (prefix) {

        NSLog(@"Hello开头");

    }

    else {

        NSLog(@"不以Hello开头");

    }

    

    

    

    //判断后缀

   //判断是否已png结尾,是就替换成jpg,否则拼接jpg

    BOOL isPng = [str hasSuffix:@"png"];

    if (isPng) {

        //执行替换

        NSString * tem = [str stringByReplacingOccurrencesOfString:@"png" withString:@"jpg"];

        NSLog(@"%@",tem);

    }

    else

    {   //执行拼接jpg

        NSString *tem = [str stringByAppendingString:@"jpg"];

        NSLog(@"%@",tem);

    }

    

    

    

=====================================================================================

2. NSMutableString//可变字符串

    NSMutableString *mulStr = [NSMutableString stringWithString:@"Hello"];

    NSLog(@"%@",mulStr);

    

    //拼接

    [mulStr appendFormat:@"guozai"];

    NSLog(@"%@",mulStr);

    [mulStr appendString:@"mutible"];

    NSLog(@"%@",mulStr);

    

    

    //删除子字符串

    NSRange ran = {4,1};//结构体类型

    [mulStr deleteCharactersInRange:ran];

    NSLog(@"%@",mulStr);

    

    //替换

    NSRange ran2 = {3,2};

    [mulStr replaceCharactersInRange:ran2 withString:@"yy"];

    NSLog(@"%@",mulStr);

    

    //插入

    [mulStr insertString:@"tt" atIndex:2];

    NSLog(@"%@",mulStr);

    

    

   // 对于不可变字符串NSString的字符串拼接,分割等操作,都会创建新的字符串

   // 对于可变字符串NSMutableString的字符串拼接分割替换等操作是在原字符串的基础上

   // 进行修改,不会创建新字符串

    // NSMutableStringNSString的子类,所以NSString的方法,NSMutableString

    //可以使用

   // 在以后的学习中,凡是出现Mutable的类,都是不带Mutable类的子类:

    //,NSMutableArrayNSArray的子类,NSMutableDictionaryNSDictonary的子

    //

 ====================================================================================  3.NSArray

//数组

    //最后的nil不可丢掉

    NSArray *arr =[NSArray arrayWithObjects:@"guozai", @"guo",@"zaiguo",nil];

    

    // 获取数组元素个数

    NSUInteger count = [arr count];

    NSLog(@"%lu",count);

    

    

    //获取第一个对象

    NSString *p1 = [arr firstObject];

    NSLog(@"%@",p1);

    

    

    // 获取最后一个对象

    NSString *p2 = [arr lastObject];

    NSLog(@"%@",p2);

    

    // 获取下标对应的对象

    NSString *p3 = [arr objectAtIndex:1];

    NSLog(@"%@",p3);

    

    

    //遍历数组

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

        NSLog(@"%@",[arr objectAtIndex:i]);

    }

=====================================================================================
4.NSMutableArray

 //可变数组

    // 一个数组的内容赋给另一个数组

    NSMutableArray * mulArray = [NSMutableArray arrayWithArray:arr];

    //删除下标为index的对象

    [mulArray removeObjectAtIndex:2];

    

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

//        NSLog(@"%@",[arr objectAtIndex:i]);

//    }

//

    // 添加一个对象元素

    [mulArray addObject:@"guoguo"];

    

    // 交换下标对应的元素对象

    [mulArray exchangeObjectAtIndex:0 withObjectAtIndex:[mulArray count]-1]; //交换第一个元素和最后一个元素

=====================================================================================
下面通过一个实例来形象的了解:
使用可变数组管理BOOk类,实现图书的增删查改
BOOK有两个成员变量:_name,_price;

 Book * book1 = [[Book alloc] initWithName:@"guozai1" andPrice:10];

    Book * book2 = [[Book alloc] initWithName:@"guozai2" andPrice:15];

    Book * book3 = [[Book alloc] initWithName:@"guozai3" andPrice:13];

    

    //数组赋值

    NSMutableArray *books = [NSMutableArray arrayWithObjects:book1,book2,book3, nil];

    

    Book * book4 = [[Book alloc] initWithName:@"guozai4" andPrice:12];

    

    //添加一本书

    [books addObject:book4];

    

    //删除一本书

    [books removeObjectAtIndex:2];

    

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

        NSLog(@"%@,%.2f",[[books objectAtIndex:i] name],[[books objectAtIndex:i] price]);

    }

    

    

    //查找名字是guozai3的书,打印价格

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

        if ([[[books objectAtIndex:i] name] isEqualToString:@"guozai3"]) {

            NSLog(@"%f",[[books objectAtIndex:i] price]);

        }

    }

    

    // 对数组进行排序,按价格从高到低

    for (int i = 0; i < [books count] - 1; i ++) {

        for (int j = 0; j < [books count] - i - 1; j ++) {

            if ([books[j] price] < [books[j+1] price]) {

                [books exchangeObjectAtIndex:j withObjectAtIndex:j+1];

            }

        }

    }

    

    

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

        NSLog(@"%@,%.2f",[books[i] name],[books[i] price]);

    }

========================================================================================
5.NSNumber:将基本数据类型转化成对象类型

 //将基本数据类型int转化为对象类型

    NSNumber * intNum = [NSNumber numberWithInt:100];//便利构造器

    NSMutableArray * ar = [NSMutableArray arrayWithObjects:intNum, nil];

    NSNumber * tem = [ar objectAtIndex:0];

    

    //将对象类型转化成基本数据类型

    int result = [tem intValue];

    NSLog(@"%d",result);

========================================================================================
6.NSValue:将结构体转化成对象

 //将一个点转化成NSValue对象

    NSPoint point = {1,2};

    //将一个结构体转化成NSValue对象

    NSValue *vPoint = [NSValue valueWithPoint:point];

    

    //vPoint转化成结构体

    NSPoint point2 = [vPoint pointValue];

    // NSLog(@"%.2f,%.2f",point2.x,point2.y);

    

    // NSStringFromPoint可以将点转化成字符串

    NSLog(@"%@",NSStringFromPoint(point2));


其他的例子类比就行:例如

 //NSsize结构体转化成NSValue对象

    NSSize size = {22,44};

    NSValue * sValue = [NSValue valueWithSize:size];

    

    //NSValue对象转化成NSSize结构体;

    NSSize size2 = [sValue sizeValue];

    NSLog(@"%@",  NSStringFromSize(size2));

========================================================================================
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值