A detailed explanation of the nsvalue in Objective-c

Source: Internet
Author: User

A Nsvalue object is a simple container for storing a C or objective-c data. It can save any type of data, such as Int,float,char, of course, it can refer to pointers, structures, and object IDs. The goal of the Nsvalue class is to allow data structures of these types to be added to the collection , such as those that require their elements to be objects, such as Nsarray or Nsset instances. It is important to note that the Nsvalue object is always non-enumerable.
We can use Nsvalue to assist us in the encapsulation of some simple data structures. For example, we define a simple struct type typedef struct
{
int id;

float height;

unsigned char flag;
}test;

At this point, we can use the MYTESTSTRUCT structure to easily encapsulate the data. Such as

Test test;

test.id=1;

test.height=23.0;

test.flag= ' A ';

Nsvalue *value=[nsvalue valuewithbytes:&test objctype: @encode (Test)];//Package the structure
NSLog (@ "id=%d,height=%f,flag=%c", Test.id,test.height,test.flag);

When we want to remove the data from the *value, we can use the following methods:


Test test2;//declares Test2, in order to get the value of Test1
[Value getvalue:&test2];//same-type assignment
NSLog (@ "id=%d,height=%f,flag=%c", Test2.id,test2.height,test2.flag);

The data can then be obtained from the operation of the theteststruct.

Another place to use the Nsvalue class is when we're on the iphone display or some other geometry. For example, we want to get the user on the screen of the click Track, the first thing we think of is to use CGPOINT this structure to save every click of the data. At this point, we can use +valuewithcgpoint in the Nsvalue Category: To help us provide data for the CG framework.

Nsvalue wrapper object pointer, CGRect structure, etc.

@interface Nsvalue (nsvalueuigeometryextensions)

+ (Nsvalue *) Valuewithpointer: (const void *) pointer;//Save object pointer

+ (Nsvalue *) Valuewithcgpoint: (cgpoint) point;//Save Cgpoint structure
+ (Nsvalue *) Valuewithcgsize: (cgsize) size;//Save cgsize structure
+ (Nsvalue *) Valuewithcgrect: (cgrect) rect;//Save CGRect structure
+ (Nsvalue *) Valuewithcgaffinetransform: (cgaffinetransform) transform;
+ (Nsvalue *) Valuewithuiedgeinsets: (uiedgeinsets) insets;

-(void *) Pointervalue;
-(Cgpoint) Cgpointvalue;
-(cgsize) Cgsizevalue;
-(CGRect) Cgrectvalue;
-(Cgaffinetransform) Cgaffinetransformvalue;
-(uiedgeinsets) Uiedgeinsetsvalue;

@end

//
Nsarrayandselectorviewcontroller.m
Nsarrayandselector
//
Created by LIXF on 09-10-15.
Copyright LIXF 2009. All rights reserved.
//

#import "NSArrayAndSelectorViewController.h"

@implementation Nsarrayandselectorviewcontroller
@synthesize Selectorarray, Rectarray, Transformarray;


Implement Viewdidload to does additional setup after loading the view, typically from a nib.
-(void) Viewdidload {
[Super Viewdidload];

Nsvalue Packing Hands
nsvalue* start = [Nsvalue valuewithpointer: @selector (start:)];
nsvalue* help = [Nsvalue valuewithpointer: @selector (Help:)];

Nsarray Adding a nsvalue-wrapped pointer
Nsarray *array = [[Nsarray alloc] Initwithobjects:start, help, nil];
Self. Selectorarray = Array;
[Array release];


CGRect rect1 = {10, 10, 200, 200};
CGRect Rect2 = {20, 20, 100, 100};

Nsvalue Packaging CGRect Structure
Nsvalue *arect1 = [Nsvalue Valuewithcgrect:rect1];
Nsvalue *arect2 = [Nsvalue valuewithcgrect:rect2];
Nsarray Adding a nsvalue-wrapped pointer
Nsarray *rearray = [[Nsarray alloc] Initwithobjects:arect1, aRect2, nil];
Self. Rectarray = Rearray;
[Rearray release];

Cgaffinetransform
Cgaffinetransform Transform1 = cgaffinetransformmakerotation (40);
Cgaffinetransform Transform2 = cgaffinetransformmakerotation (-40);
Nsvalue *atransform1 = [Nsvalue Valuewithcgaffinetransform:transform1];
Nsvalue *atransform2 = [Nsvalue Valuewithcgaffinetransform:transform2];
Nsarray *transarray = [[Nsarray alloc] Initwithobjects:atransform1, ATransform2, nil];
Self. Transformarray = Transarray;
[Transarray release];
[TestView Setframe:[[rectarray objectatindex:0] cgrectvalue];
[TestView Settransform:[[transformarray objectatindex:0] cgaffinetransformvalue];
[Self Performselector:[[selectorarray objectatindex:0] pointervalue] Withobject:nil afterdelay:0.5];
}

-(void) Start: (ID) sender{
NSLog (@ "start");
[TestView Setframe:[[rectarray objectatindex:1] cgrectvalue];
[TestView Settransform:[[transformarray objectatindex:1] cgaffinetransformvalue];
[Self Performselector:[[selectorarray objectatindex:1] pointervalue] Withobject:nil afterdelay:0.5];
}

-(void) help: (ID) sender{
NSLog (@ "help");
[TestView Setframe:[[rectarray objectatindex:0] cgrectvalue];
[TestView Settransform:[[transformarray objectatindex:0] cgaffinetransformvalue];

A detailed explanation of the nsvalue in Objective-c

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.