黑马程序员——0C语言——Foundation框架

———Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ———
一、结构体

1、NSRange(表示范围)(包含两个成员:location,length)

     1> @"I love oc";中 love 范围可表示为(location=2 length=4

              [10,19,20,21,22]最后三个数(20,21,22)的范围是(location=2,length=3)

     2> 表示@"I love oc";中 love 范围

              NSRange r = NSMakeRange(2,4)

    3> 查看@"I love oc";中 love 的范围 

               NSString * str = @"I love oc";

// CoreGraphics框架

               NSRange range = [str rangeOfString:@"love"];

               NSLog(@"location=%d,length=%d",range.location,range.length);

     2、NSPoint(CGPoint)(表示点坐标(x,y)

     创建方式:CGPoint P1 = NSMakePoint(10,10)

                    NSPoint P2 = CGPointMake(10,10)

     3、NSSize(CGSize)(宽度(width)高度(height))

创建方式:CGSize S1= CGSizeMake(100,20)

                    NSSize S2= CGMakeSize(20,10)

    4、NSRect(CGRect)

    CGRect r1 = CGRectMake(0,0,100,50)//CGRect包含了CGPoint(0,0)和CGSize (100,50) 

     5、将结构体转换为字符串

    CGPoint  P1 = NSMakePoint(10,10);

          NSString*str = NSStringFromPoint(P1);    

          NSLog(@"%@",str);  // 输出结果为{10,10}

6、结构体的其它用法

    1. 坐标原点的表示方法

    (1)CGPointMake(0,0)

    (2)CGPoint Zero 

    2. 判断两个点是否相同(BOOL类型)

         CGPoint EqualToPoint((CGPoint  point1),(CGPoint point2))

    3. 判断一个点是否在一个范围内

          CGRectContainsPoint((CGRect rect),(CGPoint  point))

       

二、字符串
1> NSString(不可变字符串)

  
  
创建方式:
1)NSString * s1 = @"4234";
2)NSString * s2 = [[NSString alloc] initwithstring : @"jack"];(不常用)
3)NSString * s3 = [[NSString alloc] initwithFormat : @"age is %d",10] ;
4)NSString*s4 = [[NSString alloc] initwithContentsOfFile:@"/Users/apple/Desktop/1.txt" encoding:NSUTF8StringEncoding  error:nil]; //NSUTF8StringEncoding用于中文编码
     //URL : 资源路径(协议头://路径)
       网络资源   : http://
       本地资源   : file://
       服务器资源: ftp://
       NSURL *url = [[NSURL alloc] initWithString:@"file:///Users/apple/Desktop/1.txt"];
       NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/1.txt"];
5)NSString *s5 = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding  error:nil];
6)NSString *s6 = [s2 stringByAppendingString:@" 1112"];
      一般都会有一个类方法跟对象方法配对
      [NSURL URLWithString:<#(NSString *)#>];
      [NSString stringWithFormat:@""];

      [NSString stringWithContentsOfFile:<#(NSString *)#>encoding:<#(NSStringEncoding)#> error:<#(NSError *__autoreleasing*)#>]

C语言字符串与OC字符串的转换

    1)C语言字符串转为OC字符串 :NSString *s7 = [[NSString alloc] initWithUTF8String:"jack"];

    2)OC字符串转为 C语言字符串:const char *cs = [s4 UTF8String];

    将一个字符创写到文件中       [@"Jack"writeToFile:@"/Users/apple/Desktop/my.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];        //将"jack"这个字符串写进/Users/apple/Desktop/my.txt这个文件中去     也可以调用类方法实现:       [str writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil];     获取当前字符串的长度:[str length];//字符串length方法返回的是字符串的字数

2> NSMuableString (可变字符串)(NSString的子类)
1) NSString所有的创建方式,NSMutableString都拥有
    2) NSMutableString是可变的,可以往里面增加或删除内容
     3) 拼接
     NSMutableString *s1 = [NSMutableStringstringWithFormat:@"my age is 10"];
     [s1 appendString:@" 11 12"];        // 拼接内容到s1的后面
4) 删除
     [s1 deletecharactersInRange:NSMakeRange(4,2)];   //删除s1中某个位置范围的字符
     5) 如果要删除某个固定的字符
     首先获取is的范围
          NSRange range = [s1 rangeOfString:@"is"];
     再进行删除

          [s1 deleteCharactersInRange:range];


三、数组

1> NSArray(不可变数组)(有序的)

   1) NSArray是一个集合类(只能存放OC 对象,不能存放非OC对象(基本数据类型:int,struct,enum))  
   2) 创建方式:
   NSArray*array=[NSArraywithobjects:@"jack",@"rose",nil];   //nil是数组元素结束的标志
   快速创建:NSArray*array=@[@"jack",@"rose"]        //一般选用这种创建方式
   3) 数组元素的个数:
      [array count]; 
      NSLog(@"%ld",array.count)
   4) 拿出数组中的某个元素
   [array  objectAtIdex:1];          //拿出数组的第一个元素
   array[0]; //一般选用这种创建方式                     
   5) NSArray的遍历
    for(int i=0;i<array.count;i++)
           {
              NSLog(@ "@%",array[i]);
           }

   快速遍历

// id obj代表数组中的每一个元素,把array中的每个元素拿出来,赋值给obj

            for (id obj in array)
           {
             // 找出obj元素在数组中的位置
             NSUInteger i = [arrayindexOfObject :obj];
             NSLog(@"%@-%@",i,obj);
           }

利用block

      [array enumerateObjectsUsingBlock:
             // 每遍历到一个元素,就会调用一次block
              // 并且当前元素和索引位置当做参数传给block
      ^(id obj, NSUInteger idx, BOOL *stop)      
           {
               NSLog(@"%ld - %@", idx, obj);
               if (idx == 0)
             {
             // 停止遍历
              *stop = YES;
             }
           }];
2> NSMutableArray(NSArray的子类)(可变数组)(无序的)
   1) 添加元素
       NSMutableArray *array = [NSMutableArray arrayWithObjects:@"rose", nil];
       [array addobject:[[person alloc] init]];
       [array addobject:@"jack"];
   2) 删除元素
       [array removeAllobjects];   //删除所有元素
       [array removeobject:@"jack"];//删除指定元素"jack"
       [array removeobjectAtIndex:2];//删除第二个元素
3) 错误写法
  [array addobject:10];    //OC数组不能存放基本数据类型
  [array addobject:nil];    //不能为空
  [NSMutableArray*array=@[@"jack",@"rose"]; //@[]只可以创建不可变数组NSArray


四、NSSet和NSMutableset

1、NSSet(不可变)
    创建
          NSSet *s = [NSSet set];        //空的set
          NSSet *s2 = [NSSet SetWithObect:@"jack",@"rose",nil]; 
    随机拿出一个对象
          NSString *str = [s2 anyobject];
2、NSMutableSet(子类)(可变)
    创建
          NSMutableSet *s = [NSMutableSet set]
    添加元素
          [s addobject:"@hake"]
    删除元素
          [s removeAllobject]     //删除所有元素

          [s removeobject::"@jack"]        //删除指定元素


五、字典
1、NSDictionary(不可变)
    创建
          NSDictionary *dict = [NSDictionaryWithObject:@"jack"for key:@"name"];//根据name可以找到jack
    字典就是通过key(索引)-----------》value(值)
    字典存储的东西都是键值对
    访问name对应的值:[dict objectForkey:@"name"];
    NSArray *keys = @[@"name",@"address"];
          NSArray *objects = @[@"jack",@" 北京"];
          NSDictionary *dict = [NSDictionary DictionaryWithObject :objects for key:keys];//根据keys找objects
    快速创建
          NSDictionary *dict = @{@"name":@"jack",@"address":@"北京"}; (常用)
          id obj=dict[@"name"];//拿出name对应的值
    遍历
          NSDictionary *dict = @{@"name":@"jack",@"address":@"北京"}; 
            for循环
               NSString *keys=[dict allkeys];
                    for(int i = 0; i < dict.count; i++)
                  {
                    NSString *key = keys[i];
                    NSString *object = dict[key];
                    NSLog(@"%@=%@",key,object);
               }
          block方法
               [dict enumeratekeysAndobjectsUsingBlock:

                  ^(id key,id obj,BOOL*stop)

{

NSLog(@"%@=%@",key,object);

}];
2、NSMutableDictionary(可变的)
     创建
          NSMutableDictionary *dict = [NSMutableDictionary dictionary];  //空字典
     添加键值对
          [dict setobject:@"jack" for key :@"name"];
          [dict setobject:@"rose" for key :@"name"];  //会将"jack"覆盖
     删除键值对
          [dict removeobjectForkey:(id)]

     NSMutableDictionary不能使用快速创建


六、NSNumber和NSvalue

NSnumber之所以能将基本数据类型包装成OC对象,是因为它继承了NSvalue

1、将基本数据类型包装成OC对象

1> 把整型数据转为NSNumber对象

1)NSNumber *num = [NSNumber numberWithInt:10];

2)@20(@"20")

3)int age = 10;

@(age);

2> 结构体转为NSValue对象

CGPoint p = CGPointMake(10,10);

  NSvalue value=[NSvalue valueWithPoint :p]

2、将OC对象转为基本数据类型

    1> 把value转为对应的整型变量

int a = [num intValue];

2> 把value转为对应的结构体

[value pointValue];

 

七、NSDate


1、创建一个时间对象
     NSDate *date = [NSDate date];
     NSLog(@"%@",date);//打印出的时间是0时区的时间
    2、NSDate *date2 = [NSDate dateWithTimeInterval:5 sinceDate:date];//date2比date晚5秒
    3、NSTimeIntelval seconds = {date timeInterval since1970};// 从1970开始走过的秒数
    4、日期格式化类(NSDate转NSString)
    NSDate date = [NSDate date];
          NSDateFormatter *formatter = [[NSDateFormatter alloc] init]
          formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
          NSString *str = [formatter stringFormDate:date];
          NSLog(@"%@"str);//y年M月d日 m分s秒H(24小时制)h(12小时制)
    5、NSString转NSDate
          NSString *time = @"2011/09/10 18:56";
          NSDateFormatter *formatter = @"yyyy-MM-dd HH:mm:ss";
          NSDate date = [formatter dateFromString:time];
          NSLog(@"%@",date); //0时区的时间

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值