zoukankan      html  css  js  c++  java
  • Objective-C数组初识

    1.数组的创建

    OC中数组存储对象的地址

    对象方法初始化

    1 NSArray * arr1 = [[NSArray alloc] initWithObjects:@"one",@"two",@"three", nil];
    2         
    3 NSLog(@"%@",arr1);

     类方法初始化

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

     

    创建数组时以nil结束

    使用@[]初始化

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

     

    2.数组存储基本数据类型 oc数组存储的是对象的地址 所以需要将基本数据类型的变量转变为对象的地址

    numberWithInt:(int)   numberWithFloat:(float)   numberWithDouble:(double)

    1 int age = 25;

    2 NSNumber * num = [NSNumber numberWithInt:age];  

    3 NSArray * arr5 = @[@"one",@"two",@"three",num];

    4 NSLog(@"%@",arr5);

     3.数组中可以存放自定义对象

    1         Person * p1 = [[Person alloc] initWithName:@"Tom" andAge:18];
    2         Person * p2 = [[Person alloc] initWithName:@"Lilei" andAge:20];
    3         Person * p3 = [[Person alloc] initWithName:@"Lily" andAge:25];
    4         
    5         NSArray * arr6 = @[p1,p2,p3];        
    1 #import <Foundation/Foundation.h>
    2 
    3 @interface Person : NSObject {
    4     NSString * _name;
    5     int _age;
    6     
    7 }
    8 - (id)initWithName:(NSString *)name andAge:(int)age;
    9 @end
    Person.h
     1 #import "Person.h"
     2 
     3 @implementation Person
     4 - (id)initWithName:(NSString *)name andAge:(int)age {
     5     if (self = [super init]) {
     6         _name = name;
     7         _age = age;
     8     }
     9     return self;
    10 }
    11 
    12 - (NSString *)description {
    13     return [NSString stringWithFormat:@"name:%@ age:%d",_name,_age];
    14 }
    15 
    16 @end
    Person.m

    - (NSString *)description; 该方法返回值即为打印数组时打印的字符串。

     

    5.获取数组中的对象 

     1 Person * p4 = [arr6 objectAtIndex:2]; 2 NSLog(@"p4 %@",[p4 description]);  

    6.获取数组中的个数

     1 NSUInteger count = [arr6 count]; 

     

    7.判断是否包含元素

    判断的是内容 而非地址 

     1         Person * p5 = [[Person alloc] initWithName:@"Jack" andAge:28];
     2         BOOL isContain = [arr6 containsObject:p5];
     3         
     4         if (isContain) {
     5             NSLog(@"包含");
     6         } else {
     7             NSLog(@"不包含");
     8         }
     9         
    10         NSString * str1 = [[NSString alloc] initWithFormat:@"one"];
    11         NSArray * arr7 = @[@"one",@"two",@"three"];
    12         BOOL isContain1 = [arr7 containsObject:str1];
    13         
    14         if (isContain1) {
    15             NSLog(@"包含");
    16         } else {
    17             NSLog(@"不包含");
    18         }

     

    8.数组排序

     1         // @selector
     2         NSArray * arr = @[@"a",@"g",@"f",@"d",@"e"];
     3         
     4         // YES 左边 > 右边
     5         // 左边 < 右边
     6         BOOL isGreater = [@"b" isGreaterThan:@"a"];
     7         NSLog(@"isGreater %d",isGreater);
     8         
     9 //        SEL sel = @selector(isGreaterThan:);
    10         NSLog(@"before %@",arr);
    11 //        NSArray * arr2 = [arr sortedArrayUsingSelector:sel];
    12         NSArray * arr2 = [arr sortedArrayUsingSelector:@selector(isGreaterThan:)];
    13         NSLog(@"after %@",arr2);
    14         
    15         // @selector
    16         // Block
    17         // 声明: 返回值 (^名字)(参数列表) = ^返回值 (参数列表) {
    18     
    19 //    }
    20         NSComparisonResult (^block)(id obj1,id obj2) = ^NSComparisonResult(id obj1, id obj2) {
    21             return [obj1 compare:obj2];
    22         };
    23         
    24         // Block
    25         NSArray * arr3 = [arr sortedArrayUsingComparator:block];
    26         
    27         [arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    28             return [obj1 isLessThan:obj2];
    29         }];        
    30         NSLog(@"after arr3 %@",arr3);
    数组排序

    @selector 传入一个方法名 也就是方法的地址  

    Block

    声明: 返回值 (^名字)(参数列表) = ^返回值 (参数列表) {

      操作

    }

     

    9.可变数组

      9.1 可变数组

    1 NSArray * arr1 = @[@"one",@"two",@"three"];
    2 
    3 NSMutableArray * muarr1 = [[NSMutableArray alloc] initWithArray:arr1];
    4 
    5 NSMutableArray * muarr2 = [[NSMutableArray alloc] initWithObjects:@"one",@"two",@"three", nil];

      NSMutableArray * muarr3 = @[@"one",@"two",@"three"];  并不能这样写

     

      9.2重置数组 1 [muarr2 setArray:@[@"one"]]; 2 NSLog(@"%@",muarr2); 

      9.3添加元素 1 [muarr2 addObject:@"two"];

    1         NSArray * arr2 = @[@"a",@"b",@"c"];
    2         // 把数组作为对象添加到数组中
    3         [muarr2 addObject:arr2];

     

      9.4插入元素

    1         // 插入对象
    2         [muarr2 insertObject:@"z" atIndex:1];
    3          [muarr2 addObject:@"one"];
    4         NSLog(@"%@",muarr2);

      9.5删除元素

            // 删除
            // 会通过对象地址,删除数组中所有的同一个地址的对象
            
            [muarr2 removeObject:@"one"];
            NSLog(@"%@",muarr2);
            
            // 删除 指定位置的对象
            [muarr2 removeObjectAtIndex:1];
            NSLog(@"%@",muarr2);
            // 删除全部对象
            [muarr2 removeAllObjects];
            NSLog(@"%@",muarr2);

     

     

    10.遍历数组

    [arr count]为数组的元素数

    [arr objectAtIndex:i] 为index处的元素

     1         NSArray * arr = @[@"英雄联盟",@"跑跑卡丁车",@"红色警戒"];
     2         NSLog(@"%@",arr);
     3         
     4         NSString * str = arr[0];
     5         NSLog(@"%@",str);
     6         for (int i = 0; i < [arr count]; i++) {
     7             NSLog(@"%@",[arr objectAtIndex:i]);
     8         }
     9         // 快速枚举
    10         for (NSString * str in arr) {
    11             NSLog(@"%@",str);
    12         }

     

    11.字符串与数组的相互转换

     1         NSString * str = @"I am a teacher";
     2         //componentsSeparatedByString: 用字符串来分割 用数组存储
     3         
     4         NSArray * arr = [str componentsSeparatedByString:@" "];
     5         NSLog(@"%@",arr);
     6         
     7         NSString * str2 = @"a";
     8         NSArray * arr1 = [str2 componentsSeparatedByString:@"a"];
     9         NSLog(@"%@",arr1);
    10         
    11         //componentsJoinedByString 用字符串拼接数组元素 范围一个新字符串
    12         NSString * str3 = [arr componentsJoinedByString:@"-"];
    13         NSLog(@"%@",str3);

     

    附加 @selector语法

     1 int main(int argc, const char * argv[]) {
     2     @autoreleasepool {
     3         NSLog(@"%d",cal(3,5, add));
     4         
     5         Dog * dog = [[Dog alloc] init];
     6 //        [dog bark];
     7         
     8         [dog eatAfterSel:@selector(rockTail)];
     9         
    10         
    11         
    12     }
    13     return 0;
    14 }
    main.m
    1 #import <Foundation/Foundation.h>
    2 
    3 @interface Dog : NSObject
    4 - (void)bark;
    5 - (void)rockTail;
    6 - (void)eat;
    7 - (void)eatAfterSel:(SEL)sel;
    8 
    9 @end
    Dog.h
     1 #import "Dog.h"
     2 
     3 @implementation Dog
     4 - (void)bark {
     5     NSLog(@"wangwang");
     6 }
     7 
     8 - (void)rockTail {
     9     NSLog(@"狗在深情地摇着尾巴");
    10 }
    11 
    12 - (void)eat {
    13     NSLog(@"狗在吃屎");
    14 }
    15 
    16 
    17 - (void)eatAfterSel:(SEL)sel {
    18     [self performSelector:sel];
    19     [self eat];
    20 }
    21 @end
    Dog.m

     

     

     

      

     

     

     

     

  • 相关阅读:
    「NOTE」支配树
    「SOL」支配 (2021省选A卷)
    「SOL」矩阵游戏 (2021省选A卷)
    「SOL」最差记者2 (LOJ / JOISC2016)
    「SOL」Nondivisible Prefix Sums(AtCoder)
    「SOL」Spaceship(LOJ/USACO)
    「NOTE」可持久化非旋Treap
    「SOL」事情的相似度(LOJ)
    FTP(File Transfer Protocol)——文件传输协议详解
    DHCP(Dynamic Host Configutation Protocol)——动态主机配置协议详解
  • 原文地址:https://www.cnblogs.com/gwkiOS/p/4924653.html
Copyright © 2011-2022 走看看