zoukankan      html  css  js  c++  java
  • [OC Foundation框架

    NSArray是不可变的,不能先创建再添加元素

    NSArray可以放入任何OC对象,但不能放入基本数据类型、结构体、枚举等非OC对象
    不能存储nil
     
    A.常用方法1
    1. 创建
    2. 返回用量
    3. 是否含有某元素
    4. 最后的元素
    5. 取得某位置的元素
    当一个对象放入数组的时候,这个对象的计数器加1
    复制代码
     1 #pragma mark create a array
     2 void arrayCreate()
     3 {
     4     //Create an empty array
     5     NSArray *array = [NSArray array];
     6    
     7     //Create an array with one element
     8     array = [NSArray arrayWithObject:@"123"];
     9    
    10     array = [NSArray arrayWithObjects:@"a", @"b", @"d", nil];
    11    
    12     NSUInteger count =[array count];
    13     NSLog(@"%@", count);
    14    
    15     [array release];
    16 }
    复制代码

    最后的nil用作标示数组的结束,不会被存储进数组元素,不允许在其他地方插入
    error:    array = [NSArray arrayWithObjects:@"a”, nil, @"d"];

    复制代码
     1 #pragma mark common use
     2 void arrayUser()
     3 {
     4     NSArray *array = [NSArray arrayWithObjects:@"a", @"b", @"c", nil];
     5     if ([array containsObject:@"a"])
     6     {
     7         NSLog(@"Contans a");
     8     }
     9    
    10     NSString *last = [array lastObject];
    11     NSLog(@"%@", last);
    12    
    13     NSString *str = [array objectAtIndex:1];
    14     NSLog(@"%@", str);
    15    
    16     NSUInteger location = [array indexOfObject:@"c"];
    17     NSLog(@"%@", location);
    18    
    19     [array release];
    20 }
    21 
    22 #pragma memory manage
    23 void memoryManage()
    24 {
    25     Student *stu1 = [[Student alloc] init];
    26     Student *stu2 = [[Student alloc] init];
    27     Student *stu3 = [[Student alloc] init];
    28    
    29     NSLog(@"stu1: %zi", [stu1 retainCount]);
    30    
    31     NSArray *array = [[NSArray alloc] initWithObjects:stu1, stu2, stu3, nil];
    32    
    33     NSLog(@"stu1: %zi", [stu1 retainCount]);
    34    
    35     [stu1 release];
    36     [stu2 release];
    37     [stu3 release];
    38    
    39     [array release];
    40    
    41 }
    复制代码
     
    B.常用方法2
     
    1.给数组所有元素发送消息,调用同一个方法
    复制代码
     1 void arrayMessage()
     2 {
     3     Student *stu1 = [[Student alloc] init];
     4     Student *stu2 = [[Student alloc] init];
     5     Student *stu3 = [[Student alloc] init];
     6    
     7     NSArray *array = [[NSArray alloc] initWithObjects:stu1, stu2, stu3, nil];
     8    
     9     [array makeObjectsPerformSelector:@selector(test2:) withObject:@"test2"];
    10    
    11     [stu1 release];
    12     [stu2 release];
    13     [stu3 release];
    14    
    15     [array release];
    16 } 
    复制代码
     
    2.遍历
    (1)for 循坏
    复制代码
     1 void arrayLoop()
     2 {
     3     Student *stu = [[Student alloc]init];
     4     NSArray *array = [[NSArray alloc] initWithObjects:stu, @"2", @"3", nil];
     5     unsigned long count = array.count;
     6 //    for (int i=0; i<count; i++)
     7 //    {
     8 //        id obj = [array objectAtIndex:i];
     9 //        NSLog(@"%i - %@", i, obj);
    10 //    }
    11    
    12     int i = 0;
    13     for (id obj in array)
    14     {
    15         NSLog(@"%i - %@", i, obj);
    16         i++;
    17     }
    18    
    19     [stu release];
    20     [array release];
    21 }
    复制代码
     
    (2)使用Block进行循环处理
    复制代码
     1 void arrayLoopByBlock()
     2 {
     3     Student *stu = [[Student alloc]init];
     4     NSArray *array = [[NSArray alloc] initWithObjects:stu, @"2", @"3", nil];
     5    
     6    
     7     [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
     8         NSLog(@"%zi - %@", idx, obj);
     9         if (idx == 1)
    10         {
    11             *stop = YES;
    12         }
    13     }];
    14    
    15     [stu release];
    16     [array release];
    17 }
    复制代码
     
    (3).迭代器
    复制代码
     1 void arrayLoopByEnumerator()
     2 {
     3     Student *stu = [[Student alloc]init];
     4     NSArray *array = [[NSArray alloc] initWithObjects:stu, @"2", @"3", nil];
     5    
     6 //    NSEnumerator *e = [array objectEnumerator];
     7     NSEnumerator *e = [array reverseObjectEnumerator];
     8     id obj = nil;
     9     while (obj = [e nextObject])
    10     {
    11         NSLog(@"The element is %@", obj);
    12     }
    13    
    14     [stu release];
    15     [array release];
    16 }
    复制代码
    创建整数数组
    (1)
        
    1      NSArray *array61 = [NSArray arrayWithObjects:@0, @1, @2, @3, @4, @5, @6, @7, @8, @9, nil];
    实际上是把数字自动转化成了NSNumber类型
     
     
    (2)不能用于NSMutableArray
            
    1 NSArray *array61 = @[@0, @1, @2, @3, @4, @5, @6, @7, @8, @9];
     
     
     
    如果一件事情你觉得难的完不成,你可以把它分为若干步,并不断寻找合适的方法。最后你发现你会是个超人。不要给自己找麻烦,但遇到麻烦绝不怕,更不要退缩。 电工查找电路不通点的最快方法是:分段诊断排除,快速定位。你有什么启示吗? 求知若饥,虚心若愚。 当你对一个事情掌控不足的时候,你需要做的就是“梳理”,并制定相应的规章制度,并使资源各司其职。
  • 相关阅读:
    一枚渣硕的2019校招记录
    PLT hook笔记
    从排序数组中删除重复项
    golang刷Leetcode系列 --- 实现strStr()
    Docker镜像浅谈
    golang刷Leetcode系列 --- 加1
    ubuntu包管理机制
    LeetCode 234——回文链表
    LeetCode 19——删除链表的倒数第N个节点(JAVA)
    LeetCode 160——相交链表(JAVA)
  • 原文地址:https://www.cnblogs.com/wvqusrtg/p/4504406.html
Copyright © 2011-2022 走看看