2012-01-23 6 views
0

私はこのコードを持っている:subdataWithRangeの部分データ範囲がクラッシュするのはなぜですか?

NSMutableData *derivedKey = [NSMutableData dataWithLength:32]; 

// code missing..fill somehow the derivedKey with 32 random bytes 

// This line doesn't crash 
NSData *iv = [derivedKey subdataWithRange:NSMakeRange(0, 32)]; 

..

// This line crashes 
NSData *iv = [derivedKey subdataWithRange:NSMakeRange(16, 32)]; 

これがなぜ起こるかどんな提案? NSRangeMakeの2番目のパラメータは範囲の長さであるので、32回のパス 私は、それがクラッシュしたバイト

答えて

10

の唯一の後半を含む単純な新しいNSDataの変数をしたい - 何とか0からの唯一の全範囲がいるようです。だからあなたがやろうとしているのは、データサイズを超えるオフセット16から始まる32バイトを取ることです(最終バイトは48バイトとなります)。

ので、単純にそれを変更:

NSData *iv = [derivedKey subdataWithRange:NSMakeRange(16, 16)]; 

チェックREF: http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#//apple_ref/c/func/NSMakeRange

+0

Oh..Iは、位置とそれを混同。どうもありがとうございました –

関連する問題