• Objective-C基础学习笔记(九)-Foundation


    一.Foundation框架简介

      伟大的苹果公司提供了很多可以在应用程序里调用的框架。要使用一个框架,需要将它添加到你的项目中,你的项目才可以使用它。Foundation框架为所有的应用程序提供基本系统服务你的应用程序,具体可以干什么呢?

     使用Foundation可以:

    • 创建和管理集合,比如数组和字典
    • 访问存储在应用程序里的图片和其它资源
    • 创建和管理字符串
    • 提交和接收通知
    • 创建日期和时间对象
    • 自动发现IP网络上的设备
    • 操作URL流
    • 执行异步代码

    二.Foundation框架中一些结构体

    NSRange 结构体

    1.   NSRange 这个结构体的成员和定义如下:

    1 typedef struct _NSRange {
    2     NSUInteger location;  // 位置
    3     NSUInteger length;    // 长度
    4  } NSRange;

    2.作用:通过它的两个成员变量可以知道,NSrange能够表示一个范围从location位置开始的length长度; 例如:

     1) 表示 love在字符串@"i love oc"中的位置则:location = 2; length = 4; 

    2)56,78,89这三组数据在数组中{10,18,56,78,89}的范围; location = 2; length = 3

    3. 得到NSRange 这个结构体的两种常见方式为

    1)直接创建: NSRange r2= NSMakeRange(2,4);

    2)通过字符串的rangeOfString:方法可返回某子串在母串在母串中的位置,这种方法如果找不到

    length = 0 

    location = NSNotFound-1

    4.NSRange使用示例:

     
    int main()
    {
        
        /* NSRange结构体的创建 */
        NSRange r2 = NSMakeRange(2,4);// (掌握)方式常用
        NSLog(@"loc %lu ,length %lu",r2.location,r2.length); 2 4
        
        /* 查找ve o 在str中的位置 */
        NSString *str = @"i love oc";
        // 如果找不到length = 0 location = NSNotFound 即-1
        NSRange r3 = [str rangeOfString:@"ve o"];
        
        NSLog(@"loc %lu ,length %lu",r3.location,r3.length);// 4 4   
    
        return 0;
    }

    2015-03-24 10:33:43.697 Foundation框架-结构体[496:21768] loc 2 ,length 4

    2015-03-24 10:33:43.699 Foundation框架-结构体[496:21768] loc 4 ,length 4

    NSPointCGPoint结构体

    1.定义如下:

     
    # define CGFLOAT_TYPE double
     struct CGPoint {
        CGFloat x; //double类型
        CGFloat y;
     };
     typedef struct CGPoint CGPoint;
     typedef CGPoint NSPoint;</span>

    2.NSPointCGPoint是一样呢,可用于表示一个位置坐标

    3.常用的设定位置的方法:

    CGPoint point = CGPointMake(20, 20); //开发中最常用

    CGPoint p1 =NSMakePoint(10,10);

    4.CGPointZero == CGPointMake(0,0);

    5. 判断两个点是否相等的方法 

    BOOL p =  CGPointEqualToPoint(CGPointMake(10, 10), CGPointMake(10, 10));
     // 返回值是BOOL类型

    NSSizeCGSize结构体

    1. 定义如下:

    1  
    2 struct CGSize {
    3  CGFloat width;
    4  CGFloat height;
    5  };
    6  typedef struct CGSize CGSize;</span>

    2. 作用,可用于表示一个矩形,一个元素占用屏幕的范围

    3.常用的设定范围的方法: CGSize size = CGSizeMake(100, 200);
    4. CGSizeZero==  CGSizeMake(0, 0);

    5.判断两个size是否相等的方法

    BOOL s = CGSizeEqualToSize(CGSizeMake(100, 200), CGSizeMake(100, 200));

     NSRectCGRect

    1.定义如下:

     
    struct CGRect {
     CGPoint origin;
     CGSize size;
     };
     typedef struct CGRect CGRect;

    2.作用,这个结构体涵盖了,基本位置的点和占用屏幕的大小,就可确定这个元素在屏幕的具体位置和范围,确切的说可以表示一个矩形

    3.常用的设定结构体值的方法

        CGRect r1 = CGRectMake(0, 2, 100, 200);
        CGRect r2 = NSMakeRect(2, 1, 200, 100);
        CGRect r3 = {{0,0},{100,200}};
        CGRect r4 = {p1,s1};//直接使用点结构体和size结构体变量</span>

    4.判断一个矩形是否包含了某点的方法

    BOOL r = CGRectContainsPoint(CGRectMake(0, 0, 100, 200), CGPointMake(100, 201));</span> 

    5. 开发过程中经常要查看几个结构的值,一般都转化为字符串:

    // 开发中经常查看:常规做法将所有数据转化为字符串
        NSString *str = NSStringFromPoint(p1);
        NSString *str1 = NSStringFromSize(s1);
        NSString *str2 = NSStringFromRect(r1);
        NSLog(@"p1:%@",str);
        NSLog(@"s1: %@",str1);
        NSLog(@"r1: %@",str2);

    三 .NSString类

     NSString :不可变字符串

    1. 创建字符串的一些方法:

     1  
     2 
     3 int main()
     4 {
     5       /* 字符串的创建方式1 @"" */
     6       NSString *str = @"hello ";
     7       /* 字符串的创建方式2 类方法 + initWithFormat*/
     8       NSString *str1 = [[NSString alloc] initWithFormat:@"hello %d",10];
     9     
    10       /* 字符串的创建方式3  C字符串转OC字符串 */
    11       NSString *str2 = [[NSString alloc] initWithUTF8String:"jack"];
    12     
    13       /* OC 转 C */
    14       const char *str3 = [str2 UTF8String];
    15     
    16       /* 字符串的创建方式4:本地路径文件使用 */
    17       /* 从文件中读取字符串 涉及到中文就用encoding:NSUTF8StringEncoding */
    18       NSString *str4 = [[NSString alloc] initWithContentsOfFile:@"/Users/niko/Desktop/ios/test/infile.txt" encoding:NS                       UTF8StringEncoding error:nil];
    19       NSLog(@"%@",str4);
    20     
    21     /* 字符串的创建方式5:资源路径文件使用 */
    22     // URL :资源路径 格式:协议头://路径
    23     
    24     // 本地资源:file://
    25     NSURL *urlLocal=[[NSURL alloc] initWithString:@"file:///Users/niko/Desktop/ios/test/infile.txt"];
    26     
    27     // 网络资源)http://weibo.com/a.png
    28     NSURL *urlNet=[[NSURL alloc] initWithString:@"http://www.baidu.com"];
    29     
    30     /* 不带头的本地文件路径的本地资源*/
    31     NSURL *urlFile = [NSURL fileURLWithPath:@"/Users/niko/Desktop/ios/test/infile.txt"];
    32     /* 使用资源创建字符串 */
    33     NSString *str5 = [[NSString alloc] initWithContentsOfURL:urlFile  encoding:NSUTF8StringEncoding error:nil];
    34     NSLog(@"str5  %@",str5);
    35     
    36      开发中经常用这种类方法创建字符串
    37      一般都会有个类方法和对象方法配对,例如有initwithxxx(对象方法)就有stringWithxxx (类方法)
    38      
    39     [NSString stringWithFormat:@""];
    40      
    41     [NSString stringWithContentsOfFile:(NSString *) encoding:(NSStringEncoding) error:NSError *)];
    42 
    43     
    44     [NSString stringWithContentsOfURL:(NSURL *) encoding:(NSStringEncoding) error:(NSError *__autoreleasing *)];
    45      
    46     [NSURL URLWithString:(NSString *)];
    47      
    48     
    49     return 0;
    50 }

    2. 将字符串导入到文件的方法:

    int string_export()
    {
        //atomically---安全:写失败不会创建
        
        /* 字符串导出到文件方法1:writeToFile*/
        [@"niko 
     wirte" writeToFile:@"/Users/niko/Desktop/ios/test/infile.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
     
        /* 字符串导出到文件方法2 writeToURL:*/
        NSString *str = @"1234567";
        NSURL *url = [NSURL fileURLWithPath:@"/Users/niko/Desktop/ios/test/infile.txt"];
        
        [str writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil];
        return 0;
    }

     NSMutableString不可变字符串:

    可变字符串,就是字符串创建之后可以改写,可删除可添加新字符的字符串。反之上面的NSString创建的字符串就是不可变的,创建之后不能对原有的字符串进行增删改操作。

    常用的操作方法:

     1 NSMutableString *s1 = [NSMutableString stringWithFormat:@"hello i am mutale %d",100];
     2     /* s1是可变的 可往里面加东西,在原基础上加的不会反回新的 */
     3     [s1 appendString:@" apend "];
     4     
     5     /* 也可以删除某个范围的字符 am */
     6     /* 下面这种方法常用 先找到要删除字符的范围,再利用delete去删*/
     7     //[s1 deleteCharactersInRange:NSMakeRange(8, 2)];
     8     
     9      NSRange r1 =[s1 rangeOfString:@"am"];
    10     [s1 deleteCharactersInRange:r1];
    11     
    12     /*stringByAppendingString:方法在原有基础上拼接出一个新的,但并不会影响原有的字符串 
    13       返回值是不可变字符串*/
    14     NSString *s2 = [NSString stringWithFormat:@"hello i am not mutable"];
    15     NSString *s3 = [s2 stringByAppendingString:@"I am appeding"];
    16     NSLog(@"s1 = %@ 
    s2= %@ 
    s3=%@",s1,s2,s3);
    17     return 0;

    2015-03-24 11:26:34.824 NSString[539:35702] s1 = hello i  mutale 100 apend  

    s2= hello iam not mutable 

    s3=hello iam not mutablehahhaha


    四. NSArray 与 SMutableArray数组

     

    OC数组只能存放OC对象,正常情况下不能存放非Oc对象如int char等类型。

    NSArray 不可变数组,创建之后元素个数等都不能改变:

    NSArray数组的几种创建方式:

     
    int  NSArray_init()
    {
        //1. 创建一个空数组:永远是空数组后面不能在添加了
        NSArray *array1 = [NSArray array];
        
        //2. 创建初始为一个元素的数组 arrayWithObject:
        NSArray *array3 = [NSArray arrayWithObject:@"mike"];
        
        //3.创建初始为n多个对象的数组 arrayWithObjects
        // arrayWithObjects方法后面必须加nil结束,nil是数组结束的标记,
        NSArray *array2 = [NSArray arrayWithObjects:@"jack",@"niko",nil];
        
        //4.开发中快速创建数组 返回值是NSArray *不可变数组 */
        NSArray *array4 =@[@"jack",@"niko",@"moki"];


    数组中元素个数的访问:get方法,成员变量count

        // 数组元素个数
        NSLog(@"array %ld",array2.count);

    数组中元素的访问的方法两种:

     
        // 数组元素的访问方式1 根据位置访问
        NSLog(@"array2[1] %@",[array2 objectAtIndex:1]);
        
        // 数组元素的访问方式2 和C数组一样,array2[0]这是编译特性,编译器帮我们改写了
        NSLog(@"array2[0] %@",array2[0]);


    数组中元素遍历的四种方法:

    int main()
    {
        /* 1. 最粗暴直接的数组遍历 */
        NSLog(@"%@",array);
        
        /* 2. for循环慢速遍历 */
        for (int i = 0;i < array.count;i++)
        {
            NSLog(@"%d --- %@",i,array[i]);
        }
        
        /* 3.开发中快速遍历数组: 缺点不知道第几次
         方法一:加 i计数
         方法二:使用indexOfObjectint返回位置
         */
        for (id obj in array5)
        {
            // 找到obj元素在数组中的位置
            NSUInteger i = [array5 indexOfObject:obj];
            NSLog(@"%ld -- %@",i,obj);
            /* 退出循环:*/
            if(i == 0)
            {
                break; //这种方式退出
            }
            
        }
        /* 4. enumerateObjectsUsingBlockBLock 参数为block 代码块
             将当前元素和索引位置当做参数歘传给block ,stop用来停止遍历的 */
        [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            NSLog(@"%ld -- %@",idx,obj);
            /* 怎么退出循环 */
            if(idx == 5)
            {
                // stop为什么是指针呢?传指针可改变里面的值
                *stop = YES; //停止
            }
            
        }];
        
        return 0;
    }
     

    NSMutableArray:可变数组

    可变数组相比不可变数组,他能删除添加元素:

    可变数组的创建:

     

        /* 可变数组的创建 */
        // @[]只创建不可变数组,返回值是NSArray
        // 1.创建初始化为空的可变数组
        NSMutableArray *marray = [[NSMutableArray alloc] init];
        
        // 2.创建初始不为空的数组  arrayWithObjects方法后面必须加nil结束
        NSMutableArray *marray = [NSMutableArray arrayWithObjects:@"kimi",@"kate", nil];


    可变数组的添加元素方法:

     1     /*  添加元素 */
     2     // 1.正规的添加元素:- (void)addObject:(id)anObject;
     3     [marray addObject:[[Person alloc] init]];
     4     
     5     // 2.最直接的,编译器帮我们做
     6     marray[marray.count] = @"hello";
     7     
     8     
     9     // 3. OC数组只能存放OC对象,不能存放非Oc对象如int char,想要放就得另想办法
    10     //[marray addObject:15];
    11     //[marray addObject:nil];

    可变数组的删除元素方法:

    可变数组的遍历同不可变数组相同。
    五. NSSet 与 NSMutableSet

    1     /* 删除元素 */    
    2     // 1.删除指定元素
    3     [marray removeObject:@"hello"];
    4 
    5     // 2.根据位置删除元素
    6     [marray removeObjectAtIndex:0];
    7 
    8     // 3.删除所有元素
    9     [marray removeAllObjects];

    1.NSSet :既然作为集合就能存放很多对象,这是集合的特性.

    2.NSSetNSArray:

     共同点:

     1>> 都是集合,都能存放OC对象

     2>> 只能存放OC对象,不能存放非OC类对象类型(基本数据类型)

     3>> 本身不可变,都有一个可变的子类

     不同点

     NSArray:有序

     NSSet:无序

    3. NSSet的创建与初始化

    1     // 1. 初始化空的set
    2     NSSet *s1 = [NSSet set];
    3     // 2. 初始化一个对象的set
    4     NSSet *s2 = [NSSet setWithObject:@"hello"];
    5     // 3. 初始化多个个对象的set
    6     NSSet *s3 = [NSSet setWithObjects:@"jimi",@"kate",nil];


    4.从set中随机拿出一个元素,无序的随机的。

    3     /* 随机拿出一个元素 */
    4     NSString *str = [s1 anyObject];

    5. NSMutableSet的操作:

    1     // 创建 初始化
    2     NSMutableSet *s = [NSMutableSet set];
    3     //添加元素
    4     [s addObject:@"hhh"];
    5     
    6     //删除元素
    7     [s removeObject:@"hhh"];

    六. 字典NSDictionaryNSMutableDictionary:

    1. 字典作用: 通过一个key找到一个值,  key---->value

    2. 字典的创建

        /* 字典的键值对是没有顺序的 */
        /* 一个key 只能对应一个值,后面的覆盖前面的 */
        /* 允许不同的key对应同样的值 */
        // 1. 第一种创建方法
        NSDictionary *dict = [NSDictionary dictionaryWithObject:@"jack" forKey:@"j"];
        
        // 2. 第二种创建方法 直接输入Key 和 值 的数组
        NSArray *keys = @[@"a",@"b",@"c",@"d"];
        NSArray *objs = @[@"after",@"boy",@"cool",@"duang"];
        NSDictionary *dict2 = [NSDictionary dictionaryWithObjects:objs forKeys:keys];
        
        // 3. 第三种创建方法不常用,有更简单的见方法四
        NSDictionary *dict3 = [NSDictionary dictionaryWithObjectsAndKeys:
                               @"come on",@"c",
                               @"duangguang",@"d",
                               @"effective",@"e",
                               @"fuck",@"f",
                               @"global",@"g",nil
                              ];
       // 4.直接大括号的形式
        NSDictionary *dict4 = @{@"c":@"come on",@"d":@"duang"};
        
        // 5. 直接创建一个空的
        NSDictionary *dict5 = [NSDictionary dictionary];

    3.字典元素的访问

    1     /* 字典对象的查看  */
    2     id obj = [dict6 objectForKey:@"c"];
    3     /* 开发中常用这种*/
    4     id obj = dict3[@"c"]; // 编译器帮我们做好了装换,装换成上一步的代码


    4.字典中键值对的个数

     

       // 返回的是键值对的个数
        NSLog(@"%ld",dict2.count);


    5. 可变字典NSMutableDictionary:

    1)可变字典的操作:创建、添加、删除;

        //1. 创建一个空的可变字典
        NSMutableDictionary *mdict =[NSMutableDictionary dictionary];
        
        /* 添加键值对 */
        [mdict setObject:@"come" forKey:@"c"];
        [mdict setObject:@"duang" forKey:@"d"];
        
        /* 移除键值对 */
        [mdict removeObjectForKey:@"c"];
    
        
       /*可变数组不能使用大括号创建
        NSMutableDictionary *mdict2 = @{@"c":@"jack"};
        [mdict setObject:@"ending" forKey:@"e"];
        NSLog(@"%ld",mdict2.count);
        */
        /* 可变字典的遍历 */
        [mdict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
            NSLog(@"%@---%@",key,obj);
        }];
    
     






  • 相关阅读:
    大数据之MapReduce工作机制
    swoft +nginx 快速获取客户端的真实的IP
    JWT 介绍 详解
    swoft 切面编程 (AOP编程思想)
    CSP 编程模型
    Ubuntu 16.04 Mysql 重置密码
    ubuntu 提示:rm: cannot remove 'you-get/tmp': Directory not empty
    Ubuntu 系统升级到php7.2/7.3 (平滑升级)-朝花夕拾
    git clone 非标准的ssh端口(非22)端口
    Ubuntu 升级npm 以及安装cross-env 过程中遇到的问题
  • 原文地址:https://www.cnblogs.com/jianghg/p/4417193.html
Copyright © 2020-2023  润新知