标签:
//
// main.m
// 10.1.2
//
// Created by fwzx11 on 16/1/10.
// Copyright (c) 2016年 Blue Mobi. All rights reserved.
//
#define NSLog(FORMAT, ...) fprintf(stderr,"%s\n",[[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#import <Foundation/Foundation.h>
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 = @"数学";
NSString *name = @"张三";
NSInteger age = 22;
NSNumber *score=@88.7;
NSString *subject = @"数学";
NSDictionary *dic=@{@"name":name,
@"age":@(age),
@"score":score,
@"subject":subject};
NSString *str=[NSString stringWithFormat:@"%@,年龄%@岁,科目%@,分数%@",dic[@"name"],dic[@"age"],dic[@"subject"],dic[@"score"]];
NSLog(@"%@",str);
}
NSNumber *a=@12;
NSNumber *b=@9;
NSNumber *c=@34;
NSNumber *d=@56;
NSNumber *e=@11;
NSArray *brr=[NSArray arrayWithObjects:a,b,c,d,e, nil];
NSLog(@"%@",brr);
NSComparator sort1=^(id obj1,id obj2)
{ //传入两个数组中的元素
NSComparisonResult result=[obj2 compare:obj1];
return result; //根据自己定义返回一定的值
};
NSArray *sort2=[brr sortedArrayUsingComparator:sort1]; //根据返回值排序
for(id str2 in sort2)
{
NSLog(@"%@",str2);
}
NSLog(@"\n");
NSArray *sort3=[brr sortedArrayUsingSelector:@selector(class)]; //根据返回值排序
for(id str2 in sort3)
{
NSLog(@"%@",str2);
}
//第2题:12,9,34,56,11存入NSArray数组,输出结果:1)倒序输出2)降序输出
NSArray *arr1=@[@12,@9,@34,@56,@11];
for (NSInteger i=[arr1 count]-1;i>=0;i--){
NSLog(@"%@",arr1[i]);
}
NSComparator comp=^(id obj1,id obj2){
NSComparisonResult result=[obj2 compare: obj1];
return result;
};
NSArray *sortedArr=[arr1 sortedArrayUsingComparator:comp];
for (id i in sortedArr){
NSLog(@"%@",i);
}
/* 第3题 判断中间目录是否存在
比如 传入字符串 @"/home/qianfeng/oc.txt" 和 @"qianfeng"
返回:YES
传入字符串 @"/usr/qianfeng/oc.txt 和 @"fengqian"
返回:NO
*/
NSString *str=@"/home/qianfeng/oc.txt";
NSRange rang1=[str rangeOfString:@"qianfeng"];
if(rang1.location==NSNotFound){
NSLog(@"not found");
}
else{
NSLog(@"found at loacation:%ld",rang1.location);
}
NSRange rang2=[str rangeOfString:@"fengqian"];
if(rang2.location==NSNotFound){
NSLog(@"not found");
}
else{
NSLog(@"found at loacation:%ld",rang2.location);
}
/* 第4题数组内元素按中文排序
*/
NSArray *arr=@[@"小雨",@"安安",@"小风",@"荣荣",@"张涛",@"李文荣"];
NSArray *sortedArr2=[arr sortedArrayUsingSelector:@selector(localizedCompare:)];
for (NSString *str in sortedArr2) {
NSLog(@"%@",str);
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/wuduo/p/5120129.html