Chinaunix首页 | 论坛 | 博客
  • 博客访问: 53174
  • 博文数量: 34
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 297
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-09 10:52
文章分类

全部博文(34)

文章存档

2015年(23)

2014年(11)

我的朋友

分类: iOS平台

2015-04-14 16:10:43

一.数组

NSArray

用来存储有序的列表,他是不可变的,一旦装入10个元素就不能再添加删除元素    

不能存储基本类型,只允许装OC对象,结构体,枚举,nil都不行,nil代表数组的结束



数组遍历的代码:

点击(此处)折叠或打开

  1. //
  2. // Student.m
  3. // 遍历
  4. //
  5. // Created by 金海洋 on 15-4-3.
  6. // Copyright (c) 2015年 金海洋. All rights reserved.
  7. //

  8. #import "Student.h"

  9. @implementation Student

  10. - (void)dealloc
  11. {
  12.     
  13.     NSLog(@"student销毁了");
  14.     [super dealloc];
  15. }

  16. +(id)stu{
  17.     //快速创建对象,不用管理内存
  18.     Student *st = [[[Student alloc]init]autorelease];
  19.     return st;
  20.     
  21. }

  22. -(void)fun{
  23.     
  24.     NSLog(@"fun");
  25. }

  26. -(void)funa:(id)a{
  27.     
  28.     NSLog(@"fun2--%@",a);
  29. }

  30. @end


点击(此处)折叠或打开

  1. //
  2. // main.m
  3. // 遍历
  4. //
  5. // Created by 金海洋 on 15-4-3.
  6. // Copyright (c) 2015年 金海洋. All rights reserved.
  7. //

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

  10. //内存管理
  11. void memory(){
  12.     
  13.     Student *s1 = [[Student alloc] init];
  14.     Student *s2 = [[Student alloc] init];
  15.     Student *s3 = [[Student alloc] init];
  16.     
  17.     NSLog(@"s1--1--%zd",[s1 retainCount]);
  18.     //当把对象塞进数组时候,这个对象计数器就加1
  19.     NSArray *n1 = [[NSArray alloc]initWithObjects:s1,s2,s3, nil];
  20.     
  21.     NSLog(@"s1--2--%zd",[s1 retainCount]);
  22.     
  23.     NSLog(@"cont--%zd",n1.count);
  24.     //当数组元素销毁的时候,他都会对里面的对象release
  25.     [n1 release];
  26.     [s1 release];
  27.     [s2 release];
  28.     [s3 release];
  29.     
  30. }

  31. //给数组里面的元素发送消息
  32. void fun1(){
  33.     
  34.     Student *s1 = [Student stu];
  35.     Student *s2 = [Student stu];
  36.     Student *s3 = [Student stu];
  37.     NSArray *n1 = [NSArray arrayWithObjects:s1,s2,s3, nil];
  38.     NSString *a =@"123";
  39.     
  40.     [n1 makeObjectsPerformSelector:@selector(fun)];
  41.     [n1 makeObjectsPerformSelector:@selector(funa:) withObject:a];
  42.     
  43. }

  44. //遍历
  45. void fun2(){

  46.     NSArray *n1 = [NSArray arrayWithObjects:@"12",@"23",@"2", nil];
  47.     int cont = n1.count;
  48.     for(int i =0 ; i<cont;i++){
  49.         //id == *void
  50.         id obj = [n1 objectAtIndex:i ];
  51.         NSLog(@"%@",obj);
  52.     
  53.     }

  54. }

  55. //遍历2
  56. void fun3(){
  57.     
  58.     NSArray *n1 = [NSArray arrayWithObjects:@"12",@"23",@"2", nil];
  59.     //快速遍历
  60.     for(id obj in n1){
  61.         NSLog(@"%@",obj);
  62.     
  63.     }
  64.     

  65. }

  66. //遍历3
  67. void fun4(){
  68. //用block来遍历
  69.     NSArray *n1 = [NSArray arrayWithObjects:@"12",@"23",@"2", nil];
  70.     //创建的时候双击就会自动生成
  71.     [n1 enumerateObjectsUsingBlock:
  72.     ^(id obj, NSUInteger idx, BOOL *stop) {
  73.         
  74.         //如果索引为1,就停止遍历
  75.         if ( idx == 1) {
  76.             //stop为yes的时候,会停止遍历
  77.             *stop = YES;
  78.         }
  79.         
  80.         NSLog(@"%@",obj);
  81.         

  82.     }];
  83.     
  84.     
  85.     
  86. }

  87. //遍历4
  88. void fun5(){

  89. //迭代器
  90.     NSArray *n1 = [NSArray arrayWithObjects:@"12",@"23",@"2", nil];
  91.     //获取数组的迭代器
  92.     NSEnumerator *e = [n1 objectEnumerator];
  93.     
  94.     //返回迭代器所有元素
  95.     //allObjects 只取出没有被遍历过的对象
  96.     NSArray *ns = [e allObjects];
  97.     
  98.     NSLog(@"%@",ns);
  99.     
  100.     e = [n1 objectEnumerator];
  101.     //获取下一个需要遍历的元素
  102.     //[e nextObject];
  103.     id a = nil;
  104.     while (a = [e nextObject]) {
  105.         
  106.         
  107.         NSLog(@"--%@",a);
  108.     }
  109.     
  110.     //获取反序的迭代器
  111.     e = [n1 reverseObjectEnumerator];
  112.     
  113.     id a1 = nil;
  114.     while (a1 = [e nextObject]) {
  115.         
  116.         
  117.         NSLog(@"=%@",a1);
  118.     }
  119.     
  120.     
  121. }

  122. int main(int argc, const char * argv[]) {
  123.     @autoreleasepool {
  124.         //创建一个空数组
  125.         NSArray *a1 = [NSArray array];
  126.         //创建多个元素的数组
  127.         NSArray *a2 = [NSArray arrayWithObjects:@"123",@"sdf",@"abc",nil];
  128.         
  129.         //获取元素个数
  130.         unsigned int cout = [a2 count];
  131.         unsigned int cout1 = a2.count;
  132.         
  133.         NSLog(@"个数为%zd",cout);
  134.         
  135.         //查看包含字符串
  136.         if([a2 containsObject:@"abc"]){
  137.             NSLog(@"包含了");
  138.         }
  139.         
  140.         //返回最后一个元素
  141.         NSString *s = [a2 lastObject];
  142.         NSLog(@"最后一个元素-%@",s);
  143.         
  144.         //根据下标获取元素
  145.         s = [a2 objectAtIndex:2];
  146.         NSLog(@"元素为-%@",s);
  147.         
  148.         //查找索引
  149.         int index = [a2 indexOfObject:@"123"];
  150.         NSLog(@"索引为-%d",index);
  151.         
  152.         //memory();
  153.         //fun1();
  154.         //fun2();
  155.         //fun3();
  156.         //fun4();
  157.         fun5();
  158.         
  159.     }
  160.     return 0;
  161. }

下面是数组的排序:


点击(此处)折叠或打开

  1. //
  2. // Student.h
  3. // 排序
  4. //
  5. // Created by 金海洋 on 15-4-3.
  6. // Copyright (c) 2015年 金海洋. All rights reserved.
  7. //

  8. #import <Foundation/Foundation.h>
  9. @class Book;

  10. @interface Student : NSObject


  11. @property (nonatomic,retain)NSString *name;
  12. @property (nonatomic,retain)NSString *xing;
  13. @property (nonatomic,retain)Book *book;

  14. +(id)stu1:(NSString*)s1 stu2:(NSString*)s2;
  15. +(id)stu1:(NSString*)s1 stu2:(NSString*)s2 bo:(NSString*)book;

  16. -(NSComparisonResult)comp:(Student*)s;

  17. @end


点击(此处)折叠或打开

  1. //
  2. // Student.m
  3. // 排序
  4. //
  5. // Created by 金海洋 on 15-4-3.
  6. // Copyright (c) 2015年 金海洋. All rights reserved.
  7. //

  8. #import "Student.h"
  9. #import "Book.h"

  10. @implementation Student

  11. +(id)stu1:(NSString*)s1 stu2:(NSString*)s2{

  12.     Student *s = [[Student alloc]init];
  13.     s.name = s1;
  14.     s.xing = s2;
  15.     
  16.     return s;
  17.     
  18. }

  19. +(id)stu1:(NSString*)s1 stu2:(NSString*)s2 bo:(NSString *)book{
  20.     
  21.     Student *s = [[Student alloc]init];
  22.     s.name = s1;
  23.     s.xing = s2;
  24.     s.book = [Book bookWithName:book];
  25.     
  26.     return s;
  27.     
  28. }

  29. -(NSComparisonResult)comp:(Student*)s{

  30.     
  31.     //先按照姓名排序
  32.     NSComparisonResult re = [self.xing compare:s.xing];
  33.     //名字一样的话,按照名字排序
  34.     if (re == NSOrderedSame){
  35.         
  36.         re = [self.name compare:s.name];
  37.         
  38.     }
  39.     
  40.     return re ;
  41. }

  42. - (NSString *)description
  43. {
  44.     return [NSString stringWithFormat:@"%@--%@--%@", self.xing,self.name,self.book.name];
  45. }

  46. -(void)dealloc{
  47.     
  48.     [_name release];
  49.     [_xing release];
  50.     [_book release];
  51.     [super release];

  52. }

  53. @end


点击(此处)折叠或打开

  1. //
  2. // Book.h
  3. // 排序
  4. //
  5. // Created by 金海洋 on 15-4-14.
  6. // Copyright (c) 2015年 金海洋. All rights reserved.
  7. //

  8. #import <Foundation/Foundation.h>

  9. @interface Book : NSObject


  10. @property (nonatomic,retain)NSString *name;

  11. +(id)bookWithName:(NSString*)name;

  12. @end


点击(此处)折叠或打开

  1. //
  2. // Book.m
  3. // 排序
  4. //
  5. // Created by 金海洋 on 15-4-14.
  6. // Copyright (c) 2015年 金海洋. All rights reserved.
  7. //

  8. #import "Book.h"

  9. @implementation Book

  10. +(id)bookWithName:(NSString*)name{

  11.     Book *b = [[[Book alloc]init]autorelease];
  12.     
  13.     b.name = name;
  14.     
  15.     return b;

  16. }

  17. - (void)dealloc
  18. {
  19.     [_name release];
  20.     [super release];

  21. }

  22. @end


点击(此处)折叠或打开

  1. //
  2. // main.m
  3. // 排序
  4. //
  5. // Created by 金海洋 on 15-4-3.
  6. // Copyright (c) 2015年 金海洋. All rights reserved.
  7. //

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

  10. //派生出新的数组
  11. void fun1(){

  12.     NSArray *n1 = [NSArray arrayWithObjects:@"123",@"23",@"222",nil ];
  13.     
  14.     NSArray *n2 = [n1 arrayByAddingObject:@"1231"];
  15.     NSArray *n3 =[n1 arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:@"123",@"23",@"222",nil ]];
  16.     NSRange r = NSMakeRange(1, 2);
  17.     //截取后派生
  18.     NSArray *n4 =[n3 subarrayWithRange:r];
  19.     
  20.     NSLog(@"n1--%zd",n1.count);
  21.     NSLog(@"n2--%zd",n2.count);
  22.     NSLog(@"n3--%zd",n3.count);
  23.     NSLog(@"n4--%zd",n4.count);

  24. }

  25. //拼接
  26. void fun2(){
  27.     
  28.     NSArray *n1 = [NSArray arrayWithObjects:@"123",@"23",@"222",nil ];
  29.     //用 -- 拼接元素
  30.     NSString *s = [n1 componentsJoinedByString:@"--"];
  31.     NSLog(@"%@",s );
  32.     //123--23--222
  33.     
  34.     NSString *s1 = @"/Users/jinhaiyang/Desktop/33.txt";
  35.     //将数组内容以xml形式写进文件
  36.     [n1 writeToFile:s1 atomically:YES];
  37.     
  38.     NSString *s2 = @"/Users/jinhaiyang/Desktop/44.txt";
  39.     //从文件读取内容,文件有严格要求
  40.     NSArray *n2 = [NSArray arrayWithContentsOfFile:s2];
  41.     NSLog(@"%@",n2);
  42.     
  43. }

  44. //排序
  45. void fun3(){

  46.     NSArray *n1 = [NSArray arrayWithObjects:@"123",@"23",@"222",nil ];
  47.     //返回一个排好序的数组,原来数组不变
  48.     //compare 告诉它这个数组的比较方法 从小到大
  49.     NSArray *n2 = [n1 sortedArrayUsingSelector:@selector(compare:)];
  50.     NSLog(@"%@",n2);

  51. }

  52. //排序2
  53. //自己写比较方法
  54. void fun4(){

  55.     Student *s = [Student stu1:@"li" stu2:@"sun"];
  56.     Student *s1 = [Student stu1:@"ang" stu2:@"li"];
  57.     Student *s2 = [Student stu1:@"gang" stu2:@"li"];
  58.     
  59.     NSArray *n1 = [NSArray arrayWithObjects:s,s1,s2,nil ];
  60.     NSArray *n2 = [n1 sortedArrayUsingSelector:@selector(comp:)];
  61.     
  62.     NSLog(@"%@",n2);
  63. }

  64. //排序3
  65. void fun5(){
  66. //block
  67.     Student *s = [Student stu1:@"li" stu2:@"sun"];
  68.     Student *s1 = [Student stu1:@"ang" stu2:@"li"];
  69.     Student *s2 = [Student stu1:@"gang" stu2:@"li"];
  70.     
  71.     NSArray *n1 = [NSArray arrayWithObjects:s,s1,s2,nil ];

  72.     NSArray *n2 = [n1 sortedArrayUsingComparator:
  73.     ^NSComparisonResult(Student *s1, Student *s2) {
  74.         
  75.         //先按照姓名排序
  76.         NSComparisonResult re = [s1.xing compare:s2.xing];
  77.         //名字一样的话,按照名字排序
  78.         if (re == NSOrderedSame){
  79.             
  80.             re = [s1.name compare:s2.name];
  81.             
  82.         }
  83.         
  84.         return re ;
  85.         
  86.     }];
  87.     
  88.     NSLog(@"%@",n2);
  89.     
  90. }

  91. //排序4
  92. void fun6(){

  93.     Student *s = [Student stu1:@"li" stu2:@"sun" bo:@"abc"];
  94.     Student *s1 = [Student stu1:@"ang" stu2:@"li" bo:@"sf"];
  95.     Student *s2 = [Student stu1:@"gang" stu2:@"li" bo:@"abc"];
  96.     
  97.     NSArray *n1 = [NSArray arrayWithObjects:s,s1,s2,nil ];
  98.     
  99.     //先创造一个排序描述器
  100.     //ascending 默认是升序
  101.     //sortDescriptorWithKey: 写上去的是 property后面声明的名字
  102.     NSSortDescriptor *a = [NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES];
  103.     NSSortDescriptor *b = [NSSortDescriptor sortDescriptorWithKey:@"xing" ascending:YES];
  104.     NSSortDescriptor *c = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
  105.     
  106.     //按照要先进行排序的属性,放进数组里
  107.     NSArray *n2 = [NSArray arrayWithObjects:a,b,c ,nil ];
  108.     
  109.     NSArray *n3 = [n1 sortedArrayUsingDescriptors:n2];
  110.     NSLog(@"%@",n3);
  111.     
  112.     
  113. }

  114. int main(int argc, const char * argv[]) {
  115.     @autoreleasepool {
  116.         
  117.         //fun1();
  118.         //fun2();
  119.         //fun3();
  120.         //fun4();
  121.         //fun5();
  122.         fun6();
  123.         
  124.     }
  125.     return 0;
  126. }


二.动态数组

可变数组

不可以添加空和基本类型


动态数组创建的代码:


点击(此处)折叠或打开

  1. //
  2. // main.m
  3. // 可变数组
  4. //
  5. // Created by 金海洋 on 15-4-8.
  6. // Copyright (c) 2015年 金海洋. All rights reserved.
  7. //

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


  10. void mem(){
  11. //内存管理
  12.     //NSMutableArray *n = [NSMutableArray array];
  13.     NSMutableArray *n = [[NSMutableArray alloc]init];
  14.     
  15.     Student *s1 = [Student StudentWithAge:34];
  16.     Student *s2 = [Student StudentWithAge:14];
  17.     Student *s3 = [Student StudentWithAge:100];
  18.     
  19.     //会给被添加的元素进行一次retain操作
  20.     [n addObject:s1];
  21.     [n addObject:s2];
  22.     [n addObject:s3];
  23.     NSLog(@"%zd",[s1 retainCount]);
  24.     //会给被添加的元素进行一次release 操作
  25.     [n removeObject:s1];
  26.     NSLog(@"%zd",[s1 retainCount]);
  27.     [n addObject:s2];
  28.     
  29.     //数组释放的时候都会对元素做一次release操作
  30.     [n release];
  31.     
  32. }

  33. void fun(){
  34. //删除

  35.     NSArray *ns1= [NSMutableArray arrayWithObjects:@"1", @"2",@"112", @"212",@"412",nil];
  36.     //删除index位置的元素
  37.     //[ns1 removeObjectAtIndex:1];
  38.     //删除NSRange所指范围的元素
  39.     NSRange r = NSMakeRange(0, 2);
  40.     [ns1 removeObjectsInRange:r];

  41.     NSLog(@"%@",ns1);
  42.     
  43.     
  44.     
  45. }

  46. void fun1(){
  47. //替换
  48.     NSMutableArray *ns1 = [NSMutableArray arrayWithObjects:@"1", @"2",@"112",nil];
  49.     
  50.     [ns1 replaceObjectAtIndex:1 withObject:@"4444"];
  51.     
  52.     NSLog(@"%@",ns1);
  53.     
  54.     
  55. }

  56. void fun2(){
  57. //排序
  58.     NSMutableArray *ns1 = [NSMutableArray arrayWithObjects:@"11", @"2",@"112",nil];
  59.     //返回排序号的数组
  60.     NSMutableArray *ns2 = [ns1 sortedArrayUsingSelector:@selector(compare:)];
  61.     //直接在这个数组里排序
  62.     [ns1 sortUsingSelector:@selector(compare:)];
  63.     
  64.     NSLog(@"%@",ns1);
  65.     NSLog(@"---%@",ns2);

  66. }

  67. int main(int argc, const char * argv[]) {
  68.     @autoreleasepool {
  69.         
  70.         NSMutableArray *ns1 = [NSMutableArray arrayWithObjects:@"1", @"2",@"112",nil];
  71.         
  72.         //动态添加元素
  73.         [ns1 addObject:@"123"];
  74.         NSLog(@"%@",ns1);
  75.         //删除对象@"1"
  76.         [ns1 removeObject:@"1"];
  77.         NSLog(@"%@",ns1);
  78.         //删除最后一个对象
  79.         [ns1 removeLastObject];
  80.         //删除所有对象
  81.         [ns1 removeAllObjects];
  82.         NSLog(@"%@",ns1);
  83.         
  84.         //mem();
  85.         fun();
  86.         //fun1();
  87.         //fun2();
  88.         
  89.     }
  90.     return 0;
  91. }




阅读(750) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~