码迷,mamicode.com
首页 > 编程语言 > 详细

不可变数组NSArray

时间:2015-12-15 15:54:34      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:

//
//  main.m
//  OC数组
//
//  Created by MAC on 15/12/15.
//  Copyright © 2015年 MAC. All rights reserved.
//

#import <Foundation/Foundation.h>
@interface Customer :NSObject
@property int cid;
@property NSString *name;
@property int age;
@end
@implementation Customer

-(NSString*)description{  //描述信息
    return [NSString stringWithFormat:@"%d,%@,%d",_cid,_name,_age];
}

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
      // 1.NSString NSArray Foundation
        //直接赋值
        NSArray *array = @[@"a",@"b",@"c",@"d",@"e"];
        
        NSObject *obj1 = [[NSObject alloc]init];
          NSObject *obj2= [[NSObject alloc]init];
        NSArray *array2 = @[obj1,obj2];
        //对象列表
        NSArray *array3 = [NSArray arrayWithObjects:@"one",@"Two",@"Three",@"four",@"five",@"six", nil];
          NSArray *array4 = [NSArray arrayWithObjects:obj1,obj2, nil];
        
        //获得数组大小
        NSUInteger r = array3.count;
        NSLog(@"%ld",r);
        //获得某个索引位置的元素
        NSString *item = [array3 objectAtIndex:1];
        NSLog(@"%@",item);
        //遍历
//        for (int i = 0; i<r; i++) {
//            NSLog(@"%@",[array3 objectAtIndex:i]);
//        }
        //增强型for循环
        for (id i in array3) {
            NSLog(@"%@",i);
        }
        //获得对象在数组中的位置
        r = [array3 indexOfObject:@"Two"];
        NSLog(@"%ld",r);
        NSRange re  = NSMakeRange(1, 5);
        r = [array3 indexOfObject:@"five" inRange:re];  //在这范围内的位置
        NSLog(@"%lu",r);
        
        Customer *c = [[Customer alloc]init];
        c.cid = 1001;
        c.name = @"tom";
        c.age = 18;
        
        Customer *c1 = [[Customer alloc]init];
        c1.cid = 1002;
        c1.name = @"kite";
        c1.age = 12;
        
        Customer *c3 = [[Customer alloc]init];
        c3.cid = 1003;
        c3.name = @"rose";
        c3.age = 20;
        
        NSArray *aa = @[c,c1,c3];
        NSString *it = [aa componentsJoinedByString:@";"];
        NSLog(@"%@",it);   //返回的是地址 需要在实现文件加入description方法返回具体信息
        NSArray *x1 = @[@"1",@"2",@"3",@"4",@"5",@"6"];
        NSArray *x2 = @[@"a",@"b",@"c"];
        //在数组后面增加元素
        NSArray *x3 = [x1 arrayByAddingObject:@"www"];
        NSLog(@"%@",x3);
        //两个数组连接在一起
        x3 = [x1 arrayByAddingObjectsFromArray:x2];
        NSLog(@"%@",x3);
        //判断数组里面是否包含某个对象
        BOOL b = [x1 containsObject:@"1"];
        NSLog(@"%d",b);
        //枚举  逆序是reverseObjectEnumerator
        NSEnumerator *e = [x2 objectEnumerator];
        NSString *s;
        while ((s=[e nextObject])!=nil) {
            NSLog(@"%@",s);
        }
        //排序 sortedArrayUsingComparator
        x1 = [x1 sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            NSInteger a = [obj1 integerValue];
            NSInteger b = [obj2 integerValue];
            if (a>b) {
                return NSOrderedAscending;
            }else if (a<b){
                return NSOrderedDescending;
            }else{
                return NSOrderedSame;
            }
        }];
        NSLog(@"%@",x1);
        //多个实例对象按照年龄来排序
        NSArray *aa1 = @[c,c1,c3];
        NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO];//描述规则
        
        NSArray *temp = [NSArray arrayWithObject:sd];
        
        aa1 = [aa1 sortedArrayUsingDescriptors:temp];
        NSLog(@"%@",aa1);
        //把数组写入文件
        NSString *path  =@"/Users/mac/desktop/test.txt";
        BOOL q = [x2 writeToFile:path atomically:YES];
        NSLog(@"%d",q);
        
    }
    return 0;
}

 

不可变数组NSArray

标签:

原文地址:http://www.cnblogs.com/WJR12/p/5048522.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!