Foundation框架下的基本类

来源:互联网 发布:媒体姓党 魂归大海知乎 编辑:程序博客网 时间:2024/05/19 21:01

Foundation框架下的基本类

NSValue/NSNumber

功能

将OC和C语言当中的基本数据类型转换成实例对象,即将值类型转换成引用类型
OC和C中的基本数据类型
int a = 5;
float b = 4.5;
double c = 34.5545;
char d = ‘c’;
// CGPoint point = {3,5};
// CGSize size = {30,47};
// CGRect rect = {point,size};
CGPoint point = CGPointMake(3, 5);
CGSize size = CGSizeMake(40, 60);
CGRect rect = CGRectMake(3, 5, 40, 60);
NSRange range = NSMakeRange(3, 7);
NSEdgeInsets edgeInsets = NSEdgeInsetsMake(10, 10, 10, 10);
NSValue 为 NSNumber 的父类. 其中NSValue类可以将CGPoint、CGSize、CGRect、NSRange、NSEdgeInsets转换成对象

NSValue *pointValue = [NSValue valueWithPoint:point];
NSValue *sizeValue = [NSValue valueWithSize:size];
NSValue *rectValue = [NSValue valueWithRect:rect];
NSValue *rangeValue = [NSValue valueWithRange:range];
NSValue *edgeInsetsValue = [NSValue valueWithEdgeInsets:edgeInsets];
其中NSNumber类可以将int 、float、 double、 char、BOOL等C中基本数据类型转换成对象

NSNumber *intNumber = [NSNumber numberWithInt:1];
NSNumber *integerNumber = [NSNumber numberWithInteger:12];
NSNumber *floatNumber = [NSNumber numberWithFloat:12.3];
NSNumber *doubleNumber = [NSNumber numberWithDouble:12.455];
NSNumber *charNumber = [NSNumber numberWithChar:’a’];
NSNumber *boolNumber = [NSNumber numberWithBool:YES];
我们将C和OC中的基本数据类型(值类型)转换成对象(引用类型)的过程,叫做封装。相对应的,也有一个将对象转变成基本数据类型,此过程叫做拆包。

//拆包
CGPoint point1 = [pointValue pointValue];
CGSize size1 = [sizeValue sizeValue];
CGRect rect1 = [rectValue rectValue];
NSRange range1 = [rangeValue rangeValue];
NSEdgeInsets edgeInsets1 = [edgeInsetsValue edgeInsetsValue];
int a1 = [intNumber intValue];
NSInteger a2 = [integerNumber integerValue];
float a3 = [floatNumber floatValue];
double a4 = [doubleNumber doubleValue];
char a5 = [charNumber charValue];
BOOL a6 = [boolNumber boolValue]
NSString / NSMutableString
OC中的字符串具有强大的功能,即封装性极强,我们只需要找到对应API,就可以对字符串做相应操作。OC中字符串分为 不可变字符串可变字符串,其中可变字符串不可变字符串的子类.
在iOS开发中,字符串通常用作显示文本,即作为UILabelUITextFeild等一些UIKit框架下控件的显示文本
NSString 不可变字符串
初始化字符串
1.快速初始化

NSString *string1 = @”bokanwisdom”;

2.格式化的方式初始化字符串

NSString *string4 = [NSString stringWithFormat:@”%d%f%@”,3,3.14,@”dajiahao”];

note:对于用格式化方式来构造字符串实例来讲,其作用并不是仅仅来构造一个字符串对象,它还可以用来做字符串拼接
3.从本地文件中读取字符串

NSString *string7 = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];

NSString *string8 = [NSString stringWithContentsOfFile:path usedEncoding:&encoding error:&error];

4.从网络中读取一个字符串

NSURL *url = [NSURL URLWithString:@”http://www.baidu.com“];
NSString *string9 = [NSString stringWithContentsOfURL:url encoding:4 error:&error];
字符串的操作
1.字符串长度

NSUInteger length = string10.length;

2.字符串的截取

   NSString *subStr1 = [string10 substringToIndex:8];   NSString *subStr2 = [string10 substringFromIndex:8];    NSString *subStr3 = [string10 substringWithRange:NSMakeRange(8, length-8)];

3.字符串比较

==:表示两个字符串内容和指针都相同

if (subStr2 == subStr3) {
//内容和指针都相等时,才成立
NSLog(@”subStr2和subStr3相等”);
}else{
NSLog(@”subStr2和subStr3不相等”);
}
if (subStr2 isEqualToString:subStr3) {
NSLog(@”subStr2和subStr3内容相同”);

    }else{        NSLog(@"subStr2和subStr3内容不相同");    }

根据字符串拿到对应的range
NSRange range = [string10 rangeOfString:@”人家”];

5.判断字符串中是否有xx前缀或xx后缀

    NSString *string11 = @"www.baidu.com";    if ([string11 hasPrefix:@"http://"]) {        NSLog(@"含有此前缀");    }else{        NSLog(@"不含有此前缀");    }    NSString *string12 = @"sdfdf.jpg";    if([string12 hasSuffix:@".jpg"]||[string12 hasSuffix:@".png"]){        NSLog(@"含有此后缀");    }

6.字符串拼接

NSString *string13 = [@”http://” stringByAppendingString:string11];
NSLog(@”string13 = %@”,string13);

    NSString *domainStr = @"http://baidu.com";    NSString *xxx = @"login";    //http://baidu.com/login   NSString *string14 = [NSString stringWithFormat:@"%@/%@",domainStr,xxx];    NSLog(@"string14 = %@",string14);    NSString *string15 = [domainStr stringByAppendingPathComponent:xxx];    NSLog(@"string15 = %@",string15);    //http://b.hiphotos.baidu.com/image/pic/item/e4dde71190ef76c666af095f9e16fdfaaf516741.jpg    NSString *imgPath = @"http://b.hiphotos.baidu.com/image/pic/item/e4dde71190ef76c666af095f9e16fdfaaf516741.jpg";    imgPath = [imgPath lastPathComponent];    NSLog(@"imgPath = %@",imgPath);    path = [path lastPathComponent];    NSLog(@"path = %@",path);    NSString *theImageName = @"lishucheng";   theImageName = [theImageName stringByAppendingPathExtension:@"png"];    NSLog(@"theImageName = %@",theImageName);
0 0