Dark Horse programmer--foundation Framework--class other commonly used classes: Nsnumber,nsdate

Source: Internet
Author: User

------iOS training look forward to communicating with you! -------

Other common classes of foundation: nsnumber,nsdate

First, NSNumber

This class is primarily used to encapsulate basic types, The collection in OC is not allowed to be stored in the basic type, so the NSNumber class is born, the basic type needs to be encapsulated, and then stored in.

From this point we can see that the nsnumber is very important, the latter will be used frequently.

1, Package and package

    //Create NSNumber//Sealing PackageNSNumber *intnumber = [NSNumber numberwithint:3]; NSNumber*floatnumber = [NSNumber numberwithfloat:9.8f]; Nsarray*array =@[intnumber,floatnumber]; // optimization SyntaxNSNumber *intnumbers = @ A; //Unpacking    intValue =[Intnumber Intvalue]; floatValues =[Floatnumber Floatvalue]; NSString*STR = [Intnumber stringvalue];

There are basic types of methods in the NSNumber class that can be manipulated, but it is worth mentioning that:

    NSString *str = [intnumber stringvalue];      NSLog (@ "%@", str);  

We see that intnumber is wrapped in int type, but we can unpack using the StringValue method, which is equivalent to converting the int type to the NSString type.

3, the structure of the type of automatic packaging and reconciliation package

    The structure is deposited into the array, and the struct is encapsulated    using Nsvalue Nsvalue is the parent class        of the NSNumber package      Nsrange rang = {1,3};       *v = [Nsvalue Valuewithrange:rang];             unpacking rang = [v rangevalue       ];  

In OC There is also a struct type, he is also cannot be directly deposited into the collection class, the package for him when the Nsvalue class, not the NSNumber class.

4. Automatic package and package for custom structure type

    // to package a custom struct body    structwxpoint{floatx; floaty;            }; structWxpoint p = { -, -}; // The first argument is the variable address of the struct, and the second argument is the typeNsvalue *v1 = [Nsvalue value:&p withobjctype: @encode (structWxpoint)]; // Unpacking    structwxpoint p1; [V1 getValue:&p1];

For a custom struct type, we need to use the Value:withobjctype: method to do this when the packet is marshaled.

It is necessary to pass in @encode (struct wxpoint), which is equivalent to passing the struct type to the past.

5. Encapsulation of NULL values

// There is no way to store nil  for an empty object in an array. NULL ];   NULL ];   *nullarray = @[n1,n2];  NSLog (@ "nullarray =%@", Nullarray);

Second, NSDate

This class is the class of the operation date in OC.

1. Create a date ( default is the current time)

Date  created NSDate *date = [NSDate Date]; // represents the current point  in time NSDate *date1 = [[NSDate alloc] init];  NSLog (@ "%@", date);

2, increase or decrease the time point

    Date1 = [NSDate datewithtimeintervalsincenow:+];  Unit is the second, at the current time plus 1000s, if it is minus a time, the direct use  of negative values can be    NSLog (@ "%@", date) ;

3, from 1970 onwards to the present time stamp

  NSDate *date2 = [nsdate dateWithTimeIntervalSince1970:+];

4. Create a date and then get his timestamp

Create a date NSDate *now = [NSDate date];   = [now timeIntervalSince1970];   = [now Timeintervalsincenow];

5. Comparison of dates

 //  by invoking the compare of the Date object, or by the difference between two dates   nscomparisonresult result = [date compare:date1];       if  (Result == nsorderedascending) {  //  greater than } if  (Result == nsordereddescending) {  //  less than } { //  equals } 


6. Date formatting (date converted to string)

    date formatting      NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];      [Dateformatter Setdateformat: @" YYYY-MM-DD HH:mm:ss " ];       *str = [Dateformatter stringfromdate:date];      NSLog (@ "%@", str);  

7. Convert a string into a date

 
Convert a string into a date  object @" December 14, 2013 16:31:08 " ;  [Dateformatter Setdateformat: @" yyyy mm month DD Day HH:MM:SS " ];  

Dark Horse programmer--foundation Framework--class other commonly used classes: Nsnumber,nsdate

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.