第十六篇:OC中的Foundation框架練習

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;
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章