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。
具体的样例代码如下:
复制代码
- //Test.h#import <Foundation/Foundation.h>
- @interface Test : NSObject {
- NSMutableDictionary* dictionary;
- CGPoint point1;
- }
- @property (nonatomic, retain) NSMutableDictionary* dictionary;
- -(void)test;
- @end
复制代码
- //Test.m@implementation Test
- @synthesize dictionary=dictionary;
- -(id)init
- {
- if ((self = [super init])) {
- dictionary = [[NSMutableDictionary alloc] init];
- }
- return self;
- }
- -(void)test
- {
- CGPoint point = CGPointMake(1.0f, 2.0f);
- NSValue* value = [NSValue valueWithPoint:point];
- [dictionary setObject:value forKey:@"point"];
- point1 = CGPointMake(3.0f, 4.0f);
- NSValue* value1 = [NSValue valueWithPointer:&point1];
- [dictionary setObject:value1 forKey:@"point1"];
- CGPoint point2 = CGPointMake(5.0f, 6.0f);
- NSValue* value2 = [NSValue valueWithPointer:&point2];
- [dictionary setObject:value2 forKey:@"point2"];
- CGPoint* point3 = (CGPoint*)malloc(sizeof(CGPoint));
- point3->x = 7.0f;
- point3->y = 8.0f;
- NSValue* value3 = [NSValue valueWithPointer:point3];
- [dictionary setObject:value3 forKey:@"point3"];
- }
- -(void)dealloc
- {
- [dictionary release];
- dictionary = nil;
- [super dealloc];
- }
- @end
复制代码
- //main.m#import <Foundation/Foundation.h>
- #import "Test.h"
- int main (int argc, const char * argv[])
- {
- NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
- // insert code here...
- NSLog(@"Hello, World!");
- Test* test = [[Test alloc] init];
- [test test];
- NSValue* val = [test.dictionary valueForKey:@"point"];
- CGPoint p = [val pointValue];
- NSLog(@"point x = %f, y = %f", p.x, p.y);
- val = [test.dictionary valueForKey:@"point1"];
- CGPoint p1 = *(CGPoint*)[val pointerValue];
- NSLog(@"point1 x = %f, y = %f", p1.x, p1.y);
- val = [test.dictionary valueForKey:@"point2"];
- CGPoint p2 = *(CGPoint*)[val pointerValue];
- NSLog(@"point2 x = %f, y = %f", p2.x, p2.y);
- val = [test.dictionary valueForKey:@"point3"];
- CGPoint* p3 = [val pointerValue];
- NSLog(@"point2 x = %f, y = %f", p3->x, p3->y);
- free(p3);
- p3 = NULL;
- [pool drain];
- return 0;
- }
输出结果如下:
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掉。
- NValue存放的数据
- 数据存放的位置
- 【Swift】数据的存放
- 数据在内存的存放
- hbase数据的存放顺序
- 堆栈的作用,以及存放的数据
- Oracle DataBlock是如何存放数据的
- Android应用程序的数据存放目录解说
- android应用程序的数据存放目录
- Android应用程序的数据存放目录解说
- Android应用程序的数据存放目录解说
- Android应用程序的数据存放目录解说
- 阶乘的精确值 大数据存放
- Android应用程序的数据存放目录解说
- Android应用程序的数据存放目录解说
- 数据在内存中存放的位置
- 多字节数据的存放顺序
- opentsdb采集的数据存放在哪里
- Android多媒体分析(四)AudioManager
- typedef和 #define的区别
- 程序退出自动打印函数栈
- 回到原点
- 仿阿里巴巴顶部用户信息网站导航条
- NValue存放的数据
- 平台移植 GPIO CONFIG
- Tomcat性能调优方案
- Android的UI组件之TextView、EditText
- 无语的ClassCastException
- VPN含义
- HTML的发展历史
- 国内手机应用开发者6成亏损 广告或是突破口
- 网页设计使用Div+css的优点总结