Article 16th: Foundation Framework exercises in OC, ocfoundation

Source: Internet
Author: User

Article 16th: Foundation Framework exercises in OC, ocfoundation

Foundation frame learning to push the blog: http://blog.csdn.net/jianxin160/article/details/47753195#array


// Fundation # import <Foundation/Foundation. h> // 14. Custom a Ball class with a color attribute (only black and white ). 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 = @ "red ";} else color1 = @ "black"; return [NSString stringWithFormat: @ "the current color is: % @", color1];} @ end ////////////////////////////////////// //////////////////////////////////////// ///////////////// // filter the numbers in str, and returns # pra Gma mark-filter the numbers in str and return 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;} // convert num to a string # pragma mark-convert num to a string NSString * castToStr (NSInteger nu M) {return [NSString stringWithFormat: @ "% ld", num];} // input an array. The returned array contains 10 elements. # pragma mark-input an array and return an array with 10 elements not contained. 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 * arg V []) {@ autoreleasepool {// 1. the following string is defined: NSString * str = @ "iphoneAndroid". Can this string be modified? If yes, output the new string after Android deletion. NSMutableString * str = [NSMutableString stringWithString: @ "iphoneAndroid"]; NSString * delstr = @ "Android"; nsange range = [str rangeOfString: delstr]; NSLog (@ "% @", [str substringToIndex: range. location]); // 2. evaluate the difference between the string "158" and "39" by decimal value and output NSInteger mins = [@ "158" integerValue]-[@ "39" integerValue] in the form of a string; NSLog (@ "% ld", mins); // 3. extract the numbers in the string "123-456-789-000" to form a new string and output NSMutableString * str1 = [NSMutableString stringWithString: @ "123-456-789-000"]; [str1 replaceOccurrencesOfString: @ "-" withString: @ "" options: NSLiteralSearch range: NSMakeRange (0, str1.length)]; NSLog (@ "% @", str1); // 4. implementation function NSString * stringWithoutNum (NSString * str); NSLog (@ "% @", stringWithoutNum (@ "abc123defg22aaAA"); // 5. NSString * castToStr (NSInteger int); function NSLog (@ "% @", castToStr (123466); // 6. define an array tens containing 10 integer elements. NSArray * tens = @ [@ 1, @ 2, @ 3, @ 4, @ 10, @ 6, @ 7, @ 8, @ 9, @ 10]; // NSLog (@ "% @", tens); // 7. implementation function: NSArray * kickOff10 (NSArray * array) NSArray * tTens = [NSArray arrayWithArray: tens]; tTens = kickOffEven (tTens); NSLog (@ "% @", tTens ); // 8. define an array to store the Student name and store the five student names. NSArray * arrName = [[NSArray alloc] init]; for (int I = 1; I <= 5; I ++) {// [mutarr addObject:]; arrName = [arrName arrayByAddingObject: [NSString stringWithFormat: @ "name % d", I];} /* You can also install 6th to initialize the array */NSLog (@ "8th outputs: % @", arrName); // 9. save the Student name array to a file. NSString * url = @ "/Users/qujie/Desktop/1.txt"; [arrName writeToFile: url atomically: YES]; // 10. Read the file and output the content. ArrName = nil; arrName = [NSArray arrayWithContentsOfFile: url]; NSLog (@ "10th outputs: % @", arrName); // 11. store student information (name, age, and gender) in a dictionary ). NSDictionary * sutInformation ={ @ "name": @ "name_xx", @ "age": @ "20", @ "sex": @ "man "}; NSLog (@ "11th outputs: % @", sutInformation); // 12. generate 3 student information and store it in an array. NSMutableArray * susuinfoarr = [NSMutableArray array]; for (int I = 0; I <3; ++ I) {NSDictionary * sutInformation ={ @ "name": [NSString stringWithFormat: @ "name _ % d", I], @ "age": @ "20", @ "sex": @ "man"}; [susuinfoarr addObject: sutInformation];} NSLog (@ "12th outputs: % @", sutInfoArr); // 13. output array. // 14. Customize a Ball class with a color attribute (only black and white ). Ball * ball = [Ball ballWithColor: BallColorBlack]; NSLog (@ "14th problem output: % @", ball); // 15. generate 20 balls randomly and place them in a collection. NSMutableSet * ballSet = [NSMutableSet setWithCapacity: 20]; for (int I = 0; I <20; I ++) {[ballSet addObject: [Ball ball];} // 16. extract 10 small balls from the set and print them out. NSMutableArray * ballMut = [NSMutableArray arrayWithCapacity: 10]; for (int I = 0; I <10; I ++) {Ball * B = [ballSet anyObject]; [ballSet removeObject: b]; [ballMut addObject: B];} NSLog (@ "16th outputs: % @", ballMut); // 17. convert May 05, 2013 to NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; formatter. dateFormat = @ "MM dd, yyyy"; NSDate * date = [formatter dateFromString: @ "July May 05, 2013" ]; NSDateFormatter * formatter2 = [[NSDateFormatter alloc] init]; formatter2.dateFormat = @ "yyyy-MM-dd"; NSString * dateString = [formatter2 stringFromDate: date]; NSLog (@ "% @ converted to % @", date, dateString); // 18. use @ "Ball" to create a Car instance ([[Ball alloc] init] cannot be used) Class class = NSClassFromString (@ "Ball "); ball * Car = [[class alloc] init]; NSLog (@ "% @", Car); // 19. compile a guess digital game.} Return 0 ;}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.