We often use structs to help us simply encapsulate basic data types in C/D + + development, and we can use structs to encapsulate data types in objective-c. At the same time, Cocoa Touch provides a nsvalue to help us better use structs in development.
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
}myteststruct;
At this point, we can use the MYTESTSTRUCT structure to easily encapsulate the data. Such as
Myteststruct myteststruct;
myteststruct.id=1;
myteststruct.height=23.0;
myteststruct.flag= ' A ';
Nsvalue *value = [Nsvalue valuewithbytes:&myteststruct objctype: @encode (myteststruct)];
When we want to remove the data from the *value, we can use the following methods:
Myteststruct theteststruct;
[Valeu getvalue:&theteststruct];
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.
from:www.firnow.com): http://dev.firnow.com/course/3_program/jdkf/20100719/449960.html
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];
}
Use of Nsvalue in Objective-c