一、CGPoint定义一个点,两个参数分别是x,y
1>CGPoint p = {100,100};
2>CGPoint p;
p.x = 100;
p.y = 100;
3>CGPoint p = CGPointMake(100,100);
二、CGSize定义一个大小:两个参数分别是width , height
三、CGRect定义一个矩形:四个参数分别是origin.x , origin.y , size.width , size.height
四、NSRange定义一个范围:两个个参数分别是:location , length
五、静态字符串NSString赋值语句:
1>NSString * str = [NSString stringWithFormat:@"%d",123];
2>NSString * str = [NSString stringWithString:@"abc"];
3>NSString * str = [[NSString alloc]initWithFormat:@"%d",123];
4>NSString * str = [NSString stringWithUTF8String:"abc"];
六、1>将字符串全部变为大写:str = [str uppercaseString];
2>全部变为小写:str = [str lowercaseString]
七:求字符串长度:1>使用[str lengthOfBytesUsingEncoding:NSUTF8StringEncoding]时,汉字长度为3;
2>使用 [str length]时,汉字长度为1;
八、判断字符串中是否有某个字符串:使用NSRrange range = [str rangeOFString:@"ab"]; 再利用if语句判断range.location==NSNotFound来执行
九、字符串内容的比较:使用if语句判断[str isEqualToString:@"abc"]来执行
十、字符串转换为数值:int a = [str integerValue];//double a = [str doubleValue];
十一、判断是否以什么字符串开头:bool b = [str hasPrefix:@"a"];//判断结尾使用 [str hasSuffix]
十二、提取子字符串:1>从某个地方开始提取:[str substringFromIndex:2];//从0开始数,包括该位置
2>提取到某个位置:[str substringToIndex:3];//从0开始数,不包括该位置
3>提取中间的几个字符:NSRange range = {1,2};[str substringWithRange:range];//可以利用该函数实现字符串的反向输出
十三、将字符串两端的空格剔除:[str stringByTrimmingCharrctersInSet:[NSCharacterSet whitespaceCharacterSet]];
十四、在字符串后面添加字符串:[str stringByAppendingString:@"abc"]
十五、在字符串中修改内容:NSRange r ={0,1};[str replaceCharactersInRange:r withString:@"abc"];
十六、在字符串中删除内容:[str deleteCharactersInRange:r];
十七、在字符串中添加内容:NSMutableString * str = [NSMutableString stringWithCapacity:100];[string appendFormat:@"123"];
十八、将字符串写入文件:[str writeToFile:@"abc.txt" automically:NO encoding:NSUTF8StringEncoding error:nil];
十九、静态数组三种赋值方法:
1>NSArray * array = @[@"1",@"2",@"3"];//直接赋值
2>NSArray * array = [NSArray arrayWithObjects:@"1",@"2",@"3",nil];
3>NSArray * array = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",nil];
两种遍历方法:
1>循环遍历for(int i=0;i<[array count];i++)
2>快速遍历for(NSString * str in array)
二十、动态数组:NSMutableArray *arr = [NSMutableArray arrayWithCapacity:20]
1>添加对象:[arr addObject:[NSString stringWithFormat:@"123"];
2>删除对象:[arr removeObject:@"2"];
3> 插入对象:[arr insertObject:@"abc" atIndex:0];
二十一、集合里的内容不能重复:
NSSet * set = [NSSet setWithObject:@"1",@"2",@"3",@"2",nil];
NSArray * arr = [set allObjects];
arr = [arr sortedArrayUsingSelector:@selector(compare:)];
二十二、动态集合:
NSMutableSet * set = [NSMutableSet setWithCapacity:20];
二十三、静态与动态Dictionary
NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"张三",@"1",@"李四",@"2", nil];//value,key
NSMutableDictionary * dict = [[NSMutableDictionary alloc]initWithCapacity:20];
[dict setObject:@"zhangsan" forKey:@"1"];//添加
[dict setObject:@"lisi" forKey:@"2"];
[dict setObject:@"wangwu" forKey:@"1"];
[dict removeObjectForKey:@"2"];//删除
NSArray * keys = [dict allKeys];
for(NSString *str in keys)
{
NSLog(@"key=%@,value=%@",str,[dict objectForKey:str]);
}
二十四、数组中只能存储对象型,所以在存储前首先利用NSValue,NSNumber变为对象型
CGPoint p1 = {100,100};
NSValue * v = [NSValue valueWithPoint:p1];
NSArray *arr = [NSArray arrayWithObjects:v,nil]
二十五、NSData:
NSArray * array = [[NSArray alloc]initWithObjects:@"1111",@"2222",@"3333",@"4444",nil];
NSArray *array = [NSArray arrayWithContentsOfFile:@"array.plist"];//array writeToFile: @"array.plist" atomically:NO];写入文件中,NSSet , NSDictionary同理
NSData * data = [NSData dataWithContentsOfFile:@"a.jpg"];//读出a.jpg文件的数据
[data writeToFile:@"b.jpg" atomically:YES];//写给b.jpg文件。相当于复制功能