中文排序
#import <Foundation/Foundation.h> #define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]) int main(int argc, const char * argv[]) { @autoreleasepool { /* 第1题 请将如下数据存储成字典,并将字典内容以字符串形式输出,输出:"XXX,年龄XXX岁,科目XXX,分数XXX"(将XXX替换为相应的数据)。 */ NSString *name = @"张三"; NSInteger age = 22; NSNumber *score=@88.7; NSString *subject = @"数学"; NSDictionary *dic=@{@"name":name, @"age":@(age), @"score":score, @"subject":subject }; NSString *str1=[NSString stringWithFormat:@"%@ 年龄%@ 科目%@ 分数%@",dic[@"name"],dic[@"age"],dic[@"score"],dic[@"subject"]]; NSLog(@"%@",str1); NSLog(@"%@ 年龄%@ 科目%@ 分数%@",dic[@"name"],dic[@"age"],dic[@"score"],dic[@"subject"]); /*第2题:12,9,34,56,11存入NSArray数组,输出结果:1)倒序输出2)降序输出*/ NSMutableArray *Arr=[@[@12,@9,@34,@56,@11]mutableCopy]; NSArray *Arr1=[Arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { return [obj2 compare:obj1]; }]; NSLog(@"降序输出"); for(id ste in Arr1){ NSLog(@"%@",ste); } NSLog(@"倒序"); for (int i = 0; i<Arr.count/2; i++) { [Arr exchangeObjectAtIndex:i withObjectAtIndex:Arr.count-1-i]; } NSLog(@"%@",Arr); //倒序法二 NSArray *arr3=@[@12,@9,@34,@56,@11]; for (NSInteger i=[arr3 count]-1;i>=0;i--){ NSLog(@"%@",arr3[i]); } /* 第3题 判断中间目录是否存在 比如 传入字符串 @"/home/qianfeng/oc.txt" 和 @"qianfeng" 返回:YES 传入字符串 @"/usr/qianfeng/oc.txt 和 @"fengqian" 返回:NO */ NSString *string=@"/home/qianfeng/oc.txt"; NSRange ran=[string rangeOfString:@"qianfeng"]; if (ran.location==NSNotFound) { NSLog(@"NO"); }else{NSLog(@"YES");} NSString *str=@"/usr/qianfeng/oc.txt"; NSRange rang1=[str rangeOfString:@"fengqian"]; if(rang1.location==NSNotFound){ NSLog(@"NO"); } else{ NSLog(@"YES"); } // 第4题数组内元素按中文排序 // NSArray *arr=@[@"小雨",@"安安",@"小风",@"荣荣",@"张涛",@"李文荣"]; // NSArray *arrr=[arr sortedArrayUsingSelector:@selector(localizedStandardCompare:)]; // for(id ser in arrr){ // NSLog(@"%@",ser); // } NSArray *arr5=@[@"小雨",@"安安",@"小风",@"荣荣",@"张涛",@"李文荣"]; NSArray *sortArr = [arr5 sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { return [obj1 localizedStandardCompare:obj2]; }]; for(id obj in sortArr) { NSLog(@"%@",obj); } } return 0; }