"Learn iOS Path: objective-c" block syntax. NSDate and NSDateFormatter

Source: Internet
Author: User
Tags date1

I. Block blocks syntax

Block syntax: You can define anonymous functions inside a function

Format: ^ return value type (parameter list) {function Body} where return value type can be omitted

Block Simple Exercise :

①. To find the maximum value of two numbers

      int (^) (int,int)//block type         int (^max) (int,int) = ^ (int a,int b) {//block's implementation body                  return a > B? a:b;                  };                 int value = max (4,6); printf ("%d", value);
②. Converting an integer to a string object
        typedef void (^exchange) (int);        Exchange Change  = ^ (int number) {            NSString *string = [[NSString alloc] initwithformat:@ "%d", number];            NSLog (@ "%@", string);        };        Change (5);
③. Converting an integer to a Nsmumber object
     typedef  NSNumber * (^EXC) (int);        EXC Exchange = ^ NSNumber * (int number) {            return @ (number);        };       NSNumber *number = Exchange (5); NSLog (@ "%@", number);
④. Passing in a string object, returning all uppercase objects
      typedef nsstring * (^upper) (NSString *);        UPPER upperstring = ^nsstring * (nsstring *string) {            return [string uppercasestring];        };                NSString *temp = @ "SDSDFASDF";        NSString *num = upperstring (temp);        NSLog (@ "%@", num);
⑤ implements the number of OC array elements,
     typedef nsuinteger (^count) (Nsarray *);       Count GetCount = ^nsuinteger (Nsarray *arr) {            return [arr count];        };        Nsarray *ar1 = @[@ "SADF", @ "asdf", @ "DSFG", @ "FDG"];        Nsuinteger count = GetCount (AR1);        NSLog (@ "%ld", count);
⑥. Defining a block to achieve a comparison of two integers
        typedef nscomparisonresult (^compare) (int,int);        Compare Compare = ^nscomparisonresult (int a,int b) {            //return [@ (a) Compare: @ (b)];            Return a > B? Nsordereddescending:a = = b?                                         nsorderedsame:nsorderedascending;        };        Nscomparisonresult x =  compare (5,6);        NSLog (@ "%ld", x);//-1
⑦. Implementing a comparison of two string objects
     typedef nscomparisonresult (^comparestring) (nsstring *,nsstring *);        comparestring com = ^nscomparisonresult (nsstring *str1,nsstring *str2) {            return [str1 compare:str2];        };        NSString *TEMP1 = @ "dasf";        NSString *TEMP2 = @ "das35f";        Nscomparisonresult tem  = com (TEMP1,TEMP2);        NSLog (@ "%ld", tem);
⑧.block Implementing array sorting

    Nsarray *ar = @[@ "ads", @ "DD", @ "FF", @ "hh"];    Nsarray *ar1 = [ar sortedarrayusingcomparator:^ (id num1,id num2) {return-[nsstring        *] NUM1 Compare: (NSString *) num 2];    NSLog (@ "%@", AR1);

2.block relationship to local variables and global variables

    __block int num = 1; __block modified can modify the local variable    void (^addnum) () = ^void () {      int c = num *,//block internal can access the external local variable        num++;//internal access to local variables, but You cannot modify a local variable. If modified with __block, you can modify        printf ("%d", c);    };    Addnum ();

Two. NSDate and NSDateFormatter Common applications

①. Getting the time of the current 0 time zone

   NSDate *date = [NSDate date];   NSLog (@ "%@", date);
②. Get the time tomorrow

   NSDate *tomorrow = [nsdate datewithtimeintervalsincenow:60 * *];   NSLog (@ "%@", Tomorrow);
③ get yesterday's current time
  NSDate *yesterday = [nsdate datewithtimeintervalsincenow:-60 * *];  NSLog (@ "%@", yesterday)
④. By 1970/1/1, calculated at this time 2014/12/29
   NSDate *now = [nsdate dateWithTimeIntervalSince1970:                          (One * 366 + * 365) * * 3600 + 362 * * 3600];   NSLog (@ "%@", now);
⑤. Calculate the time by 2001/1/1//default 2001/1/1
  NSDate *now1 = [nsdate datewithtimeintervalsincereferencedate:                                (3 * 366 + one * 365) * * 3600-3 * + * 3600];  NSLog (@ "%@", Now1);
⑥. Get two time intervals

   Nstimeinterval interval = [date1 timeintervalsincedate:date2];   Double type nstimeinterval   NSLog (@ "%.1f", interval);
⑦. Compare dates sooner or later, return morning and evening dates
NSDate *dt1 = [Date1 earlierdate:date2]; NSDate *DT2 = [Date1 laterdate:date2]; NSLog (@ "%@", DT1); NSLog (@ "%@", DT2);
⑧. Determine if the dates are the same
  BOOL isequal = [Date1 isequaltodate:date2];        if (isequal) {            NSLog (@ "equals"),        } else {            NSLog (@ "Not equal");        }
Three. Date format class NSDateFormatter
①. Converts the NSDate object to a date string in the specified date format.
-(NSString *) Stringfromdate: (NSDate *) date;
The code is as follows:

   NSDate *date = [NSDate date];   Gets the current time   nsdateformatter *formatter = [[NSDateFormatter alloc] init];   Set the format.  HH---Represents a 24-hour system, HH---represents 12-hour AA pm/am eee Tuesday  [Formatter setdateformat:@ "Yyyy-mm-dd HH:mm:ss eee AA"];  /[formatter setdateformat:@ "MM month DD day EEE aahh:mm"];   Conversion format NSString *string =[formatter stringfromdate:date]; NSLog (@ "%@", string);

2. Convert date string to NSDate object in the specified date format
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];    NSString *string1 = @ "May 01, 2014 10:53 18 sec";    NSLog (@ "%@", string1);    For NSDate objects, is always the time of the 0-time zone   //hh---represents the 24-hour system, HH---represents 12-hour system [formatter setdateformat:@ "yyyy years mm month DD Day HH point mm minutes ss seconds"];  NSDate *date1 = [Formatter datefromstring:string1]; NSLog (@ "%@", date1);

"Learn iOS Path: objective-c" block syntax. NSDate and NSDateFormatter

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.