NValue存放的数据

来源:互联网 发布:linux复制粘贴文件 编辑:程序博客网 时间:2024/04/29 10:33

NSValue仅仅存放的是地址


如何在数据结构中存入C 结构体数据   

在使用cocos2d时候发现需要将CGPoint存入数组中,而数组接受的类型是 id。为了解决这个问题,下面使用NSDictionary作为演示。
解决方案是使用 NSValue 进行转换,它有两个函数供使用,
1 valueWithPoint:转换CGPoint和NSPoint 为NSValue; 
2 valueWithPointer:转换任何C数据结构为NSValue
这样就可以使用NSValue来代替C结构体数据 存入数据结构了。
相应的从NSValue中获取数据的函数分别是 pointValue 和 pointerValue。
具体的样例代码如下
复制代码
  1. //Test.h#import <Foundation/Foundation.h>




  2. @interface Test : NSObject {
  3. NSMutableDictionary* dictionary;
  4.     CGPoint point1;
  5. }


  6. @property (nonatomic, retain) NSMutableDictionary* dictionary;


  7. -(void)test;


  8. @end




复制代码
  1. //Test.m@implementation Test


  2. @synthesize dictionary=dictionary;


  3. -(id)init
  4. {
  5.     if ((self = [super init])) {
  6. dictionary = [[NSMutableDictionary alloc] init];
  7.     }

  8. return self;
  9. }


  10. -(void)test
  11. {
  12.     CGPoint point = CGPointMake(1.0f, 2.0f);
  13.     NSValue* value = [NSValue valueWithPoint:point];

  14.     [dictionary setObject:value forKey:@"point"];


  15.     point1 = CGPointMake(3.0f, 4.0f);
  16.     NSValue* value1 = [NSValue valueWithPointer:&point1];
  17.     [dictionary setObject:value1 forKey:@"point1"];

  18.     CGPoint point2 = CGPointMake(5.0f, 6.0f);
  19.     NSValue* value2 = [NSValue valueWithPointer:&point2];
  20.     [dictionary setObject:value2 forKey:@"point2"];

  21.     CGPoint* point3 = (CGPoint*)malloc(sizeof(CGPoint));
  22.     point3->x = 7.0f;
  23.     point3->y = 8.0f;
  24.     NSValue* value3 = [NSValue valueWithPointer:point3];
  25.     [dictionary setObject:value3 forKey:@"point3"];


  26. }


  27. -(void)dealloc
  28. {
  29.     [dictionary release];
  30. dictionary = nil;
  31.     [super dealloc];
  32. }


  33. @end





复制代码
  1. //main.m#import <Foundation/Foundation.h>
  2. #import "Test.h"




  3. int main (int argc, const char * argv[])
  4. {


  5. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];


  6. // insert code here...
  7. NSLog(@"Hello, World!");

  8.     Test* test = [[Test alloc] init];
  9.     [test test];

  10.     NSValue* val = [test.dictionary valueForKey:@"point"];
  11.     CGPoint p = [val pointValue];
  12. NSLog(@"point x = %f, y = %f", p.x, p.y);

  13.     val = [test.dictionary valueForKey:@"point1"];
  14.     CGPoint p1 = *(CGPoint*)[val pointerValue];
  15. NSLog(@"point1 x = %f, y = %f", p1.x, p1.y);

  16.     val = [test.dictionary valueForKey:@"point2"];
  17.     CGPoint p2 = *(CGPoint*)[val pointerValue];
  18. NSLog(@"point2 x = %f, y = %f", p2.x, p2.y);



  19.     val = [test.dictionary valueForKey:@"point3"];
  20.     CGPoint* p3 = [val pointerValue];
  21. NSLog(@"point2 x = %f, y = %f", p3->x, p3->y);

  22.     free(p3);
  23.     p3 = NULL;

  24.     [pool drain];
  25.     return 0;
  26. }





输出结果如下:


2011-12-15 02:09:53.261 TestNSValue[2157:903] Hello, World!
2011-12-15 02:09:53.267 TestNSValue[2157:903] point x = 1.000000, y = 2.000000
2011-12-15 02:09:53.268 TestNSValue[2157:903] point1 x = 3.000000, y = 4.000000
2011-12-15 02:09:53.268 TestNSValue[2157:903] point2 x = 0.000000, y = 0.000000
2011-12-15 02:09:53.269 TestNSValue[2157:903] point3 x = 7.000000, y = 8.000000



注意:point2 的数据是有问题的因为 point2的内存在test函数中已经释放了,这说明NSValue仅仅存放的是地址而已。
point1 是Test类的成员变量所以内存一直都在 数据是正确的。

point3 是使用malloc分配内存的 所以也是正确的,别忘记在main中free掉。

原创粉丝点击