當(dāng)你想使用Cocoa的集合來存儲(chǔ)非對(duì)象型數(shù)據(jù)時(shí),NSValue和NSNumber是非常有用的。NSNumber是NSValue的子類,所以 NSValue更靈活一些。
我們先看看NSValue能做什么:
一個(gè)NSValue對(duì) 象是用來存儲(chǔ)一個(gè)C或者Objective-C數(shù)據(jù)的簡(jiǎn)單容器。它可以保存任意類型的數(shù)據(jù),比如int,float,char,當(dāng)然也可以是指pointers, structures, and object ids。NSValue類的目標(biāo)就是允許以上數(shù)據(jù)類型的數(shù)據(jù)結(jié)構(gòu)能夠被添加到集合里,例如那些需要其元素是對(duì)象的數(shù)據(jù)結(jié)構(gòu),如NSArray或者NSSet 的實(shí)例。需要注意的是NSValue對(duì)象一直是不可枚舉的。
所以下面的代碼是可行的:
// 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];
|
是不是影像很深刻呢?然而不管怎樣,蘋果的文檔里有一 行看起來有點(diǎn)含混的解釋:
時(shí)刻記住你的struct類型必須是定長(zhǎng)的。你不可以存儲(chǔ)C字符串,不定長(zhǎng)數(shù)組和結(jié)構(gòu)和其他的一些不定長(zhǎng) 的數(shù)據(jù)類型到NSValue中去。你應(yīng)該使用NSString或者NSData來存儲(chǔ)此類不定長(zhǎng)數(shù)據(jù)。當(dāng)然你可以把一個(gè)指向變長(zhǎng)對(duì)象的指針存儲(chǔ)在 NSValue對(duì)象中。
這是什么意思呢?如果你的數(shù)據(jù)不是定長(zhǎng)的會(huì)發(fā)生什么?它能被正確的存儲(chǔ)下來嗎?
typedef struct {
int dataSize;
char *data;
int year;
} myStructType1;
|
當(dāng)data指向一個(gè)字符數(shù)組時(shí),它能被正確的編碼嗎?
回 答是很簡(jiǎn)單的,它是變長(zhǎng)的,所以它指向的數(shù)據(jù)不會(huì)被編碼。
只有這個(gè)指針地址被編碼了。所以,如果你有一個(gè)服務(wù)線程編碼了一個(gè) myStructTyle1的數(shù)據(jù)發(fā)布出去,并釋放了這快內(nèi)存,那么客戶線程拿到這個(gè)數(shù)據(jù)解碼并試圖獲取data的原始數(shù)據(jù)時(shí),那就只能得到data的指 針地址,而不是數(shù)據(jù)內(nèi)容。所以不要期望它能存儲(chǔ)你的data。你應(yīng)該使用NSData或者NSArchiver來代替NSValue以達(dá)到期望目標(biāo)。
我 們?cè)倏催@個(gè)例子:
typedef struct {
int age;
int month;
int day;
} innerType;
typedef struct {
int dataSize;
innerType *innerData;
} myStructType2;
|
恩,innerTyle是一個(gè)定長(zhǎng)的類型變量,那么它會(huì)被正確 編碼嗎?
不會(huì),蘋果的文檔并沒有說明此類情況。它依然只編碼指針而不是內(nèi)容。
所以在這種情況下,依然得使用NSData。
總結(jié), 使用NSValue只能是對(duì)那些沒有變量是指針的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,都是可以對(duì)非對(duì)象進(jìn)行編碼存儲(chǔ)的。但在我的工 程里,線程之間通信的數(shù)據(jù)是需要進(jìn)行序列化的,我使用了NSKeyedArchiver來序列化。
在使用過程中發(fā)現(xiàn)NSValue存儲(chǔ)的數(shù)據(jù)不可 被序列化,而NSData可以。我的struct是定長(zhǎng)的。
所以最好包裝時(shí)都使用NSData吧,如果時(shí)rect, point之類的倒是可以用NSvalue,它已經(jīng)提供好接口供你使用了。
用NSValue試了半天.保存CGSize不行.
后來改用NSData好了..
復(fù)制代碼
- UIImage *currentImg = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i]];
- CGSize imageSize = currentImg.size;
- NSData *pointObjectIn = [NSData dataWithBytes:&imageSize length:sizeof(CGSize)];
- [persistentArray addObject:pointObjectIn];
|
復(fù)制代碼
- NSData* getImgeData = [arrayImageCGSize objectAtIndex:i] ;
- CGSize imageSize = *(CGSize*)[getImgeData bytes];
|
From: http://www.cocoachina.com/bbs/read.php?tid-13738.html
@import url(http://www.shnenglu.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);