Objective-C-nsvalue detailed description of struct

Source: Internet
Author: User

Struct is often used in C/C ++ development to help us encapsulate basic data types. In objective-C, struct can also be used to encapsulate data types. At the same time, cocoa touch also provides an nsvalue to help us better use struct in development.

We can use nsvalue to help us encapsulate some simple data structures. For example, we have defined a simple struct type.

 

 

Objective-C code
  1. Typedef struct {
  2. Int ID,
  3. Float height,
  4. Unsigned char flag
  5. } Myteststruct;

 

In this case, we can use the myteststruct struct to easily encapsulate data. For example

 

 

Objective-C code
  1. Myteststruct;
  2. Myteststruct. ID = 1;
  3. My teststruct. Height = 23.0;
  4. Myteststruct. Flag = 'a ';
  5. Nsvalue * value = [nsvalue valuewithbytes: & myteststruct objctype: @ encode (myteststruct)];

 

 

You can use the following method to retrieve the data in * value:

 

 

Objective-C code
  1. Myteststruct theteststruct;
  2. [Valeu getvalue: & theteststruct];

 

 

Then you can perform theteststruct operations to obtain the data.

 

For the nsvalue class, we can also use it when we display points or other geometric figures on the iPhone. For example, if we want to obtain the user's click track on the screen, we first think of using the cgpoint struct to save the data of each click. In this case, we can use + valuewithcgpoint in the nsvalue category to help us provide data for the CG framework.

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.