OC Learning Notes Foundation framework NSNumber, Nsvalue and NSDate (RPM)

Source: Internet
Author: User
Tags dateformat

First, NSNumber
OC Array Class Nsarray, it can only hold OC objects, for the basic data type is powerless, but the actual programming often to the basic data such as int, float, structure stored in the OC array, how to do? The nsnumber here is useful, and it is able to package the basic data types into OC objects.
//Wrap shaping into OC objectnsnumber *num1 = [NSNumber numberwithint:];  //Wrap a floating-point shape into an OC objectnsnumber *num2 = [NSNumber numberwithdouble:0.5]; //Store the packaged data in the OC array Nsarray *array = @[ @ "Age", NUM1];   / /traversal array   for (ID obj  in array)   {  < Span style= "FONT-SIZE:16PX; Color: #0000ff; " > NSLog ( @ "%@", obj);  }  

/span>
by means of nsnumber, all basic data types can be packaged into OC objects and stored in the OC array. To restore the NSNumber wrapped object to the base number
//Restore an object of type NSNumber to the base data type int age = [Num1 intvalue];    double A = [num2 doublevalue];   
You can also use compiler attributes to quickly wrap a basic data:
//Express wrapper 120; 0.5 ; @NO; //use a quick wrapper and store it in an array < Span style= "color: #0000ff; font-size:16px; " > Nsarray *array2 = @[ @ "Hello" @ 100, @YES, @ < Span style= "color: #0000ff; font-size:16px; " >0.3, @ ' H '];

< Span style= "Font-family:verdana, Arial, Helvetica, Sans-serif; line-height:1.5; " > Two, nsvalue

 nsvalue and NSNumber are similar, specifically the former parent class. Nsvalue can wrap complex basic data types, such as structs, enumerations.

int main () { Span class= "indent" >  //OC structure  Cgpoint point = Cgpointmake (11, 12);  //package the structure into OC object  Nsvalue *val = [Nsvalue Valuewithpoint:point];   //store the wrapped OC object in the OC array  Nsarray *array = @[val];   NSLog (@ "%@", array);   return 0;}      
Third, NSDate
This class is a time-dependent, date-formatted format.
Create Date ObjectNSDate *date = [NSDate Date];    Date Formatting classNSDateFormatter *formatter = [[NSDateFormatter alloc] init];  Y year M month D Day M min S Second h (24) H (12)Formatter.dateformat =@ "Yyyy-mm-dd HH:mm:ss";Fixed writing, can't write casually  Format a date as a string object NSString *str = [Formatter stringfromdate:date];   NSLog (@ "%@", str);  //formatted parameters have a fixed meaning, can not be arbitrarily written. Above is the date formatted output, following the date in the string  //the specified date is  NSString *time = Span class= "string" >@ "2022/05/01 13:30";   //create date Format object  NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];  //formatted date  Formatter2.dateformat = @ "Yyyy/mm/dd hh:mm";   NSDate *date2 = [formatter2 datefromstring:time]; NSLog (@ "%@", date2);            

OC Learning Notes Foundation framework NSNumber, Nsvalue and NSDate (RPM)

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.