OC   API库常见之函数(二)

NSMutableArray   可变数组

//initWithCapacity: 创建一个数组

// - (instancetype)initWithCapacity:(NSUInteger)numItems

NSMutableArray * array  = [[NSMutableArray alloc]initWithCapacity:1];//1表示刚开始给数组分配一个对象大小的内存,当数组已经有一个对象时,再放一个对象时,数组会自动添加1个对象大小的内存,(n代表追加的内存个数).  相同的对象可以被多次存入数组中

NSLog(@"%@", array);

addObject:     给数组中添加元素(对象)

- (void)addObject:(id)anObject

NSMutableArray * array  = [[NSMutableArray alloc]initWithCapacity:1];

//1表示刚开始给数组分配一个对象大小的内存,当数组已经有一个对象时,再放一个对象时,数组会自动添加1个对象大小的内存,(n代表追加的内存个数).  相同的对象可以被多次存入数组中

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];

[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

时间: 2024-08-04 15:03:59

OC   API库常见之函数(二)的相关文章

OC &#160; API库常见之函数

NSString类型 1.initWithFormat - (instancetype)initWithFormat:(NSString *)format 用于返回一个带有格式控制符的字符串,格式控制符里的内容被替代. 例子: int n = 17;CGFloat m = 99.9; //str 是一个地址,  内容存放于堆区 NSString * str = [[NSString alloc]initWithFormat:@"蓝鸥%d班的平均成绩是:%.2f",n, m ]; NSL

VB程序逆向反汇编常见的函数(修改版)

VB程序逆向常用的函数 1) 数据类型转换: a) __vbaI2Str    将一个字符串转为8 位(1个字节)的数值形式(范围在 0 至 255 之间) 或2 个字节的数值形式(范围在 -32,768 到 32,767 之间). b)__vbaI4Str   将一个字符串转为长整型(4个字节)的数值形式(范围从-2,147,483,6482,147,483,647) c)__vbar4Str  将一个字符串转为单精度单精度浮点型(4个字节)的数值形式 d)__vbar8Str   将一个字符

C++的开源跨平台日志库glog学习研究(二)--宏的使用

上一篇从整个工程上简单分析了glog,请看C++的开源跨平台日志库glog学习研究(一),这一篇对glog的实现代码入手,比如在其源码中以宏的使用最为广泛,接下来就先对各种宏的使用做一简单分析. 1. 日志输出宏 这里我们以一条最简单的日至输出为例说明: LOG(WARNING) << "This is a warning message"; 这里LOG是一个宏,其定义如下(logging.h line 487): #define LOG(severity) COMPACT

Zookeeper C API 指南三(回调函数)(转)

2013-02-21 12:54 by Haippy, 9237 阅读, 0 评论, 收藏, 编辑 接上一篇<Zookeeper C API 指南二(监视(Wathes), 基本常量和结构体介绍)>,本文重点介绍 Zookeeper C API 中的各种回调函数. Zookeeper C API 中各种回调函数简介 在具体介绍 Zookeeper C API 之前,首先介绍一下 Zookeeper C API 中的各种回调函数的原型: 监视函数(watch function)原型 typede

Linux的静态函数库和动态函数库

一.在Linux中,有两种函数库,以*.so后缀命名的是动态函数库,以*.a后缀命名的是静态函数库 对于静态函数库:在编译链接时候,程序所需要的函数会从静态函数库中拷贝到执行文件中.当程序运行的时候,就不需要链接外部的函数库了. 对于动态函数库:在编译链接时,程序所需要的函数不会从动态函数库中拷贝出来,而是在程序运行的时候,才会将所需要的函数加载进来. 二.静态函数库 1.静态函数库是多个目标文件*.o的集合 2.可以使用ar命令(archiver)来生成.a文件 gcc -c part_a.c

BCB 查看动态库中有什么函数

切换至 tdump.exe 所在目录: C:\Documents and Settings\Administrator>cd D:\Program Files\CodeGear\RAD Studio\6.0 \bin D:\Program Files\CodeGear\RAD Studio\6.0\bin>tdump.exe D:\Program\CKD\05191800\F nthex32.dll > d:\Fnthex32.txt 输出结(红色字体为函数名): Exports fro

如何用web api在网页中嵌入二维码?

如何用web api在网页中嵌入二维码? 随着智能手机和平板电脑的日益普及,二维码逐渐成了链接智能终端和传统网站的桥梁.在下文中,笔者将介绍几个实时生成二维码的web api,希望能够简化web design过程中的二维码集成工作. 1. 范例一 <img src="http://qrickit.com/api/qr?d=http://www.taobao.com" > 上述代码产生如下的二维码图片: 该web api还支持下面的这些特性, 说明文字:例如addtext=H

C++链接库的使用,二维向量,三维向量,Ubuntu下C++测试向量库

1.#include<iostream> using namespace std; int main() { cout<<"Hello Woeld"<<endl; return 0; } 2.vector.cxx #include<iostream> int main() {int k; char x; cout<<"请输入向量的维度:"<<endl; cin>>k; vector

调用MyFocus库,简单实现二十几种轮播效果

一.首先点击这里下载myFocus库文件,标准文件库就行了,很小仅仅1.4M. myFocus库有以下的好处: a . 文件小巧却高效强大,能够实现二十几种轮播的效果. b . 极其简单的使用,只需要调用就可以使用,下面会介绍方法. c . 灵活的设置,很多参数可以提供设置,比如不想要文字提示,设置高度为0....更多参数适用请见网站教程页面. 二.下载下来之后,解压,看到一个文件夹,如下图所示: 对此文件夹进行一下说明:a . 打开js文件夹,然后有个js文件,就是我们最开始要调用的myfoc