#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSLog(@"-----------范围----------------");
NSRange range;
range.location = 17;
range.length = 4;
NSLog(@"%ld:%ld",range.location,range.length);
NSRange range1={17,4};
NSLog(@"%ld:%ld",range1.location,range1.length);
NSRange range2 =NSMakeRange(17, 4);
NSLog(@"%ld:%ld",range2.location,range2.length);
NSLog(@"-----------字符串----------------");
NSString *str=@"字面量语法"; //推荐字符创建方式
NSLog(@"%@,%ld",str,[str length]);
NSString *str1=[NSString stringWithString:@"非字面量语法 %d:%@"];//不推荐创建方式
str1 = [NSString stringWithFormat:str1,5,@"Hello"];
NSLog(@"%@,%ld",str1,[str1 length]);
//比较字符串
NSString *str2=@"hello 5";
NSString *str3=[NSString stringWithFormat:@"hello %d",5];
if([str2 isEqualToString:str3]){//两者仅对内容进行比较
NSLog(@"值相同");
}
if(str2==str3){//两者进行内存比较是否为同一对象
NSLog(@"同一对象");
}
if([str2 isEqual:str3]){
NSLog(@"相同");
}
NSLog(@"str2:%ld str3:%ld",[str2 hash],[str3 hash]);
//compare 默认忽略大小写比较
NSString *str4= @"8";
NSString *str5 = @"9";
if([str4 compare:str5]){
NSLog(@"compare 不区分大小写,值相同");
}
//忽略大小并按字符个数排序,个数排序是防止字符串100小于99
NSComparisonResult result = [str4 compare:str5 options:NSCaseInsensitiveSearch|NSNumericSearch];
switch (result) {
case NSOrderedAscending:
NSLog(@"升序");
break;
case NSOrderedDescending:
NSLog(@"降序");
break;
case NSOrderedSame:
NSLog(@"相同");
break;
}
//字符包含,区分大小写
NSString *fileName=@"Hello world.mp3";
if([fileName hasPrefix:@"He"]){
NSLog(@"文件名包含前缀");
}
if([fileName hasSuffix:@".mp3"]){
NSLog(@"文件名包含后缀");
}
NSRange range3 =[fileName rangeOfString:@"H2"];
NSLog(@"location:%ld lenght:%ld",range3.location,range3.length);
NSLog(@"-----------可变字符串----------------");
NSMutableString *muStr = [NSMutableString stringWithCapacity:50];
[muStr appendString:@"Name"];
[muStr appendFormat:@" %@",@"John"];
NSLog(@"%@",muStr);
//删除指定包含字符串
NSRange range4 = [muStr rangeOfString:@"m"];//默认忽略大小写
[muStr deleteCharactersInRange:range4];
NSLog(@"%@",muStr);
NSLog(@"-----------数组----------------");
//1.只能存储 Objective-C 对象 2.不能存储 nil 对象
NSArray *arr1 =[NSArray arrayWithObjects:@"1",@"2",@"3", nil];//末尾 nil 值是作为装填完毕判断使用,若数组非末尾存在 nil 则数组有效范围只到 nil 前
NSArray *arr2 = @[@"1",@"2",@"3"];//字面量语法, 字面量语法不允许出现 nil 值
NSLog(@"数组长度%ld %ld",[arr1 count],[arr2 count]);
for (int i=0; i<[arr1 count]; i++) {
NSLog(@"%d %@",i,arr1[i]);
}
NSLog(@"%@",[arr1 objectAtIndex:1]);
//按指定间隔符进行切割
NSString *str6 = @"市北区,市南区,李沧区,黄岛区";
NSArray *arr3 = [str6 componentsSeparatedByString:@","];
for(NSInteger i=0;i<[arr3 count];i++){
NSLog(@"%@",arr3[i]);
}
//将数组按指定间隔符重新组装
NSLog([arr3 componentsJoinedByString:@":"]);//市北区:市南区:李沧区:黄岛区
NSLog(@"-----------可变数组----------------");
//可变数组没有字面量语法
NSMutableArray *muArr = [NSMutableArray arrayWithCapacity:20];
[muArr addObject:@"星期一"];
//[muArr addObject:nil]; //数组中不可存储 nil 值
[muArr addObject:@"星期二"];
[muArr addObject:@"星期三"];
//移除指定下标数据
[muArr removeObjectAtIndex:1];
//枚举
NSEnumerator *enum1 = [muArr objectEnumerator];
[muArr addObject:@"星期四"]; //Objective-C基础教程提示不可删除或增加可变数组容量,实际测试未发现问题
id thingie;
while(thingie = [enum1 nextObject]){
NSLog(@"%@",thingie);
}
//快速枚举
for (NSString *str7 in muArr) {
NSLog(@"%@",str7);
}
//代码块
[muArr enumerateObjectsUsingBlock:^(NSString *str8,NSUInteger index,BOOL *stop){
NSLog(@"Block %@ %ld %@",str8,index,stop?@"YES":@"NO");
}];
NSLog(@"-----------字典----------------");
NSMutableDictionary *muDic = [NSMutableDictionary dictionaryWithCapacity:50];
[muDic setObject:@"青岛" forKey:@1];
[muDic setObject:@"济南" forKey:@2];
[muDic setObject:@"菏泽" forKey:@3];
//移除指定对象
[muDic removeObjectForKey:@2];
//代码块
[muDic enumerateKeysAndObjectsUsingBlock:^(NSString *key,NSString *obj,BOOL *stop){
NSLog(@"key:%@ value:%@",key,obj);
if([key intValue]==1){//满足条件退出循环
*stop=YES;
}
}];
NSLog(@"-----------数字----------------");
NSNumber *num1;
//类型转换
num1= [NSNumber numberWithInt:1];
num1=[NSNumber numberWithChar:'X'];
NSLog(@"%@",num1);
//字面量变量
num1=@'X';//字符型
num1=@123456;//整型
num1=@123.123f;//浮点型
num1=@NO;//布尔型
NSLog(@"%@",num1);
NSLog(@"-----------NSValue----------------");
//NSValue 可以封装任意值
NSRect rect = NSMakeRect(1, 2, 30, 40);
NSPoint np = rect.origin;
NSSize ns = rect.size;
NSLog(@"MSPoint:%@ NSSize:%@",NSStringFromPoint(np),NSStringFromSize(ns));
NSMutableArray *muArr2 = [NSMutableArray arrayWithCapacity:50];
//方式1
//NSValue *value = [NSValue valueWithBytes:&rect objCType:@encode(NSRect)];
//方式2
NSValue *value=[NSValue valueWithRect:rect];
[muArr2 addObject:value];
NSRect rect2 = [[muArr2 objectAtIndex:0]rectValue];
NSLog(@"MSRect%@",NSStringFromRect(rect2));
NSLog(@"-----------NSRect NSPoint NSSize----------------");
CGFloat x = 0.0f;
CGFloat y = 0.0f;
NSPoint np1 = CGPointMake(x, y);//坐标[0,0]点 ,字面量语法 NSPoint np1 ={x,y}
CGFloat w = 30.0f;
CGFloat h = 40.0f;
NSSize ns1 = CGSizeMake(w, h); //宽30 高40
NSRect rect3 = CGRectMake(np1.x, np1.y, ns1.width, ns1.height);
NSLog(@"%@",NSStringFromRect(rect3));
NSLog(@"-----------NSNull----------------");
NSMutableArray *muArr3 = [NSMutableArray arrayWithCapacity:50];
//[muArr3 addObject:nil]; //此处不允许将 nil 值存储
[muArr3 addObject:[NSNull null]]; //需要空值时
if([muArr3 objectAtIndex:0]==[NSNull null]){//空值比较
NSLog(@"%@",[NSNull null]);
}
}
return 0;
}
2014-06-27 11:00:31.221 Example02[931:303] -----------范围----------------
2014-06-27 11:00:31.223 Example02[931:303] 17:4
2014-06-27 11:00:31.224 Example02[931:303] 17:4
2014-06-27 11:00:31.224 Example02[931:303] 17:4
2014-06-27 11:00:31.224 Example02[931:303] -----------字符串----------------
2014-06-27 11:00:31.225 Example02[931:303] 字面量语法,5
2014-06-27 11:00:31.225 Example02[931:303] 非字面量语法 5:Hello,14
2014-06-27 11:00:31.225 Example02[931:303] 值相同
2014-06-27 11:00:31.226 Example02[931:303] 相同
2014-06-27 11:00:31.226 Example02[931:303] str2:4915003425557038704 str3:4915003425557038704
2014-06-27 11:00:31.227 Example02[931:303] compare 不区分大小写,值相同
2014-06-27 11:00:31.227 Example02[931:303] 升序
2014-06-27 11:00:31.227 Example02[931:303] 文件名包含前缀
2014-06-27 11:00:31.228 Example02[931:303] 文件名包含后缀
2014-06-27 11:00:31.228 Example02[931:303] location:9223372036854775807 lenght:0
2014-06-27 11:00:31.228 Example02[931:303] -----------可变字符串----------------
2014-06-27 11:00:31.229 Example02[931:303] Name John
2014-06-27 11:00:31.229 Example02[931:303] Nae John
2014-06-27 11:00:31.229 Example02[931:303] -----------数组----------------
2014-06-27 11:00:31.230 Example02[931:303] 数组长度3 3
2014-06-27 11:00:31.230 Example02[931:303] 0 1
2014-06-27 11:00:31.231 Example02[931:303] 1 2
2014-06-27 11:00:31.231 Example02[931:303] 2 3
2014-06-27 11:00:31.231 Example02[931:303] 2
2014-06-27 11:00:31.232 Example02[931:303] 市北区
2014-06-27 11:00:31.232 Example02[931:303] 市南区
2014-06-27 11:00:31.232 Example02[931:303] 李沧区
2014-06-27 11:00:31.233 Example02[931:303] 黄岛区
2014-06-27 11:00:31.233 Example02[931:303] 市北区:市南区:李沧区:黄岛区
2014-06-27 11:00:31.234 Example02[931:303] -----------可变数组----------------
2014-06-27 11:00:31.238 Example02[931:303] 星期一
2014-06-27 11:00:31.238 Example02[931:303] 星期三
2014-06-27 11:00:31.239 Example02[931:303] 星期四
2014-06-27 11:00:31.239 Example02[931:303] 星期一
2014-06-27 11:00:31.239 Example02[931:303] 星期三
2014-06-27 11:00:31.240 Example02[931:303] 星期四
2014-06-27 11:00:31.240 Example02[931:303] Block 星期一 0 YES
2014-06-27 11:00:31.240 Example02[931:303] Block 星期三 1 YES
2014-06-27 11:00:31.241 Example02[931:303] Block 星期四 2 YES
2014-06-27 11:00:31.241 Example02[931:303] -----------字典----------------
2014-06-27 11:00:31.242 Example02[931:303] key:1 value:青岛
2014-06-27 11:00:31.242 Example02[931:303] -----------数字----------------
2014-06-27 11:00:31.243 Example02[931:303] 88
2014-06-27 11:00:31.243 Example02[931:303] 0
2014-06-27 11:00:31.243 Example02[931:303] -----------NSValue----------------
2014-06-27 11:00:31.244 Example02[931:303] MSPoint:{1, 2} NSSize:{30, 40}
2014-06-27 11:00:31.244 Example02[931:303] MSRect{{1, 2}, {30, 40}}
2014-06-27 11:00:31.244 Example02[931:303] -----------NSRect NSPoint NSSize----------------
2014-06-27 11:00:31.245 Example02[931:303] {{0, 0}, {30, 40}}
2014-06-27 11:00:31.245 Example02[931:303] -----------NSNull----------------
2014-06-27 11:00:31.246 Example02[931:303] <null>
Program ended with exit code: 0