您好,登錄后才能下訂單哦!
// NSSet
//特點:1. 存儲對象 2. 無序 3. 隨機取數據 4.元素不能重復
//setWithObject: 類方法創建集合
Person * p1 = [[Person alloc]initWithName:@"張1" sex:@"男" age:71];
Person * p2 = [[Person alloc]initWithName:@"李2" sex:@"男" age:22];
Person * p3 = [[Person alloc]initWithName:@"王3" sex:@"女" age:63];
Person * p4 = [[Person alloc]initWithName:@"錢4" sex:@"男" age:71];
Person * p5 = [[Person alloc]initWithName:@"趙5" sex:@"男" age:22];
NSSet * set = [NSSet setWithObjects:p1, p2, p3, p4, p5, nil];
NSLog(@"從字典內取值:%@",[set anyObject]);
for (int i = 0; i < 3; i++) {
NSLog(@"%@", [set anyObject]);
}
打印結果:
從字典內取值:王3 女 63
王3 女 63
王3 女 63
王3 女 63
//快速枚舉
Person * p1 = [[Person alloc]initWithName:@"張1" sex:@"男" age:71];
Person * p2 = [[Person alloc]initWithName:@"李2" sex:@"男" age:22];
Person * p3 = [[Person alloc]initWithName:@"王3" sex:@"女" age:63];
Person * p4 = [[Person alloc]initWithName:@"錢4" sex:@"男" age:71];
Person * p5 = [[Person alloc]initWithName:@"趙5" sex:@"男" age:22];
NSArray * array = [NSArray arrayWithObjects:p1, p2, p3, p4, p5, nil];
for (Person * per in array) {
NSLog(@"%@", per);
}
NSArray * stringsArray = [NSArray arrayWithObjects:@"zhangsan", @"wangwu", @"liliu",@"sunqi", nil];
for (NSString * str in stringsArray) {
NSLog(@"%@", str);
}
//數組里類型不一樣可以用 id 如果只打印 Person類,需要用isKindOfClass判斷一下類型
// if (obj isKindOfClass [Person class])
NSArray * stringsArray1 = [NSArray arrayWithObjects:@"zhangsan",p1, p2, p3, @"wangwu", @"liliu",@"sunqi", nil];
for (id obj in stringsArray1) {
NSLog(@"%@", obj);
}
張1 男 71
李2 男 22
王3 女 63
錢4 男 71
趙5 男 22
zhangsan
wangwu
liliu
sunqi
zhangsan
張1 男 71
李2 男 22
王3 女 63
wangwu
liliu
sunqi
//遍歷字典 for in
NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"name", @"男", @"sex", @"20", @"age", nil];
for (NSDictionary * key in dic) { //遍歷字典,只能遍歷每個key的內容
NSString * value = [dic objectForKey:key];
NSLog(@"%@", value);
}
zhangsan
男
20
Person * p1 = [[Person alloc]initWithName:@"張1" sex:@"男" age:71];
Person * p2 = [[Person alloc]initWithName:@"李2" sex:@"男" age:22];
Person * p3 = [[Person alloc]initWithName:@"王3" sex:@"女" age:63];
Person * p4 = [[Person alloc]initWithName:@"錢4" sex:@"男" age:71];
Person * p5 = [[Person alloc]initWithName:@"趙5" sex:@"男" age:22];
NSMutableArray * array = [NSMutableArray arrayWithObjects:p1, p2, p3, p4, p5, nil];
/*******************************************************************************************************************************************************************************************
運行崩潰, for in 語法里面不允許對數組或字典,增刪元素(開始已經確定了要遍歷的次數, 數組刪掉一個位置會發生變化,導致崩潰)
for (Person * per in array) {
[array removeObject:per]; //錯誤!!
}
NSLog(@"%@", array);
*/
//***************sortedArrayUsingSelector: 數組排序方法***********************
NSArray * stringArray = [NSArray arrayWithObjects:@"lisi", @"wangwu", @"cuiyayun", @"xiaohao", @"yihunyun", @"liuyan", @"gobin", @"jifeifei", @"fangdatong", @"fanbingbing", nil];
NSLog(@"%@", stringArray);
//sortedArrayUsingSelector:
//對字符串數組排序
NSArray * newArray = [stringArray sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"%@", newArray);
2015-01-17 23:25:58.736 OC 一月16號之2[2747:303] (
lisi,
wangwu,
cuiyayun,
xiaohao,
yihunyun,
liuyan,
gobin,
jifeifei,
fangdatong,
fanbingbing
)
2015-01-17 23:25:58.738 OC 一月16號之2[2747:303] (
cuiyayun,
fanbingbing,
fangdatong,
gobin,
jifeifei,
lisi,
liuyan,
wangwu,
xiaohao,
yihunyun
)
Person * p1 = [[Person alloc]initWithName:@"zhangsan" sex:@"男" age:71];
Person * p2 = [[Person alloc]initWithName:@"lier" sex:@"男" age:22];
Person * p3 = [[Person alloc]initWithName:@"wangsan" sex:@"女" age:63];
Person * p4 = [[Person alloc]initWithName:@"qiansi" sex:@"男" age:76];
Person * p5 = [[Person alloc]initWithName:@"zhaowu" sex:@"男" age:59];
NSArray * array = [NSArray arrayWithObjects:p1, p2, p3, p4, p5, nil];
//按年齡進行排序
NSArray * newArray = [array sortedArrayUsingSelector:@selector(sortByAge:)];
NSLog(@"%@", newArray);
//按名字降序排列,可以在方法里調用 compare:
NSArray * newArray1 = [array sortedArrayUsingSelector:@selector(sortByName:)];
NSLog(@"%@", newArray1);
附:
- (NSComparisonResult)sortByAge:(Person *)p
{
if (_age < [p age]) {
return NSOrderedAscending;//-1升序
}
else if(_age > [p age])
return NSOrderedDescending; //1 降序
else return NSOrderedSame;
}
- (NSComparisonResult)sortByName:(Person *)p
{
return [[p name] compare:_name];
}
年齡降序: (
"lier \U7537 22",
"zhaowu \U7537 59",
"wangsan \U5973 63",
"zhangsan \U7537 71",
"qiansi \U7537 76"
)
名字降序 (
"zhaowu \U7537 59",
"zhangsan \U7537 71",
"wangsan \U5973 63",
"qiansi \U7537 76",
"lier \U7537 22"
)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。