OC基础 NSArray 容器

 

 

@autoreleasepool {
        // insert code here...
        NSLog(@"Hello, World!");
        //c语言中的数组问题
        int arr[10]={1,2,3,4,5,6,7,8,9,0};
        //c语言的数组只能存同类型的值
        //固定大小的数组很难确定大小
        int* p=(int*)malloc(10*sizeof(int));
        //动态数组的内存管理需要程序员自己完成
        //如果是对象数组,数组中的对象的内存管理也需要程序员自己完成
        
        //oc中提供了NSArray
        //1.NSArray 不可变的数组对象
        //2.NSMutableArray 可变的数组对象
        
        //NSArray的创建
        //1.
        NSArray* array=[[NSArray alloc]init];
        //2.
        NSArray* array2=[NSArray array];
        //3.
        NSArray* array3=[NSArray arrayWithObjects:@"one",@"two",@"three" ,nil];
        //NSArray中只能存放oc中的对象类型,c语言中的基本数据类型,enum,结构体不能存放在NSArray
        //nil ,Null 不能存放在NSArray,NSArray,以nil(NULL,0)为结束
        //4.
        NSArray* array4=@[@"one",@"two",@"three"];
        
        //数组的遍历
        
        //1.快速遍历
        for(id obj in array4) //for each
        {
            
            NSLog(@"%@",obj);
        }
        //2.下标法
        for (int i=0;i<[array4 count];i++)
        {
            NSLog(@"%@",array4[i]);
        }
        //3.
        for (int i=0;i<[array4 count];i++)
        {
            NSLog(@"%@",[array4 objectAtIndex:i]);
            
            
        }
        //4.
        NSLog(@"%@",array4);
        
        //5.通过枚举对象
        NSEnumerator* enumator=[array4 objectEnumerator];
        id obj;
        while (obj =[enumator nextObject])
        {
            NSLog(@"%@",obj);
        }
        
        
        

 

 

数组比较

oc排序

 //二 。排序
        NSArray* array5=@[@"one",@"two",@"three"];
        NSArray* sorted=[array5 sortedArrayUsingSelector:@selector(compare:)];
        
        NSLog(@"%@",sorted);

 

自定义排序

@interface Person : NSObject
@property NSString* lastName;
@property NSString* firstName;
@property int age;
-(instancetype)initWithFirstName:(NSString*)f andLastName:(NSString*)l andAge:(int)a;

-(NSComparisonResult)compareWithName:(Person*)p;
-(NSComparisonResult)compareWithAge:(Person*)p;



@end

 

 

#import "Person.h"

@implementation Person
-(instancetype)initWithFirstName:(NSString*)f andLastName:(NSString*)l andAge:(int)a
{
    self=[super init];
    if(self)
    {
        _firstName=f;
        _lastName=l;
        _age=a;
    }
    return self;
}


-(NSComparisonResult)compareWithName:(Person*)p;
{
    //第一个姓名与firstname是否一样
    NSComparisonResult result=[_firstName compare:p.firstName];
    if (result==NSOrderedSame)
    {
        return [_lastName compare:p.lastName];
        
    }
    else{
        return result;
    }
        
}

-(NSComparisonResult)compareWithAge:(Person*)p
{
    
    if(_age==p.age)
    {
        return NSOrderedSame;
    }
    else if(_age>p.age)
    {
        return NSOrderedDescending;
    }
    else{
        return NSOrderedAscending;
    }
    
}
-(NSString*)description
{
    
    return [NSString stringWithFormat:@"firstName:%@ lastName:%@ age:%i",_firstName,_lastName,_age ];
}

@end

实现

 

//自定义数组排序
        Person* p1=[[Person alloc]initWithFirstName:@"Tom" andLastName:@"Joe" andAge:25];
        Person* p2=[[Person alloc]initWithFirstName:@"Tom" andLastName:@"Smith" andAge:21];
        Person* p3=[[Person alloc]initWithFirstName:@"Ace" andLastName:@"WashingTon" andAge:23];
        
        
        NSArray* person=[NSArray arrayWithObjects:p1,p2,p3,nil];
        NSLog(@"%@",person);
        NSArray* sortedPerson=[person sortedArrayUsingSelector:@selector(compareWithAge:)];
        NSLog(@"%@",sortedPerson);
        

 

 

可变数组

 // 可变的对象数组
        NSMutableArray* array6=[NSMutableArray array];
        
        //添加对象
        [array6 addObject:@"one"];
        [array6 addObject:@"two"];
        [array6 addObject:@"three"];
        [array6 addObject:@"four"];
    
        NSLog(@"%@",array6);
        
        //删除对象 通过比较 删除
        [array6 removeObject:@"two"];
        NSLog(@"%@",array6);
        // 删除最后一个对象
        [array6 removeLastObject];
        NSLog(@"%@",array6);
//        [array6 removeAllObjects];
        NSLog(@"%@",array6);
        
        // 插入对象
        
        [array6 insertObject:@"five" atIndex:0];
        NSLog(@"%@",array6);
        
        // 利用不可变 创建可变数组对象
        NSArray* array7=@[@1,@2,@3,@4]; //NSMumber
        NSMutableArray* array8=[NSMutableArray arrayWithArray:array7];
        NSLog(@"%@",array8);

str. list 相互转换

 

 

//字符串对象的分割  str to list
        NSString* str=@"how are you ?I am fine";
        NSArray* words =[str componentsSeparatedByString:@"?"];
        NSLog(@"%@",words);
        //字符串对象拼接 list to str
        NSArray* words2 = @[@"one",@"two",@"three"];
        NSString* str2=[words2 componentsJoinedByString:@"_"];
        NSLog(@"%@",str2);

 

posted @ 2020-06-25 22:16  逆欢  阅读(213)  评论(0编辑  收藏  举报