NSDictionary
オブジェクトの合計サイズを計算する方法は?NSDictionary
異なるキーを持つ3000のStudentClassオブジェクトがあります。そして、辞書の合計サイズをKB単位で計算したいと思います。私は使用malloc_size()
しましたが、常に24を返します(NSDictionary
1つのオブジェクトまたは3000のオブジェクトを含む)
sizeof()
も常に同じを返します。
7378 次
4 に答える
12
この方法も見つけることができます:
Objective C
NSDictionary *dict=@{@"a": @"Apple",@"b": @"bApple",@"c": @"cApple",@"d": @"dApple",@"e": @"eApple", @"f": @"bApple",@"g": @"cApple",@"h": @"dApple",@"i": @"eApple"};
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:dict forKey:@"dictKey"];
[archiver finishEncoding];
NSInteger bytes=[data length];
float kbytes=bytes/1024.0;
NSLog(@"%f Kbytes",kbytes);
スウィフト4
let dict: [String: String] = [
"a": "Apple", "b": "bApple", "c": "cApple", "d": "dApple", "e": "eApple", "f": "bApple", "g": "cApple", "h": "dApple", "i": "eApple"
]
let data = NSMutableData()
let archiver = NSKeyedArchiver(forWritingWith: data)
archiver.encode(dict, forKey: "dictKey")
archiver.finishEncoding()
let bytes = data.length
let kbytes = Float(bytes) / 1024.0
print(kbytes)
于 2013-03-18T12:06:30.500 に答える
5
辞書内のすべてのキーを配列で取得し、配列を反復処理してサイズを見つけることができます。これにより、辞書内のキーの合計サイズが得られる場合があります。
NSArray *keysArray = [yourDictionary allValues];
id obj = nil;
int totalSize = 0;
for(obj in keysArray)
{
totalSize += malloc_size(obj);
}
于 2013-03-18T11:47:49.113 に答える
3
bigのサイズを計算する最良の方法は、それをに変換してデータのサイズを取得NSDictionary
することだと思います。NSData
幸運を!
于 2013-03-18T11:43:06.063 に答える
2
辞書に標準クラス(NSStringなど)が含まれていて、カスタムクラスが含まれていない場合は、NSDataに変換すると便利な場合があります。
NSDictionary *yourdictionary = ...;
NSData * data = [NSPropertyListSerialization dataFromPropertyList:yourdictionary
format:NSPropertyListBinaryFormat_v1_0 errorDescription:NULL];
NSLog(@"size of yourdictionary: %d", [data length]);
于 2013-03-18T11:51:28.903 に答える