ObjC第六節 2 OC常用類

2021-07-04 17:41:03 字數 3681 閱讀 4815

oc常用類:nsarray、nsdictionary、nsdata

1、nsarray

2、nsdictionary

3、nsdata

nsarray

nsarray

#import #import "classa.h"

#import "classb.h"

int main(int argc, const char * argv)

陣列排序

nsarray(普通的陣列排序)

#import int main(int argc, const char * argv)

for (id object in arr)

nsarray * arr1 = [nsarray arraywithobjects:str1, str2, @"11", nil];

nsarray * arr2 = [arr1 sortedarrayusingselector:@selector(compare:)];

nslog(@"%@", arr2);

return 0;

}

nsarray 陣列排序之自寫compareperson

#import #import "classa.h"

int main(int argc, const char * argv)

;nscomperator comparestringsblock = ^(id stringa, id stringb) ;

nsarray *array = [nsarray arraywithobjects:a1, a2, a3, a4, nil];

nsarray *sortedarray = [array sortedarrayusingcomparator:^nscomparisonresult(id obj1, id obj2)];

nslog(@"%@",sortedarray);

這裡的**可以參照上面compare:預設的排序方法,也可以把自定義的方法寫在這裡,給物件排序

[a4 release];

return 0;

}

classa.h

#import @inte***ce classa : nsobject

@property (nonatomic) int x, y;

- (void) print;

#pragma mark --自寫compare

@property (nonatomic)int age;

@property (nonatomic, retain) nsstring * name;

+(id)personwithage:(int) age withname:(nsstring *)name;

-(id)initwithage:(int) age withname:(nsstring *)name;

-(nscomparisonresult)compareperson:(classa *)person;

-(nsstring *) description;

@end

classa.m

#import "classa.h"

@implementation classa

@synthesize x, y;

- (void) print

#pragma mark --自寫compareperson

@synthesize age, name;

+(id)personwithage:(int) age withname:(nsstring *)name

-(id)initwithage:(int)_age withname:(nsstring *)_name;

return self;

}-(nscomparisonresult)compareperson:(classa *)person

return result;

}-(nsstring *) description

@end

nsarray 陣列排序之高階排序(組合類的排序,排序描述器sortedarrayusingdescriptors:)

#import #import "person.h"

int main(int argc, const char * argv)

car

#import @inte***ce car : nsobject

@property (nonatomic, retain) nsstring * color;

+(car *)carwithcolor:(nsstring *)_color;

@end

#import "car.h"

@implementation car

@synthesize color;

+(car *)carwithcolor:(nsstring *)_color

@end

person

#import #import "car.h"

@inte***ce person : nsobject

@property (nonatomic)int age;

@property (nonatomic, retain) nsstring * name;

@property (nonatomic, retain) car * car;

+(id)personwithage:(int) age withname:(nsstring *)name withcar:(car *)car;

-(nsstring *) description;

@end

#import "person.h"

@implementation person

@synthesize age, name, car;

+(id)personwithage:(int) age withname:(nsstring *)name withcar:(car *)car

-(nsstring *) description

@end

快速遍歷

nsarray

#import int main ()

for (id obj in arr)

return 0;

}

nsmutablearray

nsmutablearray

#import int main ()

nsdictionary

//nsdictionary
#import int main ()
nsdictionary的空值

#import int main(int argc, const char * argv)

else

return 0;

}

ObjC第六節 OC常用類

oc常用類 foundation nsobject nsnumber nsstring 1 foundation 1.1 乙個框架是由類 方法 函式 文件組合在一起構成的集合,使程式開發更容易 1.2 foundation可以使用一些基本的類,如數字類 字串類 集合類等 1.4 foundation...

第六節 Node陣列

var arr new array var arr array var arr array 1,2,3,4 var arr var arr 1,2,3 instanceof 操作符 array.isarray 方法 1.將物件的鍵值轉換為陣列 object.keys 2.string型別轉換為陣列 ...

第六節 雙鏈表

結構特點 template struct dnode 設指標p指向雙鏈表中某一結點,則有下式成立 p llink rlink p p rlink llink 建立空雙鏈表 first rlink null first llink null last first 插入操作 q rlink p rlin...