此时,我们就可以使用MyTestStruct这个结构体来很容易地封装数据。如
Test test;
test.id=1;
test.height=23.0;
test.flag='A';
NSValue *value=[NSValue valueWithBytes:&test objCType:@encode(Test)];//对结构体进行封装
NSLog(@"id=%d,height=%f,flag=%c",test.id,test.height,test.flag);
在我们想取出*value 中的数据时,可以使用如下方式:
之后就可以对theTestStruct的操作来取得其中的数据。
对于NSValue 类的使用,还有一个地方就是我们在iPhone显示点或者其它几何图形时。比如我们想取得用户在屏幕上的点击轨迹,此时首先我们想到的就是使用 CGPoint这个结构体来保存每一次点击的数据。此时,我们就可以使用NSValue类别中的+valueWithCGPoint:来帮助我们来为CG Framework提供数据。
NSValue包装对象指针,CGRect结构体等
@interface NSValue (NSValueUIGeometryExtensions)
+ (NSValue *)valueWithPointer:(const void *)pointer;//保存对象指针
+ (NSValue *)valueWithCGPoint:(CGPoint)point;//保存CGPoint结构体
+ (NSValue *)valueWithCGSize:(CGSize)size;//保存CGSize结构体
+ (NSValue *)valueWithCGRect:(CGRect)rect;//保存CGRect结构体
+ (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 do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
//NSValue包装指针
NSValue* start = [NSValue valueWithPointer:@selector(start:)];
NSValue* help = [NSValue valueWithPointer:@selector(help:)];
//NSArray 添加NSValue包装后的指针
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包装CGRect结构
NSValue *aRect1 = [NSValue valueWithCGRect:rect1];
NSValue *aRect2 = [NSValue valueWithCGRect:rect2];
//NSArray 添加NSValue包装后的指针
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]];
}