逛奔的蜗牛

我不聪明,但我会很努力

   ::  :: 新随笔 ::  ::  :: 管理 ::

当你想使用Cocoa的集合来存储非对象型数据时,NSValue和NSNumber是非常有用的。NSNumber是NSValue的子类,所以 NSValue更灵活一些。
我们先看看NSValue能做什么:

                 一个NSValue对 象是用来存储一个C或者Objective-C数据的简单容器。它可以保存任意类型的数据,比如int,float,char,当然也可以是指pointers, structures, and object ids。NSValue类的目标就是允许以上数据类型的数据结构能够被添加到集合里,例如那些需要其元素是对象的数据结构,如NSArray或者NSSet 的实例。需要注意的是NSValue对象一直是不可枚举的。

所以下面的代码是可行的:

 
            // assume ImaginaryNumber defined:
            typedef struct {
            float real;
            float imaginary;
            } ImaginaryNumber;
             
            ImaginaryNumber miNumber;
            miNumber.real = 1.1;
            miNumber.imaginary = 1.41;
             
            NSValue *miValue = [NSValue value:miNumber
            withObjCType:@encode(ImaginaryNumber)]; // encode using the type name
             
            ImaginaryNumber miNumber2;
            [miValue getValue:&miNumber2];
             



是不是影像很深刻呢?然而不管怎样,苹果的文档里有一 行看起来有点含混的解释:

时刻记住你的struct类型必须是定长的。你不可以存储C字符串,不定长数组和结构和其他的一些不定长 的数据类型到NSValue中去。你应该使用NSString或者NSData来存储此类不定长数据。当然你可以把一个指向变长对象的指针存储在 NSValue对象中。

这是什么意思呢?如果你的数据不是定长的会发生什么?它能被正确的存储下来吗?

 
            typedef struct {
            int dataSize;
            char *data;
            int year;
            } myStructType1;
             


当data指向一个字符数组时,它能被正确的编码吗?
回 答是很简单的,它是变长的,所以它指向的数据不会被编码。
只有这个指针地址被编码了。所以,如果你有一个服务线程编码了一个 myStructTyle1的数据发布出去,并释放了这快内存,那么客户线程拿到这个数据解码并试图获取data的原始数据时,那就只能得到data的指 针地址,而不是数据内容。所以不要期望它能存储你的data。你应该使用NSData或者NSArchiver来代替NSValue以达到期望目标。

我 们再看这个例子:

 
            typedef struct {
            int age;
            int month;
            int day;
            } innerType;
             
            typedef struct {
            int dataSize;
            innerType *innerData;
            } myStructType2;
             


恩,innerTyle是一个定长的类型变量,那么它会被正确 编码吗?
不会,苹果的文档并没有说明此类情况。它依然只编码指针而不是内容。
所以在这种情况下,依然得使用NSData。
总结, 使用NSValue只能是对那些没有变量是指针的struct。
Then how the NSValue stores? It is kind of shallow copy. Please read this.
Here the address of myCString is passed (&myCString), so the address of the first character of the string is stored in theValue. Note that the NSValue object doesn’t copy the contents of the string, but the pointer itself. If you create an NSValue object with an allocated data item, don’t deallocate its memory while the NSValue object exists.

=============================== 我是引用分割线=================

不管是NSValue还是NSData,都是可以对非对象进行编码存储的。但在我的工 程里,线程之间通信的数据是需要进行序列化的,我使用了NSKeyedArchiver来序列化。
在使用过程中发现NSValue存储的数据不可 被序列化,而NSData可以。我的struct是定长的。

所以最好包装时都使用NSData吧,如果时rect, point之类的倒是可以用NSvalue,它已经提供好接口供你使用了。


用NSValue试了半天.保存CGSize不行.
后来改用NSData好了..
复制代码
  1. UIImage *currentImg = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i]];
  2.         CGSize imageSize = currentImg.size;            
  3.         NSData *pointObjectIn = [NSData dataWithBytes:&imageSize length:sizeof(CGSize)];
  4.         [persistentArray addObject:pointObjectIn];        


复制代码
  1. NSData* getImgeData = [arrayImageCGSize objectAtIndex:i] ;
  2.         CGSize imageSize = *(CGSize*)[getImgeData bytes];

From: http://www.cocoachina.com/bbs/read.php?tid-13738.html

@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
posted on 2011-12-09 03:53 逛奔的蜗牛 阅读(599) 评论(0)  编辑 收藏 引用 所属分类: Cocoa

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理