Function中的方法
来源:互联网 发布:万网哪些域名可以备案 编辑:程序博客网 时间:2024/05/19 16:49
#import<Fundation/Fundation.h>
一NSNumber
1 初始化
基本类型
Int i=1;
Char c=’a’;
Float b = 9.9;
BOOL isBool = NO;
将基本类型封装成对象
用类方法的实现
NSNumber * intNumber = [NSNumber numberWithInt :i];
NSNumber * floatNumber = [NSNumber numberWIthFloat:b];
用实例方法实现
NSNumber *cNumber =[ [NSNumber alloc]initWithChar:c];
NSNumber *bNumber = [[NSNumber alloc]initWithBool:isBool];
将类转化成基本类型
i = [intNumber intValue];
b = [floatNumber floatValue];
c = [cNumber charValue];
isBool = [bNumber boolValue];
打印
NSLog(@”intNumber=%@ floatNumber=%@ cNumber=%@ bNumber=%@ %d %d %d %d”,intNumber,floatNumber,cNumber,bNumber,i,,b,c,isBool);
二NSString
1 初始化
常量字符串
NSString *str1=@”hello”;
字符串的创建
空字符串的创建
NSString *str1 = [[NSString alloc]init];
Str1 = @”hello”;
NSString *str2 = [[NSString string];
快速创建字符串
NSString *str1= [[NSString alloc]initWithString:@”hello”];
NSString *str2= [NSString stringWithString:@”world”,1];
格式化创建字符串
NSString * str3 = [NSString alloc]initWithFomat:@“%@_%@_%@_%d”,@”I”,@”like”,@”world”,11];
2 判断字符串是否相等
isEqualToString:
If(str1 isEqualToString: str2)
{
NSLog(“str1和str2相等”);
}else
{
NSLog(“str1和str2不相等”);
}
}
与“==”的区别是,前者判断字符串是不是相等,后者判断对象是不是相等
基本数据类型转换成字符串
Str1 =[ [NSString alloc]initWithFomat:@”%d”,5]
字符串转化成基本类型
NSString * c = [NSString alloc]initWithFomat:@”%c”,b];
int i = [c intValue];
将字符串转化成数组
NSArray * array = [str2 componentsSeparatedByString:@”_”];
字符串的截取
NSString * str5 = @”abcdefg”
NSString *str6 = [str5 subStringToIndex:5];
NSString *str7 = [str5 subStringFromIndex:2];
NSRange range range.location = 2;
Range.length = 4;
str6 = [str5 subStringWithRange :range]
字符串查找
NSRange range2 = [str5 rangeOfString:@”a”]
可变长度的字符串
NSMutableString *mutableS =[ [NSMutableString alloc] stringWithString:@”老鼠”]
动态插入内容
[mutableS insertString:@”爱大米” atIndex :3]
三NSDictionary
1 初始化
NSNumber * numObject = [NSNumber numberWithInt:100];
初始化一个数组
NSDictionary * dictionary = [NSDictionary dictionaryWithObject:numObj forKey:@"key1"];
初始化多维数组
NSDictionary * dictionary2 = [[NSDictionary dictionaryWithObjectsAndKeys:@”Hello”,@”key1”,@”world”,@”key2”,nil]
用一个字典初始化另外一个字典
NSDictionary *dic3 = [NSDictionary dictionaryWithDictionary:dictionary2];
根据key获取key所对应的value
NSLog(@"key3 value = %@",[dic2 objectForKey:@"key3"]);
可以获取所有的keys
NSDictionary *allKeys = [dic3 allKeys];
NSLog[“all keys = %@”,allKeys];
可以获取所有的values
NSArray *allvalues = [dic3 allValues];
NSLog(@"NSarray allvalues = %@",allvalues);
可变字典
----- 初始化
NSMutableDictionary*dic4=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"one",@"key4",@"two",@"key5", nil];
NSLog(@"dic4 : %@",dic4);
//定义成空字典
NSMutableDictionary *dic5 = [NSMutableDictionary dictionary];
//将字典dic2整体添加到dic4钟
[dic4 addEntriesFromDictionary:dic2];
NSLog(@"addEntriesFromDictionary dic2 : %@",dic4);
//添加一个元素
[dic4 setValue:@"three" forKey:@"key6"];
NSLog(@"dic4 setValue : %@",dic4);
//根据key获取value
NSLog(@"key6 = %@",[dic4 objectForKey:@"key6"]);
//------ 字典的遍历
//1)一般的遍历
NSArray *keys4 = [dic4 allKeys];
for(int i=0;i<[dic4 count];i++){
NSLog(@"dic4 key = %@,value=%@",[keys4 objectAtIndex:i],[dic4 objectForKey:[keys4 objectAtIndex:i]]);
}
NSLog(@"-----------------------");
//2)高效的for
for (NSString *key in dic4){
NSLog(@"dic4 key = %@ ,value = %@",key,[dic4 objectForKey:key]);
}
NSLog(@"-----------------------");
//3)使用枚举进行遍历
NSEnumerator *enum1 = [dic4 keyEnumerator];
//获取key,如果不为空,则进行偏移
id key = [enum1 nextObject];
while (key) {
NSLog(@"key = %@ ,value = %@ ",key,[dic4 objectForKey:key]);
key = [enum1 nextObject];
}
//----------------------------------NSSet-------------------
//----- 定义、初始化
NSSet *set = [[NSSet alloc] initWithObjects:@"one",@"one",@"two",nil];
//用数组定义NSSet;
NSArray *arrayset = [NSArray arrayWithObjects:@"1",@"2",@"3",nil];
NSSet *set2 = [NSSet setWithArray:arrayset];
NSLog(@"set1 = %@,set2 = %@",set,set2);
//访问方式
//----- 获取长度
int len = [set2 count];
NSString *s = [set2 anyObject];
NSLog(@"set2 length = %d,obj = %@",len,s);
}
return 0;
}
四NSArray
1 定义并初始化数组
NSArray * array = [NSArray arrayWithObject:@”hello”];
NSArray *array1 = [NSArray arrayWithObjects:@”I”,@”love”,@”my”,@”mother”];
NSArray *array2 = [NSArray arrayWithArray:array1];
2 数组的访问
数组的长度
[array2 count];
数组元素的访问
[array2 atIndex:2];
将数组元素连接成一个字符串
[array2 componentsJoinedByString:@"_"];
可变数组的使用
NSMutableArray * mutableArray = [NSMutableArray arrayWitObject:@”v”,nil];
增加数组元素
NSMutableArray * mutableArray2 = [mutableArray addObject:@”g”];
增加一个数组
NSMutableArray * mutableArray3 = [mutableArray addObjectFromArray:array1]
计算长度
[mutableArray3 count];
移除最后一个元素
[mutableArray3 removeLastObject];
移除指定的一个元素
[mutableArray3 removeObjectAtIndex:2];
数组元素的便利访问
传统的方式
For(int i = 0; i< [mutableArray3 count];i++)
{
NSLog(@“%@”,[mutableArray3 objectAtIndex:i]);
}
快速访问
For(NSString *str in mutableArray)
{
NSLog(@“%@”,str);
}
[mutableArray3 atIndex:2]
- Function中的方法
- JavaScript中的Function.prototype.bind()方法简介
- Python中的map(function,iterable, ...)方法
- 解剖ExtJS中的Function类中的createDelegate方法
- php中的function函数参数扩展的方法
- java8中的stream,function包的常用方法总结
- javascript中的Function与function
- C++中的function object
- javascript中的Function对象
- JS Function中的return
- C++中的function object
- JavaScript中的function使用方法
- ECMAScript中的Function
- Python中的Function
- javascript中的new function
- c++中的function object
- JavaScript中的function
- C++中的function object
- js求两个日期时间差。精确到年月日到秒
- 容器类常用方法(2)
- oracle分组求个数
- Tomcat解压版安装后不能快捷启动报未安装某tomcat服务
- 【练习向】jQuery基础教程第四版课后练习——Book07_jQuery_使用插件
- Function中的方法
- 定义一个Course类,并编写几个成员函数
- Deep Learning (深度学习) 学习总结
- Microsoft Win32 to Microsoft .NET Framework API Map
- 春天的美妙
- 利用栈和类实现求一个数的所有约数
- jQuery滑动式幻灯片播放特效
- 类的成员函数指针(比较深入)
- 逝去