#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 *[email protected];
NSString *subject = @"数学";
//数据存储成字典
NSDictionary *[email protected]{@"name":name,
@"age":@(age),
@"score":score,
@"subject":subject};
NSLog(@"%@,年龄%@岁,科目%@,分数%@",dic[@"name"],dic[@"age"],dic[@"subject"],dic[@"score"]);
/*第2题:12,9,34,56,11存入NSArray数组,输出结果:1)倒序输出2)降序输出*/
NSArray *array=[[NSArray alloc]initWithObjects:@12,@9,@34,@56,@11,nil];
for (int i=4; i<[array count]; i--) {
NSString *element=[array objectAtIndex:i];
NSLog(@"%@",element);
}
NSLog(@"\n");
NSComparator sortBlock=^(id obj1,id obj2){
return [obj2 compare:obj1];
};
NSArray *sortArray=[array sortedArrayUsingComparator:sortBlock];
for (id str in sortArray) {
NSLog(@"%@",str);
}
/* 第3题 判断中间目录是否存在
比如 传入字符串 @"/home/qianfeng/oc.txt" 和 @"qianfeng"
返回:YES
传入字符串 @"/usr/qianfeng/oc.txt 和 @"fengqian"
返回:NO
*/
NSString *[email protected]"/home/qianfeng/oc.txt";
NSRange rang=[str rangeOfString:@"qianfeng"];
if (rang.location==NSNotFound) {
NSLog(@"NO");
}else{
NSLog(@"YES");
}
NSString *[email protected]"/usr/qianfeng/oc.txt";
NSRange rang1=[str1 rangeOfString:@"fengqian"];
if (rang1.location==NSNotFound) {
NSLog(@"NO");
}else{
NSLog(@"YES");
}
/* 第4题数组内元素按中文排序
*/
NSArray *[email protected][@"小雨",@"安安",@"小风",@"荣荣",@"张涛",@"李文荣"];
NSArray *sortedArr2=[arr2 sortedArrayUsingSelector:@selector(localizedCompare:)];
for (NSString *str in sortedArr2) {
NSLog(@"%@",str);
}
}
return 0;
}