NSMutableArray 可變數組數組
//initWithCapacity: 建立一個數組ide
// - (instancetype)initWithCapacity:(NSUInteger)numItemsspa
NSMutableArray * array = [[NSMutableArray alloc]initWithCapacity:1];//1表示剛開始給數組分配一個對象大小的內存,當數組已經有一個對象時,再放一個對象時,數組會自動添加1個對象大小的內存,(n表明追加的內存個數). 相同的對象能夠被屢次存入數組中對象
NSLog(@"%@", array);索引
addObject: 給數組中添加元素(對象)內存
- (void)addObject:(id)anObjectci
NSMutableArray * array = [[NSMutableArray alloc]initWithCapacity:1];rem
//1表示剛開始給數組分配一個對象大小的內存,當數組已經有一個對象時,再放一個對象時,數組會自動添加1個對象大小的內存,(n表明追加的內存個數). 相同的對象能夠被屢次存入數組中string
Person * p1 = [[Person alloc]initWithName:@"張1" sex:@"男" age:71];it
Person * p2 = [[Person alloc]initWithName:@"李2" sex:@"男" age:22];
Person * p3 = [[Person alloc]initWithName:@"王3" sex:@"女" age:63];
[array addObject:p1];[array addObject:p2];[array addObject:p1];
NSLog(@"%@", array);
//insertObject:atIndex:
[array insertObject:p2 atIndex:2];
NSLog(@"在下標2插入p2%@", array);
//removeLastObject
[array removeLastObject];
NSLog(@"將最後元素刪除%@", array);
//removeObject:
[array removeObject:p2];
NSLog(@"將p2元素刪除%@", array);
//removeObject:inRange:
[array addObject:p2]; [array addObject:p3];
[array addObject:p1];
NSLog(@"添加後%@", array);
[array removeObject:p1 inRange:NSMakeRange(0, 4)];
NSLog(@"刪除給定個範圍內出現的對象%@", array);
//removeObjectAtIndex:
[array removeObjectAtIndex:1];
NSLog(@"刪除給定下標元素%@", array);
//replaceObjectAtIndex:withObject:
[array replaceObjectAtIndex:0 withObject:p3];
NSLog(@"替換後%@", array);
//exchangeObjectAtIndex:withObjectAtIndex:
[array addObject:p1];[array addObject:p2];
[array exchangeObjectAtIndex:1 withObjectAtIndex:2];
NSLog(@"交換下標1和下標2元素%@", array);
打印結果:
2015-01-15 22:53:04.514 OC 1月15號[3452:303] (
"\U5f201 \U7537 71",
"\U674e2 \U7537 22",
"\U5f201 \U7537 71"
)
2015-01-15 22:53:04.516 OC 1月15號[3452:303] 在下標2插入p2(
"\U5f201 \U7537 71",
"\U674e2 \U7537 22",
"\U674e2 \U7537 22",
"\U5f201 \U7537 71"
)
2015-01-15 22:53:04.517 OC 1月15號[3452:303] 將最後元素刪除(
"\U5f201 \U7537 71",
"\U674e2 \U7537 22",
"\U674e2 \U7537 22"
)
2015-01-15 22:53:04.517 OC 1月15號[3452:303] 將p2元素刪除(
"\U5f201 \U7537 71"
)
2015-01-15 22:53:04.518 OC 1月15號[3452:303] 添加後(
"\U5f201 \U7537 71",
"\U674e2 \U7537 22",
"\U738b3 \U5973 63",
"\U5f201 \U7537 71"
)
2015-01-15 22:53:04.556 OC 1月15號[3452:303] 刪除給定個範圍內出現的對象(
"\U674e2 \U7537 22",
"\U738b3 \U5973 63"
)
2015-01-15 22:53:04.556 OC 1月15號[3452:303] 刪除給定下標元素(
"\U674e2 \U7537 22"
)
2015-01-15 22:53:04.557 OC 1月15號[3452:303] 替換後(
"\U738b3 \U5973 63"
)
2015-01-15 22:53:04.557 OC 1月15號[3452:303] 交換下標1和下標2元素(
"\U738b3 \U5973 63",
"\U674e2 \U7537 22",
"\U5f201 \U7537 71"
)
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];
NSArray * array = [NSArray arrayWithObjects:p1,p2,p3,@"1111", @"222", nil];
NSLog(@"%@",array);
for (int i = 0; i < [array count]; i++) {
id obj = [array objectAtIndex:i];
//檢查對象是不是某個類的對象
// isKindOfClass
if ([obj isKindOfClass:[NSString class]]) {
NSString * newstr = [obj stringByAppendingString:@"藍鷗"];
NSLog(@"%@", newstr);
}
}
2015-01-15 22:57:53.527 OC 1月15號[3466:303] (
"\U5f201 \U7537 71",
"\U674e2 \U7537 22",
"\U738b3 \U5973 63",
1111,
222
)
2015-01-15 22:57:53.529 OC 1月15號[3466:303] 1111藍鷗
2015-01-15 22:57:53.530 OC 1月15號[3466:303] 222藍鷗
//*************NSNumber***************
//NSNumber 數值類 把基本數值型轉換成對象性,或者是數值對象轉成基本型
//intValue 把對象轉化爲數值.
// intValue 和 NSNumber numberWithInt: 是一對
int n = 5;
NSNumber * n1 = [NSNumber numberWithInt:n];
NSArray * array = [NSArray arrayWithObjects:n1,[NSNumber numberWithInt:10], nil];
NSLog(@"%@", array);
int result = [[array firstObject] intValue] + [[array lastObject] intValue];
NSLog(@"result = %d", result);
打印結果:
2015-01-15 22:59:36.037 OC 1月15號[3474:303] (
5,
10
)
2015-01-15 22:59:36.039 OC 1月15號[3474:303] result = 15
//NSDictionary 不可修改的字典
//1. 鍵值對 2. 字典無序 3. 存對象 4. 經過key索引對應的值 5. key惟一
//initWithObjectsAndKeys:
NSDictionary * dic = [[NSDictionary alloc]initWithObjectsAndKeys:@"張三",@"name",@"男",@"sex",@"25",@"年齡", nil];
NSLog(@"%@", dic);
2015-01-15 23:00:33.258 OC 1月15號[3484:303] {
name = "\U5f20\U4e09";
sex = "\U7537";
"\U5e74\U9f84" = 25;
}
NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"name", @"M",@"sex", @"30", @"age", nil];
NSLog(@"%@", dic);
//dictionaryWithObjects:forKeys: 類方法,將值和鍵分別對應,返回一個字典
//+ (instancetype)dictionaryWithObjects:(NSArray *)objectsforKeys:(NSArray *)keys
NSArray * arrayValue = [NSArray arrayWithObjects:@"zhangsan",@"30",@"W", nil];
NSArray * keyArray = [NSArray arrayWithObjects:@"name", @"age", @"sex", nil];
NSDictionary * dic1 = [NSDictionary dictionaryWithObjects:arrayValue forKeys:keyArray];
NSLog(@"%@", dic1);
//count //獲得鍵值對的個數
NSLog(@"%ld",[dic1 count]);
//allKeys 獲得全部的鍵
NSLog(@"%@",[dic1 allKeys]);
//allValues 獲得全部的值
NSLog(@"%@", [dic1 allValues]);
打印結果:
2015-01-15 23:01:17.213 OC 1月15號[3492:303] {
age = 30;
name = zhangsan;
sex = M;
}
2015-01-15 23:01:17.216 OC 1月15號[3492:303] {
age = 30;
name = zhangsan;
sex = W;
}
2015-01-15 23:01:17.216 OC 1月15號[3492:303] 3
2015-01-15 23:01:17.217 OC 1月15號[3492:303] (
name,
age,
sex
)
2015-01-15 23:01:17.217 OC 1月15號[3492:303] (
zhangsan,
30,
W
)
//**************************objectForKey: 重要***********************************//
//objectForKey: 根據Key來索引數據
//- (id)objectForKey:(id)aKey
NSArray * arrayValue = [NSArray arrayWithObjects:@"zhangsan",@"30",@"W", nil];
NSArray * keyArray = [NSArray arrayWithObjects:@"name", @"age", @"sex", nil];
NSDictionary * dic1 = [NSDictionary dictionaryWithObjects:arrayValue forKeys:keyArray];
NSString * str = [dic1 objectForKey:@"name"];
NSLog(@"%@", str);
//遍歷字典,取出字典全部的值
for (int i = 0; i < [dic1 count]; i++) {
NSString * key = [[dic1 allKeys] objectAtIndex:i];
NSString * value = [dic1 objectForKey:key];
NSLog(@"遍歷1 %@", value);
}
//遍歷2
NSArray * array = [dic1 allValues];
for (int i= 0; i < [dic1 count]; i++) {
NSString * str = array[i];
NSLog(@"遍歷2 %@", str);
}
2015-01-15 23:05:29.028 OC 1月15號[3524:303] zhangsan
2015-01-15 23:05:29.030 OC 1月15號[3524:303] 遍歷1 zhangsan
2015-01-15 23:05:29.031 OC 1月15號[3524:303] 遍歷1 30
2015-01-15 23:05:29.031 OC 1月15號[3524:303] 遍歷1 W
2015-01-15 23:05:29.032 OC 1月15號[3524:303] 遍歷2 zhangsan
2015-01-15 23:05:29.032 OC 1月15號[3524:303] 遍歷2 30
2015-01-15 23:05:29.032 OC 1月15號[3524:303] 遍歷2 W
//************************** NSMutableDictionary*********************
// NSMutableDictionary
// dictionaryWithCapacity: 建立一個能夠修改內容的字典
// 一個Key對應一個value
// 一個value能夠對應多個key
NSMutableDictionary * dic = [NSMutableDictionary dictionaryWithCapacity:1];
NSLog(@"dic = %@", dic);
//setObject:forKey: 往字典裏添加鍵值對
//- (void)setObject:(id)anObjectforKey:(id<NSCopying>)aKey
[dic setObject:@"zhangsan" forKey:@"name"];
[dic setObject:@"zhangsi" forKey:@"name"];//鍵同樣時 , 會將鍵指的內容替換掉
[dic setObject:@"20" forKey:@"value1"];
[dic setObject:@"20" forKey:@"value2"];
NSLog(@"%@", dic);
//removeObjectForKey:
[dic removeObjectForKey:@"value2"];
NSLog(@"%@", dic);
// [dic removeAllObjects];
// NSLog(@"%@", dic);
NSArray * array = [NSArray arrayWithObjects:@"value1",@"name" , nil];
[dic removeObjectsForKeys:array]; //移除多個鍵對應的值
NSLog(@"%@", dic);
2015-01-15 23:08:49.741 OC 1月15號[3538:303] dic = {
}
2015-01-15 23:08:49.743 OC 1月15號[3538:303] {
name = zhangsi;
value1 = 20;
value2 = 20;
}
2015-01-15 23:08:49.744 OC 1月15號[3538:303] {
name = zhangsi;
value1 = 20;
}
2015-01-15 23:08:49.744 OC 1月15號[3538:303] {
}
//*************************OC之二維數組***********************//
NSArray * firstArray = [NSArray arrayWithObjects:@"111", @"222", @"333", @"444", nil];
NSArray * secondArray = [NSArray arrayWithObjects:@"AAA", @"BBB", @"CCC", nil];
NSArray * thirdArray = [NSArray arrayWithObjects:@"name", @"sex", @"age", nil];
NSArray * bigArray = [NSArray arrayWithObjects:firstArray, secondArray,thirdArray, nil];
NSLog(@"%@", bigArray);
// 1 遍歷數組(兩層循環, 數組裏面套數組)
for (int i = 0; i < [bigArray count]; i++) {
for (int j = 0; j < [[bigArray objectAtIndex:i] count]; j++) {
id a = [[bigArray objectAtIndex:i] objectAtIndex:j];
NSLog(@"遍歷數組之結果:%@", a);
}
}
2015-01-15 23:09:37.710 OC 1月15號[3546:303] (
(
111,
222,
333,
444
),
(
AAA,
BBB,
CCC
),
(
name,
sex,
age
)
)
2015-01-15 23:09:37.713 OC 1月15號[3546:303] 遍歷數組之結果:111
2015-01-15 23:09:37.713 OC 1月15號[3546:303] 遍歷數組之結果:222
2015-01-15 23:09:37.713 OC 1月15號[3546:303] 遍歷數組之結果:333
2015-01-15 23:09:37.714 OC 1月15號[3546:303] 遍歷數組之結果:444
2015-01-15 23:09:37.714 OC 1月15號[3546:303] 遍歷數組之結果:AAA
2015-01-15 23:09:37.715 OC 1月15號[3546:303] 遍歷數組之結果:BBB
2015-01-15 23:09:37.715 OC 1月15號[3546:303] 遍歷數組之結果:CCC
2015-01-15 23:09:37.715 OC 1月15號[3546:303] 遍歷數組之結果:name
2015-01-15 23:09:37.716 OC 1月15號[3546:303] 遍歷數組之結果:sex
2015-01-15 23:09:37.716 OC 1月15號[3546:303] 遍歷數組之結果:age
// 2 數組裏面套字典
NSDictionary * stuDic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan", @"name", @"M", @"sex",@"30", @"age", nil];
NSDictionary * stuDic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"lisi", @"name", @"M", @"sex", @"25", @"age", nil];
NSArray * array = [NSArray arrayWithObjects:stuDic1, stuDic2, nil];
NSLog(@"%@", array);
for (int i = 0; i < [array count]; i++) {
for (int j = 0; j < [[array objectAtIndex:i] count]; j++) {
id result = [[[array objectAtIndex:i]allValues] objectAtIndex:j];
NSLog(@"遍歷後:%@", result);
}
}
2015-01-15 23:10:27.425 OC 1月15號[3557:303] (
{
age = 30;
name = zhangsan;
sex = M;
},
{
age = 25;
name = lisi;
sex = M;
}
)
2015-01-15 23:10:27.427 OC 1月15號[3557:303] 遍歷後:zhangsan
2015-01-15 23:10:27.427 OC 1月15號[3557:303] 遍歷後:M
2015-01-15 23:10:27.428 OC 1月15號[3557:303] 遍歷後:30
2015-01-15 23:10:27.428 OC 1月15號[3557:303] 遍歷後:lisi
2015-01-15 23:10:27.429 OC 1月15號[3557:303] 遍歷後:M
2015-01-15 23:10:27.429 OC 1月15號[3557:303] 遍歷後:25
<<3>>字典裏放數組
NSArray * firstArray = [NSArray arrayWithObjects:@"111", @"222", @"333", @"444", nil];
NSArray * secondArray = [NSArray arrayWithObjects:@"AAA", @"BBB", @"CCC", nil];
NSArray * thirdArray = [NSArray arrayWithObjects:@"name", @"sex", @"age", nil];
NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:firstArray, @"first",secondArray, @"second",thirdArray, @"third", nil];
NSLog(@"%@", dic);
for (int i = 0; i < [dic count]; i++) {//外層循環遍歷字典,
for (int j = 0; j < [[[dic allValues]objectAtIndex:i] count]; j++) {//內層循環遍歷小數組
id a = [[[dic allValues] objectAtIndex:i] objectAtIndex:j];//取出數組裏的內容
NSLog(@"遍歷後%@", a);
}
}
2015-01-15 23:11:20.271 OC 1月15號[3565:303] {
first = (
111,
222,
333,
444
);
second = (
AAA,
BBB,
CCC
);
third = (
name,
sex,
age
);
}
2015-01-15 23:11:20.274 OC 1月15號[3565:303] 遍歷後111
2015-01-15 23:11:20.274 OC 1月15號[3565:303] 遍歷後222
2015-01-15 23:11:20.275 OC 1月15號[3565:303] 遍歷後333
2015-01-15 23:11:20.275 OC 1月15號[3565:303] 遍歷後444
2015-01-15 23:11:20.275 OC 1月15號[3565:303] 遍歷後AAA
2015-01-15 23:11:20.276 OC 1月15號[3565:303] 遍歷後BBB
2015-01-15 23:11:20.276 OC 1月15號[3565:303] 遍歷後CCC
2015-01-15 23:11:20.277 OC 1月15號[3565:303] 遍歷後name
2015-01-15 23:11:20.277 OC 1月15號[3565:303] 遍歷後sex
2015-01-15 23:11:20.277 OC 1月15號[3565:303] 遍歷後age
// 4 字典裏套子典
NSDictionary * stuDic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan", @"name", @"M", @"sex",@"30", @"age", nil];
NSDictionary * stuDic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"lisi", @"name", @"M", @"sex", @"30", @"age", nil];
NSDictionary * bigDic = [NSDictionary dictionaryWithObjectsAndKeys:stuDic1,@"first", stuDic2, @"second", nil];
NSLog(@"%@", bigDic);
for (int i = 0; i < [bigDic count]; i++) {
//外層循環控制大字典鍵值對數
for (int j = 0; j < [[[bigDic allValues]objectAtIndex:i] count]; j++) {
//內層循環控制小字典鍵值對數!!!注意要先取出大字典的值集( allValue ),用objiectAtIndex: 得出小字典,再求小字典的值集,用objiectAtIndex: 求出具體內容
id a = [[[[bigDic allValues]objectAtIndex:i] allValues] objectAtIndex:j];
NSLog(@"遍歷字典(裏套小字典)後%@", a);
}
}
2015-01-15 23:12:36.630 OC 1月15號[3573:303] {
first = {
age = 30;
name = zhangsan;
sex = M;
};
second = {
age = 30;
name = lisi;
sex = M;
};
}
2015-01-15 23:12:36.632 OC 1月15號[3573:303] 遍歷字典(裏套小字典)後zhangsan
2015-01-15 23:12:36.633 OC 1月15號[3573:303] 遍歷字典(裏套小字典)後M
2015-01-15 23:12:36.633 OC 1月15號[3573:303] 遍歷字典(裏套小字典)後30
2015-01-15 23:12:36.634 OC 1月15號[3573:303] 遍歷字典(裏套小字典)後lisi
2015-01-15 23:12:36.634 OC 1月15號[3573:303] 遍歷字典(裏套小字典)後M
2015-01-15 23:12:36.634 OC 1月15號[3573:303] 遍歷字典(裏套小字典)後30