Objective-c:foundation Framework-Common class-nsvalue

Source: Internet
Author: User
Tags date1

NSNumber is a subclass of Nsvalue, the former can only wrap numbers, the latter may wrap any value. Nsarray, Nsdictionary can only store OC objects and cannot store structures. Therefore, if you want to put the structure in Nsarray, nsdictionary, can curve the salvation, the structure is packaged into OC objects, and then put in Nsarray, Nsdictionary and other sets of classes.

voidvalue () {Cgpoint point= Cgpointmake (Ten,Ten); //wrapping a struct variable as an objectNsvalue *value =[Nsvalue Valuewithpoint:point]; Nsmutablearray*array =[Nsmutablearray array]; //Add Value[Array addobject:value]; //Take out the value that was put inNsvalue *value1 =[Array lastobject]; Cgpoint point1=[value1 Pointvalue]; BOOL result=Cgpointequaltopoint (point1, point); NSLog (@"result=%i", result);} typedefstruct {    intYear ; intmonth; intDay ;} Date;voidvalue2 () {Date Date= { -,4,7}; //void * represents any pointer//This is going to be the address of the struct &date//generate a corresponding type description string based on struct type    Char*type =@encode (Date); Nsvalue*value = [Nsvalue value:&date Withobjctype:type]; //To define a struct-body variableDate date1; //Remove the packaged structure[Value getvalue:&Date1]; //[value objctype]; Remove type description stringNSLog (@"year=%i, month=%i, day=%i", Date1.year, Date1.month, date1.day);}

Objective-c:foundation Framework-Common class-nsvalue

Related Article

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.