练习题3

#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 = @"数学";
        NSDictionary *student=@{@"name":name,
                                @"age":@(age),
                                @"score":score,
                                @"subject":subject};
        NSString *stu=[NSString stringWithFormat:@"%@,年龄%@岁,科目%@,分数%@",student[@"name"],student[@"age"],student[@"subject"],student[@"score"]];
        NSLog(@"%@",stu);
        
        //第2题:12,9,34,56,11存入NSArray数组,输出结果:1)倒序输出2)降序输出
        
        NSArray *brr=[[NSArray alloc]initWithObjects:@12,@9,@34,@56,@11, nil];
        NSArray *newbrr1=[brr sortedArrayUsingSelector:@selector(class)];
        NSArray *newbrr2=[[brr reverseObjectEnumerator]allObjects];
        NSLog(@"倒序输出:");
        for (id str in newbrr1) {
            NSLog(@"%@",str);
        }
        NSLog(@"倒序输出:");
        for (id str in newbrr2) {
            NSLog(@"%@",str);
        }
        NSArray *newbrr3=[brr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
            return [obj2 compare:obj1];
        }];
        NSLog(@"降序输出");
        for (id str in newbrr3) {
            NSLog(@"%@",str);
        }
        
         
         /* 第3题 判断中间目录是否存在
         比如 传入字符串 @"/home/qianfeng/oc.txt" 和 @"qianfeng"
         返回:YES
         传入字符串 @"/usr/qianfeng/oc.txt 和 @"fengqian"
         返回:NO
         */
        NSString *str1=@"/home/qianfeng/oc.txt";
        NSString *str2=@"/usr/qianfeng/oc.txt";
        NSRange range1=[str1 rangeOfString:@"qianfeng"];
        NSRange range2=[str2 rangeOfString:@"fengqian"];
        if (range1.location!=NSNotFound) {
            NSLog(@"YES");
        }
        else{
            NSLog(@"NO");
        }
        if (range2.location!=NSNotFound) {
            NSLog(@"YES");
        }
        else
        {
            NSLog(@"NO");
        }
        /* 第4题数组内元素按中文排序
         */
        NSArray *arr=@[@"小雨",@"安安",@"小风",@"荣荣",@"张涛",@"李文荣"];
        NSArray *newarr=[arr sortedArrayUsingSelector:@selector(localizedCompare:)];
        for (NSString *str in newarr) {
            NSLog(@"%@",str);
        }
    }
    return 0;
}

 

posted @ 2016-01-11 13:08  纽伦堡幅度  阅读(125)  评论(0编辑  收藏  举报