概述
1
// 使用NSNumber 封装double
double dNum = 1.3;NSNumber *double1 = [NSNumber numberWithDouble:dNum];
double dNum1 ;
[double1 getValue:&dNum1];
NSLog(@"double is %lf", dNum1);
NSNumber *n = @45;
NSNumber *n1 = @5.1;
NSNumber *n2 = @5.2f;
struct Human human;
human.age = 100;
strcpy(human.name, "xiaoming ");
Human h;
h.age = 100;
2
// 使用NSValue 封装 int
NSValue *intVal = [NSValue value:&a withObjCType:@encode(int)];
NSLog(@"intVal = %@", intVal);
int b = 0;
[intVal getValue:&b];
NSLog(@"b = %d", b);
3
NSString *S1 = @"China";
NSString *S2 = @"China";
NSString *S3 = [[NSString alloc] initWithString:@"China"];
NSString *S4 = [[NSString alloc] initWithFormat:@"China"];
NSLog(@"%d",S1 == S2);
NSLog(@"%d",S1 == S3);
NSLog(@"%d",S1 == S4);
NSString *S5 = [[NSString alloc] init];
NSString *S6 = [[NSString alloc] init];
NSLog(@"%d",S5 == S6);
4
// 查找子串
NSString *str = @"I love China";
NSString *sub = @"love";
NSRange range = [str rangeOfString:sub];
if (range.length !=0 && range.location != NSNotFound && [str containsString:sub]) {
NSLog(@"loc = %ld, len = %ld",range.location,range.length);
}
// 字符串拼接
NSString *string = @"I love";
NSString *newstring = [string stringByAppendingString:@"I no"];
NSLog(@"newstring is %@",newstring);
newstring = [newstring stringByAppendingString:@" too"];
NSLog(@"newstring is %@",newstring);
//数字字符串
NSString *number = @"343";
int num = number.intValue;
// 大小写
NSString *m = @"I Love You";
NSLog(@"%@",m.uppercaseString);
NSLog(@"%@",m);
NSLog(@"%@",m.lowercaseString);
NSLog(@"%@",m.capitalizedString);
// 可变字符串 enable change String
NSMutableString *mulStr = [[NSMutableString alloc] init];
[mulStr appendString:@"beijing kaifeng ss"];
NSLog(@"%@",mulStr);
//insert a String to String
[mulStr insertString:@"456" atIndex:[mulStr rangeOfString:@"beijing"].location];
NSLog(@"%@",mulStr);
// replace a String into String
[mulStr replaceCharactersInRange:[mulStr rangeOfString:@"beijing"] withString:@"kaifeng"];
NSLog(@"%@",mulStr);
// delete a String into String
[mulStr deleteCharactersInRange:[mulStr rangeOfString:@"ss"]];
NSLog(@"%@",mulStr);
最后
以上就是细心巨人为你收集整理的NSValue和NSNumber和NSString的全部内容,希望文章能够帮你解决NSValue和NSNumber和NSString所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复