foundation-数组的遍历
来源:互联网 发布:哈工大 知乎 编辑:程序博客网 时间:2024/04/29 10:33
//// main.m// foundation-NSArray//// Created by apple on 15/6/28.// Copyright (c) 2015年 itcast. All rights reserved.//#import <Foundation/Foundation.h>#import "Student.h"#pragma mark 数组的创建void array(){ //NSArray *arr = [NSArray array]; NSArray *arr1 = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d", nil]; unsigned long count = [arr1 count]; NSLog(@"%ld",count);}#pragma mark 数组常用方法void arrayUse(){ NSObject *obj = [[NSObject alloc]init]; NSArray *arr1 = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d",obj, nil]; if ([arr1 containsObject:@"a"]) { NSLog(@"包含了a"); } NSString *last = [arr1 lastObject]; NSLog(@"last = %@",last); NSString *str = [arr1 objectAtIndex:1]; NSLog(@"objectAtIndex = %@",str); unsigned long index = [arr1 indexOfObject:@"c"]; NSLog(@"indexOfObject = %ld",index);}#pragma mark 数组的内存管理void memory(){ Student *stu1 = [[Student alloc]init]; Student *stu2 = [[Student alloc]init]; Student *stu3 = [[Student alloc]init];// NSLog(@"stu1.count = %ld",[stu1 retainCount]); NSArray *arr = [NSArray arrayWithObjects:stu1,stu2,stu3, nil];// NSLog(@"stu1.count = %ld",[stu1 retainCount]); NSLog(@"count = %ld",arr.count);}#pragma mark 给数组中的元素发送消息void arrayMessage(){ Student *stu1 = [Student student]; Student *stu2 = [Student student]; Student *stu3 = [Student student]; NSArray *arr = [NSArray arrayWithObjects:stu1,stu2,stu3,nil]; //让数组中的所有元素都调用test方法 //[arr makeObjectsPerformSelector:@selector(test)]; [arr makeObjectsPerformSelector:@selector(test2:) withObject:@"peter"];}#pragma mark 遍历数组void arrayFor(){ NSArray *arr = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d", nil]; unsigned long count = arr.count; for (long i=0; i<count; i++) { //id == void *(指向任何类型的指针) id obj = [arr objectAtIndex:i]; NSLog(@"%ld-%@",i,obj); }}#pragma mark 遍历数组2void arrayFor2(){ NSArray *arr = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d", nil]; unsigned long i = 0; for(id obj in arr){ NSLog(@"%ld-%@",i,obj); i++; }}#pragma mark 遍历数组3void arrayFor3(){ NSArray *arr = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d", nil]; [arr enumerateObjectsUsingBlock: //*stop在里面要改变stop的值,所以要传一个指针 ^(id obj, NSUInteger idx, BOOL *stop) { NSLog(@"%ld-%@",idx,obj); //如果索引为1就停止遍历 if(idx == 1){ //利用指针修改外面BOOL变量的值 *stop = YES; } }];}#pragma mark 遍历数组3void arrayFor4(){ NSArray *arr = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d", nil]; //获取数组的迭代器 NSEnumerator *enumerator = [arr objectEnumerator]; //反序迭代器 enumerator = [arr reverseObjectEnumerator]; //取出所有对象(没有遍历过的) NSArray *arr2 = [enumerator allObjects]; NSLog(@"obj=%@",arr2); //获取下一个元素 NSObject *obj = nil; while (obj = [enumerator nextObject]) { NSLog(@"obj=%@",obj); }}int main(int argc, const char * argv[]) { @autoreleasepool { arrayFor4(); } return 0;}
0 0
- foundation-数组的遍历
- foundation-数组的遍历2
- Foundation框架–NSArray 的遍历方法
- Cocoa Foundation 框架:NSArray数组的使用
- 关于数组的遍历
- 数组的遍历
- smarty_foreach的数组遍历
- PERL数组的遍历
- java 数组的遍历
- PHP数组的遍历
- 数组的遍历
- 数组遍历的练习
- PHP数组的遍历
- 数组的遍历
- 数组的遍历
- 二维数组的遍历
- 二维数组的遍历
- 整数数组的遍历
- CSS改变字体下划线颜色
- Onenote的小脾气和大财富
- struts2框架学习之国际化资源
- eclipse调试
- 黑马程序员——Java中的异常和包
- foundation-数组的遍历
- Codeforces #310 Div 1 简要题解
- ConcurrentModificationException异常解决办法
- 新浪微博爬虫(模拟登录+数据解析)
- ODBC与JDBC比较
- [USACO Nov08]玩具toys解题报告
- 有人照顾有人不照顾
- Java向Oracle数据库插入时间
- Ubuntu的回收站