今天学习了NSArray的遍历和排序,现在在这里做一下总结:

遍历现在实现了四中方法:

排序大概有三中方法:(代码中都有注释)

关于对象的排序还是以Student和Book为例 每个Student持有一个Book.

主函数:

//
// main.m
// NSArray
//
// Created by WildCat on 13-7-25.
// Copyright (c) 2013年 wildcat. All rights reserved.
// #import <Foundation/Foundation.h>
#import "Student.h"
#pragma mark 创建一个集合
void arrayTest(){
//创建一个数组
NSArray *array=[NSArray array]; array=[NSArray arrayWithObjects:@"123",@"sf",@"dsg", nil];
NSLog(@"array is:%@",array); NSLog(@"tength is: %zi",array.count);
} #pragma mark 给数组里的元素发送消息
void arrayMessage(){
Student *stu1=[Student student];
Student *stu2=[Student student];
Student *stu3=[Student student];
NSArray *array=[NSArray arrayWithObjects:stu1,stu2,stu3, nil];
//让数组里的元素调用addMessage方法
[array makeObjectsPerformSelector:@selector(addMessage:) withObject:@"你好。"]; } #pragma mark 数组遍历(四种方法) void arrayLoop(){ Student *stu1=[Student student];
NSArray *array=[NSArray arrayWithObjects:stu1,@"1",@"2",@"3", nil];
//方法一 for循环
for (int i=0; i<array.count; i++) {
NSLog(@"%i->%@",i,[array objectAtIndex:i]);
} //方法二
int i=0;
for (id obj in array) {
NSLog(@"%i->%@",i,obj);
i++;
}
//方法三 通过Block遍历:官方建议使用block
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%zi->%@",idx,obj);
//当执行到第3个时停止
if (idx==2){
*stop=YES;
}
}]; //方法四 通过迭代器遍历 //获得一个迭代器
NSEnumerator *enumerator=[array objectEnumerator];
id next=nil;
while (next=[enumerator nextObject]) {
NSLog(@"next->%@",next);
} //反向遍历
NSEnumerator *rvenumerator=[array reverseObjectEnumerator];
id rvnext=nil;
while (rvnext=[rvenumerator nextObject]) {
NSLog(@"rvnext->%@",rvnext);
} } #pragma mark 数组排序
void arraySort(){ NSArray *array=[NSArray arrayWithObjects:@"4",@"1",@"2",@"3", nil];
//排完后产生一个新的数组
//指定元素的排序方法 compare:
NSArray *array2=[array sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"排序后:%@",array2);
}
#pragma mark 对数组中的Student对象进行排序:
//先按照性进行排序,后按照名进行排序(两种方法)
void studentSort(){ Student *stu1=[Student initWithFirstName:@"lianjie" LastName:@"Li"];
Student *stu2=[Student initWithFirstName:@"dehua" LastName:@"Liu"];
Student *stu3=[Student initWithFirstName:@"xingle" LastName:@"Li"];
Student *stu4=[Student initWithFirstName:@"long" LastName:@"Cheng"];
NSArray *array=[NSArray arrayWithObjects:stu1,stu2,stu3,stu4, nil]; //第一种方法
NSArray *array2=[array sortedArrayUsingSelector:@selector(compareStudent:)];
NSLog(@"排序后:%@",array2);
//第二种方法 通过Block排序
NSArray *array3=[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(@"通过Block排序后:%@",array3); } #pragma mark 对数组中的Student对象进行排序: //对数组中的Student对象进行排序:先按照性进行排序,后按照名进行排序(另一种方法)
void studentSort2(){ Student *stu1=[Student initWithFirstName:@"lianjie" LastName:@"Li" bookName:@"book1"];
Student *stu2=[Student initWithFirstName:@"dehua" LastName:@"Liu" bookName:@"book2"];
Student *stu3=[Student initWithFirstName:@"xingle" LastName:@"Li" bookName:@"book2"];
Student *stu4=[Student initWithFirstName:@"long" LastName:@"Cheng" bookName:@"book1"];
NSArray *array=[NSArray arrayWithObjects:stu1,stu2,stu3,stu4, nil];
//先按照书名进行排序 NSSortDescriptor *bookNameDesc=[NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES];
NSSortDescriptor *lastnameDesc=[NSSortDescriptor sortDescriptorWithKey:@"lastname" ascending:YES];
NSSortDescriptor *firstnameDesc=[NSSortDescriptor sortDescriptorWithKey:@"firstname" ascending:YES]; NSArray *decarray=[NSArray arrayWithObjects:bookNameDesc,lastnameDesc,firstnameDesc, nil]; NSArray *array2=[array sortedArrayUsingDescriptors:decarray];
NSLog(@"排序后:%@",array2); } int main(int argc, const char * argv[])
{ @autoreleasepool {
// arrayTest();
// arrayMessage();
//arrayLoop();
// arraySort();
// studentSort();
//先根据book名,后根据性 ,再根据名 进行排序
studentSort2();
}
return 0;
}

Student函数:

头文件:

//  Student.h
// NSArray
//
// Created by WildCat on 13-7-25.
// Copyright (c) 2013年 wildcat. All rights reserved.
// #import <Foundation/Foundation.h>
@class Book;
@interface Student : NSObject{
NSString *_firstname;
NSString *_lastname; }
@property (nonatomic,retain) NSString * firstname;
@property (nonatomic,retain) NSString * lastname;
//每个学生拥有一本书
@property (nonatomic,retain)Book *book;
+(id)initWithFirstName:(NSString *)firstname LastName:(NSString *)lastname;
+(id)initWithFirstName:(NSString *)firstname LastName:(NSString *)lastname bookName:(NSString *)bookName;
+(id)student;
-(void) addMessage:(NSString *)str;
-(NSComparisonResult)compareStudent:(Student *)stu;
@end

.m文件:

//
// Student.m
// NSArray
//
// Created by WildCat on 13-7-25.
// Copyright (c) 2013年 wildcat. All rights reserved.
// #import "Student.h"
#import "Book.h"
@implementation Student
@synthesize firstname=_firstname;
@synthesize lastname=_lastname;
+(id)initWithFirstName:(NSString *)firstname LastName:(NSString *)lastname{
Student *stu=[[[Student alloc] init] autorelease]; stu.firstname=firstname;
stu.lastname=lastname;
return stu;
}
+(id)initWithFirstName:(NSString *)firstname LastName:(NSString *)lastname bookName:(NSString *)bookName{
Student *stu=[Student initWithFirstName:firstname LastName:lastname];
Book *book=[Book initBookWithName:bookName];
stu.book=book;
return stu;
} +(id)student{ return [[[Student alloc] init] autorelease]; }
-(void) addMessage:(NSString *)str{ NSLog(@"%@->addMessage->%@",self,str);
}
-(void)dealloc{
[_lastname release];
[_firstname release]; NSLog(@"%@ 被销毁",self);
[super dealloc];
}
//定义一个排序方法
-(NSComparisonResult)compareStudent:(Student *)stu{ //先按照性进行排序
NSComparisonResult result=[self.lastname compare:stu.lastname];
//再按照名进行排序
if (result==NSOrderedSame) {
result=[self.firstname compare:stu.firstname];
}
return result;
}
//重定义description 方法
-(NSString *)description{ return [NSString stringWithFormat:@"%@ %@->%@",self.lastname,self.firstname,self.book.name]; } @end

Book类:

.h文件:

//  Book.h
// NSArray
//
// Created by WildCat on 13-7-25.
// Copyright (c) 2013年 wildcat. All rights reserved.
// #import <Foundation/Foundation.h> @interface Book : NSObject
@property (nonatomic ,retain) NSString *name;
+(id)initBookWithName:(NSString *)name; @end

.m文件:

#import "Book.h"

@implementation Book

+(id)initBookWithName:(NSString *)name{
Book *book=[[[Book alloc] init] autorelease];
book.name=name;
return book; }
-(void)dealloc{
[_name release];
[super dealloc]; }
@end

Object-c学习之路八(NSArray(数组)遍历和排序)的更多相关文章

  1. Android开发学习之路-RecyclerView滑动删除和拖动排序

    Android开发学习之路-RecyclerView使用初探 Android开发学习之路-RecyclerView的Item自定义动画及DefaultItemAnimator源码分析 Android开 ...

  2. Java学习之路(八):Set

    Set集合概述以及特点: set 是一个不包含重复元素的collection set只是一个接口,一般使用它的子类HashSet,LinkedHashSet,TreeSet HashSet 此类是Se ...

  3. zigbee学习之路(八):定时器1(中断)

    一.前言 通过上次的实验,我们已经学会了定时器3的中断方式,这次,我们来看看定时器1通过中断怎么控制. 二.原理分析 定时器1的初始化跟前面提到的一样,也是要配置寄存器T1CTL,还要进行开中断的操作 ...

  4. 嵌入式Linux驱动学习之路(八)创建最小的根文件系统

    busybox 在配置busybox,在是否选择要静态链接库时,在静态下,busybox中的工具不需要动态链接库,能够直接运行.而用户自己编写的程序如果需要动态链接库,还是依然需要有. 如果是动态链接 ...

  5. IOS7学习之路八(iOS 禁止屏幕旋转的方法)

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { retu ...

  6. IOS学习之路八(GCD与多线程)

    GCD,全称Grand Central Dispath,是苹果开发的一种支持并行操作的机制.它的主要部件是一个FIFO队列和一个线程池,前者用来添加任务,后者用来执行任务. GCD中的FIFO队列称为 ...

  7. python学习之路 八 :面向对象编程基础

    本节重点 了解面向对象.面向过程的区别 掌握什么是类,什么是对象 掌握如何定义及使用类和对象 了解类与对象间的关系 掌握类属性和实例属性 掌握绑定方法 一.编程范式 ​    ​编程即写程序or写代码 ...

  8. Go数组遍历与排序

    遍历数组 Go遍历数组有两种方式 1.按照数组下标进行遍历 2.用range遍历 package main import ( "fmt" ) func main() { // 声明 ...

  9. Go语言学习笔记八: 数组

    Go语言学习笔记八: 数组 数组地球人都知道.所以只说说Go语言的特殊(奇葩)写法. 我一直在想一个人参与了两种语言的设计,但是最后两种语言的语法差异这么大.这是自己否定自己么,为什么不与之前统一一下 ...

随机推荐

  1. hdu 4915 Parenthese sequence(模拟)2014多培训学校5现场

    Parenthese sequence                                                                     Time Limit: ...

  2. linux在构建SVNserver

    最近搞了一个云计算server,一些尝试部署server相关的东西.作为用显影剂server.首先要考虑的是建立SVNserver.关于构建过程记录.方便以后. 一.安装svn软件.有些云server ...

  3. 利用EntityFramework获得双色球数据库

    原文 利用EntityFramework获得双色球数据库 双色球想必大家都很熟悉了,尽管屡买屡不中,但还是会买.以前就想过利用双色球的走势图得到双色球的数据库,至于得到数据库干什么倒没想过,不过对以往 ...

  4. linux c ping 实现

    用c语言实现的linux中的ping命令 #include <stdio.h> #include <signal.h> #include <arpa/inet.h> ...

  5. js 里面 写 C# 代码 遇到的问题

    js  代码块 必须放置在 body 里面

  6. MS SQL Server递归查询

    原文:MS SQL Server递归查询 刚才在论坛上看到网友一个要求.参考如下,Insus.NET分析一下,可以使用MS SQL Server的递归查询,得到结果.准备一张表: 根据网友提供的数据, ...

  7. HTML静态分页(形如:首页,上一页,下一页,尾页)

    在HTML中有时候我们会用到静态分页,一次拿回一定量的数据结果条目,我们会以形如:第2页,共12页  首页 上一页 下一页 尾页 的方式进行静态分页,以下是该种静态分页的代码,供兄弟姐妹们参考. &l ...

  8. MonkeyRunner源码分析之-谁动了我的截图?

    本文章的目的是通过分析monkeyrunner是如何实现截屏来作为一个例子尝试投石问路为下一篇文章做准备,往下一篇文章本人有意分析下monkeyrunner究竟是如何和目标测试机器通信的,所以最好的办 ...

  9. ASP.NET 5应用是如何运行的(3)

    ASP.NET 5应用是如何运行的(3) 设置自定义的入口程序体现应用本身与应用托管之间的分离,它使我们可以创建独立于托管环境的应用,并根据需要寄宿于任何一个我们希望的宿主程序下,对于Web应用来说这 ...

  10. jmeter 控制器

    Critical Section Controller :