ios配列の基本的な使い方とソート

6542 ワード

1.配列の作成
[objc]view plaincopy CODEでチップを見て私のチップに派生//空の配列NSArray*array=[NSArray array];1要素の配列array=[NSArray arrayWithObject:@"123";//複数の要素を持つ配列array=[NSArray arrayWithObjects:@"a",@"b",@"c",nil nil];NSArray *array3 = [array arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:@"4", @"5", nil nil]];
NSArray *array4 = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil nil]; NSRange range = NSMakeRange(1, 2); NSArray *array5 = [array4 subarrayWithRange:range];
2.配列の基本的な方法
[objc]view plaincopy CODEでチップを見て私のチップint count=[array count];//個数//ある要素if([array containsObject:@"a"){NSLog(@"文字列aを含む");NSString *last = [array lastObject];最後の要素NSString*str=[array objectAtIndex:1];インデックスに基づいて配列内の要素int index=[array indexOfObject:@"c"];指定した要素のインデックスを取得//配列内のすべてのオブジェクトにtestメソッドを呼び出す.123はパラメータNSArray*array=[NSArray arrayWithObjects:stu 1,stu 2,stu 3,nil nil];[array makeObjectsPerformSelector:@selector(test2:) withObject:@"123"]; NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil nil];//1-2-3-4//セパレータを使用-すべての配列要素NSString*str=[array componentsJoinedByString:@"-";//1つの配列をファイルに書き込む(xmlファイルが生成される)NSString*path=@"/Users/apple/desktop/array.xml;[array writeToFile:path atomically:YES];path=@"/Users/apple/desktop/array.txt";//ファイルから配列内容を読み取る(ファイルに厳格なフォーマット要件がある)NSArray*array 2=[NSArray array array array 2=[NSArray array WithContentsOfFile:path];3.配列配列を巡る
[objc]view plaincopy CODEでコードシートを見て私のコードシートに派生
pragma mark遍歴配列1
void arrayFor1() { NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil nil]; int count = array.count; for (int i = 0; i id obj = [array objectAtIndex:i]; } }
pragma mark遍歴配列2快速遍歴
void arrayFor2() { Student *stu1 = [Student student]; NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil nil]; int i =0; for (id obj in array) { NSLog(@"%i-%@", i, obj); i++; } }
pragma mark遍歴配列3
void arrayFor 3(){Student*stu 1=[Student student];NSArray*array=[NSArrayarrayWithObjects:stu 1,@"1",@"2",@"3",nil nil];[arrayenumerateObjectsUsingBlock:^(id obj,NSUInteger idx,NSUInteger idx,BOOLBOOL*stop){NSLog(@"%i-%@",idx,obj j j j j);////////インデックスが1であれば,遍遍遍遍遍遍遍遍遍停止(////////////////@歴if(idx==1)//ポインタで外BOOL変数の値を修正する*stop=YES;}];
pragma mark遍歴配列4
void arrayFor 4(){Student*stu 1=[Student student];NSArray*array=[NSArrayarrayWithObjects:stu 1,@"1",@"2",@"3",nil nil];//////配列の反復器を取得////////////////@配列の反復器*NSEnumeerator*enumerator=[arrayobjectEnumerator];//////////////////////////////////////////////////反復器(末尾から要素を遍歴する)NSEnumerator*enumerator=[arrayreverseObjeObjectEnbjeObjectEnumerator];////////////ctEnumerator;//AllObjectsは、遍歴されていないオブジェクトNSArray*array 2=[enumerator allObjects];NSLog(@"array2:%@", array2);//次の遍歴が必要な要素id obj=nilを取得します.while (obj = [enumerator nextObject]) { NSLog(@"obj=%@", obj); } }
4.配列ソート[objc]view plaincopyコードシートから派生したコードシートをCODEで表示
pragma mark配列ソート1
void arraySort1() { NSArray *array = [NSArray arrayWithObjects:@"2", @"3", @"1", @"4", nil 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 nil];//ソートの比較方法NSArray*array 2=[array sortedArrayUsingSelector:@selector(compareStudio:)];NSLog(@"array2:%@", array2); }
  • (NSComparisonResult)comparareStudent:(Student*)stu{//NSComparisonResult result=[self.lastname comparare:stu.lastname]//同じ姓があればif(result==NSOrderedSame){result=[self.firstname comparare:stu.firstname];return result; }

  • 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 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 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 nil];  
      
    NSArray *array2 = [array sortedArrayUsingDescriptors:descs];  
      
    NSLog(@"array2:%@", array2);  
    

    }