#import <Foundation/Foundation.h>
#import "Student.h" #pragma mark 创建一个数组
void arrayCreate() {
// 创建一个空的数组
NSArray *array = [NSArray array]; // 创建有1个元素的数组
array = [NSArray arrayWithObject:@""]; // 创建有多个元素的数组
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:];
NSLog(@"%@", str); int index = [array indexOfObject:@"c"];
NSLog(@"index=%i", index); [obj release];
} #pragma mark 数组的内存管理
void arrayMemory() {
//
Student *stu1 = [[Student alloc] init];
Student *stu2 = [[Student alloc] init];
Student *stu3 = [[Student alloc] init]; NSLog(@"stu1:%zi", [stu1 retainCount]); // 当把一个对象塞进数组中时,这个对象的计数器会加1,也就是说数组会对它做一次retain操作
//
NSArray *array = [[NSArray alloc] initWithObjects:stu1, stu2, stu3, nil]; NSLog(@"stu1:%zi", [stu1 retainCount]); NSLog(@"count=%zi", array.count); //
[stu1 release];
[stu2 release];
[stu3 release]; // 数组被销毁的时候,会对内部的所有元素都做一次release操作
//
[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:@""];
} #pragma mark 遍历数组1
void arrayFor1() {
Student *stu1 = [Student student];
NSArray *array = [NSArray arrayWithObjects:stu1, @"", @"", @"", nil];
int count = array.count;
for (int i = ; 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, @"", @"", @"", nil];
// 快速遍历
int i =;
for (id obj in array) {
NSLog(@"%i-%@", i, obj);
i++;
}
} #pragma mark 遍历数组3
void arrayFor3() {
Student *stu1 = [Student student];
NSArray *array = [NSArray arrayWithObjects:stu1, @"", @"", @"", nil];
[array enumerateObjectsUsingBlock:
^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%i-%@", idx, obj); // 如果索引为1,就停止遍历
if (idx == ) {
// 利用指针修改外面BOOL变量的值
*stop = YES;
}
}];
} #pragma mark 遍历数组4
void arrayFor4() {
Student *stu1 = [Student student];
NSArray *array = [NSArray arrayWithObjects:stu1, @"", @"", @"", 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 ;
}
//
// 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:@"", @"", nil]; NSArray *array2 = [array arrayByAddingObject:@""]; NSArray *array3 = [array arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:@"", @"", nil]]; NSLog(@"array:%@", array);
NSLog(@"array2:%@", array2);
NSLog(@"array3:%@", array3); NSArray *array4 = [NSArray arrayWithObjects:@"", @"", @"", @"", nil];
NSRange range = NSMakeRange(, );
NSArray *array5 = [array4 subarrayWithRange:range];
NSLog(@"array5:%@", array5);
} #pragma mark 数组的其他用法
void arrayOther() {
NSArray *array = [NSArray arrayWithObjects:@"", @"", @"", @"", 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:@"", @"", @"", @"", 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 ;
}

OC NSArray使用的更多相关文章

  1. OC NSArray 数组

    # OC NSArray 数组 NSArray常用方法 获取数组中第一位元素 array.firstObject 获取数组中最后一个元素 array.lastObject 获取数组中指定索引下标的元素 ...

  2. iOS - OC NSArray 数组

    前言 @interface NSArray<__covariant ObjectType> : NSObject <NSCopying, NSMutableCopying, NSSe ...

  3. OC——NSArray和NSMutableArray

    /*---------------------NSArray---------------------------*/ //创建数组 NSArray *array1 = [NSArray arrayW ...

  4. OC NSArray数组排序

    一.一般排序 // 排序 NSArray *arr = @["]; NSArray *newarr = [arr sortedArrayUsingSelector:@selector(com ...

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

    目的 1.NSArray 能够支持中文排序 2.NSLog 能够直接输出 NSArray 内的中文(事实上 java 直接打印数组也不能显示内容哈) 又是死去活来的搞了1个小时,分类实现.废话少说,上 ...

  6. JSPatch学习笔记

    本文参考JSPatch wiki :https://github.com/bang590/JSPatch/wiki 1.概念 JSPatch是一个轻量的JS引擎,能够使用JavaScript语言来调用 ...

  7. Foundation

    类:NSObject .NSString.NSMutableString.NSNumber.NSValue.NSDate.NSDateFormatter.NSRange.Collections:NSS ...

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

    类:NSObject .NSString.NSMutableString.NSNumber.NSValue.NSDate.NSDateFormatter.NSRange.Collections:NSS ...

  9. pop回指定控制器

    //OCNSArray *array = [NSMutableArray new]; array = self.navigationController.viewControllers; //1.返回 ...

随机推荐

  1. python2.7 输入&函数参数&路径表示&各种下标_含义

    1.Python2.x与3.x的input区别 input与python3不同,在python2.7中分为input()与raw_input() 其中input()返回的是int/float类型数据, ...

  2. 机器学习——LightGBM

    基础概念 LigthGBM是boosting集合模型中的新进成员,它和xgboost一样是对GBDT的高效实现,很多方面会比xgboost表现的更为优秀.原理上它和GBDT及xgboot类似,都采用损 ...

  3. Java web验证码

    绘制验证码的主要步骤: 1,设置宽度高度,验证码个数,干扰线个数,可选字符,背景颜色,字体格式 2,画干扰线,随机生成颜色,字体,字符 3,设置缓冲区,得到画笔,设置边框,读写数据,存储图片. 1,S ...

  4. 从websphere6.1迁移到weblogic10.3的问题总结--转

    http://blog.itpub.net/9399028/viewspace-692974/ 以前用weblogic低版本的时候,感觉挺容易的,也没什么要注意的,这次真正把应用从websphere迁 ...

  5. Linux修改命令行样式

    Linux修改Shell命令提示符及颜色 Linux修改Shell命令提示符及颜色 1. Linux登录过程中加载配置文件顺序: /etc/profile → /etc/profile.d/*.sh ...

  6. [转]微信小程序开发踩坑记录

    本文转自:http://www.cnblogs.com/NKnife/p/6283605.html 1.由于小程序wx.request()方法是异步的,在app.js执行ajax后,各分页加载app. ...

  7. MVC 下拉框联动效果(单选)

    下拉框联动效果,我们以部门--职位为例,选择部门时,关联到该部门的职位.下拉框的写法就不多说了,详细请参照前文. 视图: 其中,dept是部门的属性,deptlist是部门下拉框的属性,job是职位的 ...

  8. django-admin管理后台高级自定义

    django自带的admin后台管理系统,在很多网站中被称为django的杀手级的应用.那么django-admin的适用情形倒底有哪些呢,一般 来说对于大型的商业性的项目通常不用采用django-a ...

  9. Cheatsheet: 2017 02.01 ~ 02.28

    Web Debouncing and Throttling Explained Through Examples What is TypeScript? An Absolute Beginner's ...

  10. [android] 练习使用ListView(一)

    练习使用ListView,BaseAdapter,先展示文字的,再练习图片的 MainActivity.java package com.android.test; import android.ap ...