欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > 内容正文

第十六篇:OC中的Foundation框架练习,ocfoundation

来源: 开发者 投稿于  被查看 36306 次 评论:162

第十六篇:OC中的Foundation框架练习,ocfoundation


Foundation框学习推存博客:http://blog.csdn.net/jianxin160/article/details/47753195#array


//  Fundation

#import <Foundation/Foundation.h>

// 14. 自定义一个Ball类,有一个颜色属性(只有黑色和白色)。
typedef enum _BallColor{
    BallColorRed,
    BallColorBlack,
}BallColor;

@interface Ball : NSObject

@property (nonatomic,assign)BallColor color;

+ (Ball *)ball;
+ (Ball *)ballWithColor:(BallColor) color;

@end

@implementation Ball

+ (Ball *)ball{
    Ball * bb = [[Ball alloc]init];
    NSInteger rand = arc4random()%2;
    if (BallColorBlack == rand) {
        bb.color = BallColorBlack;
    }
    else
        bb.color = BallColorRed ;
    return bb;
}
+ (Ball *)ballWithColor:(BallColor)color{
    Ball * p = [self ball];
    p.color = color;
    return p;
    
}
- (NSString *)description{
    NSString * color1 ;
    color1 = nil ;
    if (BallColorRed == _color) {
        color1 = @"红色";
    }
    else
        color1 = @"黑色";
    return [NSString stringWithFormat:@"当前求的色是:%@",color1];
}
@end

//////////////////////////////////////////////////////////////////////////////////////////////////

// 过滤str中的数字,并返回
#pragma mark - 过滤str中的数字,并返回
NSString * stringWithoutNum(NSString *str){
    NSMutableString * mutStr = [NSMutableString stringWithString:str];
    
    for (int i = 0 ; i<10; i++) {
        [mutStr replaceOccurrencesOfString:[NSString stringWithFormat:@"%d",i]
                                withString:@""
                                   options:NSLiteralSearch
                                     range:NSMakeRange(0, mutStr.length)];
    }
    return mutStr;
}

// 将num转换成字符串
#pragma mark - 将num转换成字符串
NSString *castToStr(NSInteger num){
    return [NSString stringWithFormat:@"%ld",num];
}

// 传入一个数组,返回一个数组其中元素不能包含10.
#pragma mark - 传入一个数组,返回一个数组其中元素不能包含10.
NSArray *kickOffEven(NSArray *array){
    NSMutableArray * mutArr = [NSMutableArray arrayWithArray:array];
    for (int i = (int)mutArr.count-1; i>=0; --i) {
        if(10 == [mutArr[i] integerValue])
            [mutArr removeObjectAtIndex:(NSUInteger)i ];
    }
    return mutArr ;
}

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        // 1. 现有如下定义的字符串: NSString * str=@“iphoneAndroid”,能不能对该字符串进行修改,如果能,请输出删除Android后的新字符串。
        NSMutableString * str = [NSMutableString stringWithString:@"iphoneAndroid"];
        NSString * delstr = @"Android";
        NSRange  range = [str rangeOfString:delstr];
        NSLog(@"%@",[str substringToIndex:range.location]);
        
        
        // 2. 求字符串“158”和“39”按十进制数值做差后的结果以字符串形式输出
        NSInteger mins = [@"158" integerValue] - [@"39" integerValue];
        NSLog(@"%ld",mins);
        
        
        // 3. 取出符串“123-456-789-000”中的数字部分,组成一个新的字符串输出
        NSMutableString * str1 = [NSMutableString stringWithString:@"123-456-789-000"];
        [str1 replaceOccurrencesOfString:@"-" withString:@"" options:NSLiteralSearch range:NSMakeRange(0, str1.length)];
        NSLog(@"%@",str1);
        
        
        // 4.实现函数 NSString * stringWithoutNum(NSString *str);
        NSLog(@"%@",stringWithoutNum(@"abc123defg22aaAA"));
        
        
        // 5. 完成NSString *castToStr(NSInteger int);函数
        NSLog(@"%@",castToStr(123466));
        
        
        // 6. 定义一个含有10个整数元素的数组tens。
        NSArray * tens = @[@1 , @2 , @3 , @4 , @10 , @6 , @7 , @8 , @9 , @10];
        //NSLog(@"%@",tens);
        
        
        // 7. 实现函数:NSArray *kickOff10(NSArray *array)
        NSArray * tTens = [NSArray arrayWithArray:tens];
        tTens = kickOffEven(tTens);
        NSLog(@"%@",tTens);
        
        
        // 8. 定义一个数组用来存储学生的姓名,存入5个学生姓名。
        NSArray * arrName = [[NSArray alloc]init];
        for (int i = 1; i <= 5; i++) {
            //[mutarr addObject:];
            arrName = [arrName arrayByAddingObject:[NSString stringWithFormat:@"name%d",i]];
        }
           /* 也可安第 6 问对数组初始化*/
        NSLog(@"第8个输出:%@",arrName);
        
        
        // 9. 将学生姓名数组存入文件保存。
        NSString * url = @"/Users/qujie/Desktop/1.txt";
        [arrName writeToFile:url atomically:YES];
        
        
        // 10.读取文件,并将内容输出。
        arrName = nil ;
        arrName = [NSArray arrayWithContentsOfFile:url];
        NSLog(@"第10个输出:%@",arrName);
        
        
        // 11. 在一个字典中存储学生信息(姓名,年龄,性别)。
        NSDictionary * sutInformation = @{ @"name": @"name_xx",
                                           @"age" : @"20",
                                           @"sex" : @"man"
                                           };
        NSLog(@"第11个输出:%@",sutInformation);
        
        
        // 12. 生成3个学生信息,存入一个数组中。
        NSMutableArray * sutInfoArr = [NSMutableArray array];
        for (int i = 0; i < 3 ; ++i) {
            NSDictionary * sutInformation = @{ @"name": [NSString stringWithFormat:@"name_%d",i],
                                               @"age" : @"20",
                                               @"sex" : @"man"
                                            };
            [sutInfoArr addObject:sutInformation];
        }
        NSLog(@"第12个输出:%@",sutInfoArr);
        
        // 13. 输出数组。
        
        
        // 14. 自定义一个Ball类,有一个颜色属性(只有黑色和白色)。
        Ball * ball = [Ball ballWithColor:BallColorBlack];
        NSLog(@"第14个问题输出:%@",ball);
        
        
        // 15. 随机生成20个小球,放入一个集合中。
        NSMutableSet * ballSet = [NSMutableSet setWithCapacity:20];
        for (int i=0; i < 20 ; i++) {
            [ballSet addObject:[Ball ball]];
        }
        
        
        // 16. 从集合中取出10个小球,并打印出来。
        NSMutableArray * ballMut = [NSMutableArray arrayWithCapacity:10];
        for (int i = 0 ; i < 10 ; i++) {
            Ball * b = [ballSet anyObject];
            [ballSet removeObject:b];
            [ballMut addObject:b];
        }
        NSLog(@"第16个输出:%@",ballMut);
        
        
        // 17. 将2013年05月05日转换为2013-05-05
        NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
        formatter.dateFormat = @"yyyy年MM月dd日";
        NSDate * date = [formatter dateFromString:@"2013年05月05日"];
        
        NSDateFormatter * formatter2 = [[NSDateFormatter alloc]init];
        formatter2.dateFormat = @"yyyy-MM-dd";
        NSString * dateString = [formatter2 stringFromDate:date];
        NSLog(@"%@ 转换为 %@",date,dateString);
        
        
        // 18. 通过@“Ball”创建一个Car的实例(不可使用 [[Ball alloc] init] 创建)
        Class class = NSClassFromString(@"Ball");
        Ball * Car = [[class alloc]init];
        NSLog(@"%@",Car);
        
        // 19. 编写一个猜数字游戏。
    }
    return 0;
}





版权声明:本文为博主原创文章,未经博主允许不得转载。

相关频道:

用户评论