iOS trivia is for reference only

Source: Internet
Author: User

//Calculate the number of words in a string
1
+ (int) CountWords: (nsstring*) s2 { 3 intI,n=[s length],l=0, a=0, b=0; 4 Unichar C; 5 for(i=0; i<n;i++){ 6C=[s characteratindex:i]; 7 if(Isblank (c))8 { 9b++; Ten}Else if(Isascii (c)) One { Aa++; -}Else - { thel++; - } - } - if(a==0&& l==0) + { - return 0; + } A returnL + (int) Ceilf (float) (A+B)/2.0); at}
Get system language
+ (NSString *) getlocallanguage { *language = [[[Nsuserdefaults Standarduserdefaults] Objectforkey: @" applelanguages "] Objectatindex:0]; return language;
Get the timestamp of this month, this week, the first day of the quarter
+ (unsigned long Long) Getfirstdayofweek: (unsigned long long) timestamp{nsdate *now = [NSDate DateWithTimeIntervalSince1 970:timestamp]; Nscalendar *cal = [Nscalendar Currentcalendar]; Nsdatecomponents *comps = [Cal components:nsyearcalendarunit| Nsmonthcalendarunit| Nsweekcalendarunit | Nsweekdaycalendarunit | Nsweekdayordinalcalendarunit Fromdate:now]; if (Comps.weekday <2) {comps.week = comps.week-1; } comps.weekday = 2; NSDate *firstday = [cal Datefromcomponents:comps]; return [FirstDay timeIntervalSince1970];} + (unsigned long Long) Getfirstdayofquarter: (unsigned long long) timestamp{nsdate *now = [NSDate datewithtimeintervalsin Ce1970:timestamp]; Nscalendar *cal = [Nscalendar Currentcalendar]; Nsdatecomponents *comps = [cal Components:nsyearcalendarunit | Nsmonthcalendarunit | Nsdaycalendarunit Fromdate:now]; if (comps. Month <=3) {comps.month = 1; } else if (comps.month<=6) {comps.month = 4; } else if (comps.month<=9) {comps.month = 7; } else if (comps.month<=12) {comps.month = 10; } comps.day = 1; NSDate *firstday = [cal Datefromcomponents:comps]; return [FirstDay timeintervalsince1970]*1000;} + (unsigned long Long) Getfirstdayofmonth: (unsigned long long) timestamp{nsdate *now = [NSDate datewithtimeintervalsince 1970:TIMESTAMP/1000]; Nscalendar *cal = [Nscalendar Currentcalendar]; Nsdatecomponents *comps = [cal Components:nsyearcalendarunit | Nsmonthcalendarunit | Nsdaycalendarunit Fromdate:now]; Comps.day = 1; NSDate *firstday = [cal Datefromcomponents:comps]; return [FirstDay timeintervalsince1970]*1000;}

//Monitor network status
1
- (void) reachabilitychanged: (Nsnotification *) Note {2reachability* Curreach = [NoteObject];3Nsparameterassert ([Curreach iskindofclass: [reachabilityclass]]);4NetworkStatus status =[Curreach currentreachabilitystatus];5 6 if(Status = =notreachable)7 {8 9 }Ten Else if(Status = =Kreachableviawifi) One { A - } - Else if(Status = =Kreachableviawwan) the { - - } - + } - +- (void) Setnetworknotification A { at [[Nsnotificationcenter Defaultcenter] addobserver:self - selector: @selector (reachabilitychanged:) - name:kreachabilitychangednotification - Object: nil]; -_hostreach = [[reachability reachabilitywithhostname:@"http://www.baidu.com"] retain]; - [_hostreach Startnotifier]; in}
//16 Color Turn Uicolor
1
+ (Uicolor *) Colorwithhex: (NSString *) Hex {2 //Remove ' # ' and ' 0x '3 if([Hex Hasprefix:@"#"]) {4Hex = [Hex Substringfromindex:1];5}Else if([Hex Hasprefix:@"0x"]) {6Hex = [Hex Substringfromindex:2];7 }8 9 //Invalid if not 3, 6, or 8 charactersTenNsuinteger length =[hex length]; One if(Length! =3&& Length! =6&& Length! =8) { A returnNil; - } - the //Make the string 8 characters long for easier parsing - if(Length = =3) { -NSString *r = [Hex Substringwithrange:nsmakerange (0,1)]; -NSString *g = [Hex Substringwithrange:nsmakerange (1,1)]; +NSString *b = [Hex Substringwithrange:nsmakerange (2,1)]; -Hex = [NSString stringWithFormat:@"%@%@%@%@%@% @ff", + R, R, G, G, B, b]; A}Else if(Length = =6) { atHex = [Hex stringbyappendingstring:@"FF"]; - } - -CGFloat red = [[Hex Substringwithrange:nsmakerange (0,2)] [_hexvalue]/255.0f; -CGFloat green = [[Hex Substringwithrange:nsmakerange (2,2)] [_hexvalue]/255.0f; -CGFloat blue = [[Hex Substringwithrange:nsmakerange (4,2)] [_hexvalue]/255.0f; inCGFloat alpha = [[Hex Substringwithrange:nsmakerange (6,2)] [_hexvalue]/255.0f; - to return[Uicolor colorwithred:red green:green blue:blue Alpha:alpha]; +}

iOS trivia is for reference only

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.