Foundation框架包含了很多开发中常用的数据类型: 结构体、枚举、类
一、常用结构体
Nsrange
NSPoint\CGPoint
NSSize\CGSize
NSRect\CGRect
1.NSRange的定义
1) NSRange包含了location和length两个属性
typedef struct _NSRange{
NSUInteger location;
NSUInteger length;
} NSRange;
2)创建方式
NSRange r1 = {2,4}; // 不用
NSRange r2 = {.location = 2, .length =4 }; // 不用
NSRange r3 = NSMakeRange(2,4); // 掌握
3)查找某个字符串在str中的范围
// 查找love在字符串的范围如果找不到该字符串的范围,则length=0,location=NSNotFound=unsigned long =-1 2.NSPoint\CGPoint、NSSize\CGSize、NSRect\CGRect的创建
NSString *str = @"i love oc";
NSRange range = [str rangeOfString:@"love"];
NSLog(@"location=%ld,length=%ld",range.location,range.length);
1)定义
struct CGPoint{
CGFloat x;
CGFloat y;
};
typedef struct CGPoint CGPoint;
struct CGSize{
CGFloat width;
CGFloat height;
};
typedef struct CGSize CGSize;
struct CGRect{
CGPoint origin;
CGSize size;
};
typedef struct CGRect CGRect;
2)创建方式
CGPoint p1 = NSMakePoint(10, 20);
NSPoint p2 = CGPointMake(10, 10); // 最常用
NSSize s1 = CGSizeMake(100, 80);
NSSize s2 = NSMakeSize(100, 80);
CGSize s3 = NSMakeSize(100, 100);
CGRect r1 = CGRectMake(0, 0, 100, 80);
CGRect r2 = {{0,0},{100,80}};
CGRect r3 = {p1,s2};
CGRect r4 = {CGPointZero,CGSizeMake(100, 80)};
3)将结构体转为字符串
NSString *str = NSStringFromPoint(p2);
NSString *str2 = NSStringFromSize(s1);
NSString *str3 = NSStringFromRect(r4);
NSLog(@"%@",str3);
4)比较判断两个点是否相同
BOOL b = CGPointEqualToPoint(CGPointMake(10, 20), CGPointMake(10, 10));
//CGRectEqualToRect(<#CGRect rect1#>, <#CGRect rect2#>)
CGSizeEqualToSize(CGSizeMake(100, 80), CGSizeMake(100, 100));
BOOL b2 = CGRectContainsPoint(CGRectMake(50, 40, 100, 50), CGPointMake(130, 70));
NSLog(@"%d",b2);
二、字符串
// NSString :不可变字符串1.NSString字符串
// NSMutableString :可变字符串
1)创建字符串的几种方式</h4><pre name="code" class="objc"><pre name="code" class="objc"> NSString *s1 = @"jack";
NSString *s2 = [[NSString alloc] initWithString:@"Rose"];
NSString *s3 = [[NSString alloc] initWithFormat:@"age is %d",10];
// c字符串-->oc字符串
NSString *s4 = [[NSString alloc] initWithUTF8String:"jack"];
// oc字符串-->c字符串
const char* cs = [s4 UTF8String];
2)读取文件中得字符串
// NSUTF8StringEncoding 用到中文就可以用这种编码
NSString *s5 = [[NSString alloc] initWithContentsOfFile:@"/users/apple/desktop/1.txt" encoding:NSUTF8StringEncoding error:nil];
NSString *url = [[NSURL alloc] initWithString:@"file///users/apple/desktop/1.txt"];
NSString *s6 = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"\n%@",s6);
// 一般度会有一个类方法跟对象方法配对
[NSURL URLWithString:@"jack"];
[NSURL fileURLWithPath:@“/users/apple/desktop/1.txt”];
[NSString stringWithFormat:@"Rose"];
[NSString stringWithContentsOfFile:@"/users/apple/desktop/1.txt" encoding:NSUTF8StringEncoding error:nil];
3)写入字符串
NSString *str = @"1234";
NSURL *url = [NSURL fileURLWithPath:@"/users/apple/desktop/my.txt"];
[str writeToFile:url atomically:YES encoding:NSUTF8StringEncoding error:nil];
2.NSMutableString字符串
1)拼接NSMutableString *s1 = [NSMutableString stringWithFormat:@"age is 10 "];
// 拼接内容到s1后面
[s1 appendString:@"11 12"];
NSLog(@"%@",s1);
2)删除某个位置的字符串
//获取is的范围
NSRange range = [s1 rangeOfString:@"is"];
[s1 deleteCharactersInRange:range];
NSLog(@"%@",s1);
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------