OC NSArray使用

#import <Foundation/Foundation.h>
#import "Student.h"

#pragma mark 创建一个数组
void arrayCreate() {
    // 创建一个空的数组
    NSArray *array = [NSArray array];

    // 创建有1个元素的数组
    array = [NSArray arrayWithObject:@"123"];

    // 创建有多个元素的数组
    array = [NSArray arrayWithObjects:@"a", @"b", @"c", nil];

    int count = [array count];
    // count = array.count;
    NSLog(@"%i", count);
}

#pragma mark 数组的简单使用
void arrayUse() {
    NSObject *obj = [[NSObject alloc] init];
    NSArray *array = [NSArray arrayWithObjects:@"a", @"b", @"c" , obj, nil];
    // 判断是否包含了某个元素
    if ([array containsObject:@"a"]) {
        NSLog(@"包含了字符串a");
    }

    NSString *last = [array lastObject];
    NSLog(@"last=%@", last);

    NSString *str = [array objectAtIndex:1];
    NSLog(@"%@", str);

    int index = [array indexOfObject:@"c"];
    NSLog(@"index=%i", index);

    [obj release];
}

#pragma mark 数组的内存管理
void arrayMemory() {
    // 1
    Student *stu1 = [[Student alloc] init];
    Student *stu2 = [[Student alloc] init];
    Student *stu3 = [[Student alloc] init];

    NSLog(@"stu1:%zi", [stu1 retainCount]);

    // 当把一个对象塞进数组中时,这个对象的计数器会加1,也就是说数组会对它做一次retain操作
    // 2
    NSArray *array = [[NSArray alloc] initWithObjects:stu1, stu2, stu3, nil];

    NSLog(@"stu1:%zi", [stu1 retainCount]);

    NSLog(@"count=%zi", array.count);

    // 1
    [stu1 release];
    [stu2 release];
    [stu3 release];

    // 数组被销毁的时候,会对内部的所有元素都做一次release操作
    // 0
    [array release];
}

#pragma mark 给数组里面的元素发送消息
void arrayMessage() {
    Student *stu1 = [Student student];
    Student *stu2 = [Student student];
    Student *stu3 = [Student student];

    NSArray *array = [NSArray arrayWithObjects:stu1, stu2, stu3, nil];
    // 让数组里面的所有对象都调用test方法
    // [array makeObjectsPerformSelector:@selector(test)];
    [array makeObjectsPerformSelector:@selector(test2:) withObject:@"123"];
}

#pragma mark 遍历数组1
void arrayFor1() {
    Student *stu1 = [Student student];
    NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil];
    int count = array.count;
    for (int i = 0; i<count; i++) {
        // id == void *
        id obj = [array objectAtIndex:i];
        NSLog(@"%i-%@", i, obj);
    }
}

#pragma mark 遍历数组2
void arrayFor2() {
    Student *stu1 = [Student student];
    NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil];
    // 快速遍历
    int i =0;
    for (id obj in array) {
        NSLog(@"%i-%@", i, obj);
        i++;
    }
}

#pragma mark 遍历数组3
void arrayFor3() {
    Student *stu1 = [Student student];
    NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil];
    [array enumerateObjectsUsingBlock:
     ^(id obj, NSUInteger idx, BOOL *stop) {
        NSLog(@"%i-%@", idx, obj);

         // 如果索引为1,就停止遍历
         if (idx == 1) {
             // 利用指针修改外面BOOL变量的值
             *stop = YES;
         }
    }];
}

#pragma mark 遍历数组4
void arrayFor4() {
    Student *stu1 = [Student student];
    NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil];

    // 获取数组的迭代器
    // NSEnumerator *enumerator = [array objectEnumerator];
    // 反序迭代器(从尾部开始遍历元素)
    NSEnumerator *enumerator = [array reverseObjectEnumerator];

    // allObjects是取出没有被遍历过的对象
    NSArray *array2 = [enumerator allObjects];
    NSLog(@"array2:%@", array2);

    // 获取下一个需要遍历的元素
    id obj = nil;
    while (obj = [enumerator nextObject]) {
        NSLog(@"obj=%@", obj);
    }
}

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        arrayFor4();
    }
    return 0;
}
//
//  main.m
//  Foundation7-NSArray2
//
//  Created by mj on 13-4-5.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Student.h"

#pragma mark 派生出新的数组
void arrayNew() {
    NSArray *array = [NSArray arrayWithObjects:@"1", @"2", nil];

    NSArray *array2 = [array arrayByAddingObject:@"3"];

    NSArray *array3 = [array arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:@"4", @"5", nil]];

    NSLog(@"array:%@", array);
    NSLog(@"array2:%@", array2);
    NSLog(@"array3:%@", array3);

    NSArray *array4 = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil];
    NSRange range = NSMakeRange(1, 2);
    NSArray *array5 = [array4 subarrayWithRange:range];
    NSLog(@"array5:%@", array5);
}

#pragma mark 数组的其他用法
void arrayOther() {
    NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil];
    // 1-2-3-4
    // 利用分隔符-拼接所有的数组元素
    NSString *str = [array componentsJoinedByString:@"-"];
    NSLog(@"%@", str);

    // 将一个数组写入文件(生成的是一个xml文件)
    NSString *path = @"/Users/apple/Desktop/array.xml";
    [array writeToFile:path atomically:YES];

    path = @"/Users/apple/Desktop/array.txt";
    // 从文件中读取数组内容(文件有严格的格式要求)
    NSArray *array2 = [NSArray arrayWithContentsOfFile:path];
    NSLog(@"array2:%@", array2);
}

#pragma mark 数组排序1
void arraySort1() {
    NSArray *array = [NSArray arrayWithObjects:@"2", @"3", @"1", @"4", nil];

    // 返回一个排好序的数组,原来数组的元素顺序不会改变
    // 指定元素的比较方法:compare:
    NSArray *array2 = [array sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"array2:%@", array2);
}

#pragma mark 数组排序2
void arraySort2() {
    Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li"];
    Student *stu2 = [Student studentWithFirstname:@"LongHu" lastname:@"Huang"];
    Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li"];
    Student *stu4 = [Student studentWithFirstname:@"Jian" lastname:@"Xiao"];
    NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil];

    // 指定排序的比较方法
    NSArray *array2 = [array sortedArrayUsingSelector:@selector(compareStudent:)];

    NSLog(@"array2:%@", array2);
}

#pragma mark 数组排序3
void arraySort3() {
    Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li"];
    Student *stu2 = [Student studentWithFirstname:@"LongHu" lastname:@"Huang"];
    Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li"];
    Student *stu4 = [Student studentWithFirstname:@"Jian" lastname:@"Xiao"];
    NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil];

    // 利用block进行排序
    NSArray *array2 = [array sortedArrayUsingComparator:
     ^NSComparisonResult(Student *obj1, Student *obj2) {
         // 先按照姓排序
         NSComparisonResult result = [obj1.lastname compare:obj2.lastname];
         // 如果有相同的姓,就比较名字
         if (result == NSOrderedSame) {
             result = [obj1.firstname compare:obj2.firstname];
         }

         return result;
    }];

    NSLog(@"array2:%@", array2);
}

#pragma mark 数组排序4-高级排序
void arraySort4() {
    Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li" bookName:@"book1"];
    Student *stu2 = [Student studentWithFirstname:@"LongHu" lastname:@"Huang" bookName:@"book2"];
    Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li" bookName:@"book2"];
    Student *stu4 = [Student studentWithFirstname:@"Jian" lastname:@"Xiao" bookName:@"book1"];
    NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil];

    // 1.先按照书名进行排序
    // 这里的key写的是@property的名称
    NSSortDescriptor *bookNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES];
    // 2.再按照姓进行排序
    NSSortDescriptor *lastnameDesc = [NSSortDescriptor sortDescriptorWithKey:@"lastname" ascending:YES];
    // 3.再按照名进行排序
    NSSortDescriptor *firstnameDesc = [NSSortDescriptor sortDescriptorWithKey:@"firstname" ascending:YES];
    // 按顺序添加排序描述器
    NSArray *descs = [NSArray arrayWithObjects:bookNameDesc, lastnameDesc, firstnameDesc, nil];

    NSArray *array2 = [array sortedArrayUsingDescriptors:descs];

    NSLog(@"array2:%@", array2);
}

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        arraySort4();
    }
    return 0;
}
时间: 2024-10-12 14:34:13

OC NSArray使用的相关文章

OC -- NSArray NSMutableArray

NSArray  NSMutableArray的声明 注:NSArray与NSMutableArray中只能存放OC对象类型数据,不能存放int, float, double, struct, enum类型数据 NSArray *array1 = @[@"first", @"second", @"third"]; NSArray *array2 = [NSArray arrayWithObjects:@"one", @&quo

ios ——OC——NSArray的用法

NSArray 从本质上讲,NSArray 就是一个存放对象的容器. NSArray只能存储Objective-C 对象,而不能存储C语言中的基本数据类型,比如:int,flot,指针等. 在使用NSArray时,需特别注意,必须以nil 结尾.以此来代表结束. NSArray 创建的是静态数组,一旦创建之后,就再也不能添加和删除数组中的对象了. 1.NSArray的创建: 类方法创建数组: NSArray *array1 = [NSArray arrayWithObject:@"obj&quo

死去活来的OC NSArray 中文排序 及输出

目的 1.NSArray 可以支持中文排序 2.NSLog 可以直接输出 NSArray 内的中文(其实 java 直接打印数组也不能显示内容哈) 又是死去活来的搞了1个小时,分类实现,废话少说,上代码 main.m #import <Foundation/Foundation.h #import "NSArray+Log.h" int main(int argc, const char * argv[]) { NSArray * arr = [NSArray arrayWith

OC——NSArray和NSMutableArray

/*---------------------NSArray---------------------------*/ //创建数组 NSArray *array1 = [NSArray arrayWithObject:@"1"]; NSArray *array2 = [NSArray arrayWithObjects:@"1",@"2",@"3", nil]; NSArray *array3 = [NSArray array

Swift与OC的语法简单对比

01-常量与变量   学习swift第一步打印Hello World print("Hello World") swift是不用加分号的 什么是常量? 常量是在程序运行过程中不能改变值的量 什么时变量? 变量是可以在程序运行过程中不断变化的量 在swift当中常量和变量必须在使用前声明 用let来声明常量,用var来声明变量 常量定义方式: 可以用任何你喜欢的字符作为常量和变量名,包括Unicode字符 常量与变量名不能包含以下: 数学符号,箭头,保留的(或者非法的)Unicode码位

ios9基础知识总结(foundation)笔记

类:NSObject .NSString.NSMutableString.NSNumber.NSValue.NSDate.NSDateFormatter.NSRange.Collections:NSSet.NSArray(Ordered.Copy).NSMutableArray.NSMutableSet.NSDictionary ====================================================================================

数组的基本使用

/* 数组定义: OC: 有值数组 NSArray *arr0 = @[@1, @2, @3]; NSArray *arr1 = [NSArray arrayWithObjects:@1, @2, @3, nil]; NSLog(@"%@", arr1); 空数组 NSArray *arr2 = @[]; NSArray *arr3 = [NSArray array]; NSLog(@"%@", arr3); 不可变数组:NSArray 可变数组:NSMutable

OC学习篇之---Foundation框架中的NSArray类和NSMutableArray类

在之前的一篇文章中介绍了Foundation框架中的NSString类和NSMutableString类: http://blog.csdn.net/jiangwei0910410003/article/details/41788223 今天我们继续来看一下Foundation框架中的NSArray类和NSMutableArray类,其实NSArray类和Java中的List差不多,算是一种数据结构,当然我们从这两个类可以看到,NSArray类是不可变的,NSMutableArray类是可变的.

oc/object-c/ios哪种遍历NSArray/NSDictionary方式快?测试报告

做app的时候,总免不了要多次遍历数组或者字典.究竟哪种遍历方式比较快呢?我做了如下测试:首先定义测试用宏: ? 1 2 3 4 5 6 7 8 9 #define MULogTimeintervalBegin(INFO) NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];\ NSTimeInterval duration = 0;\ NSLog(@"MULogTimeintervalBegin:%@", IN