如何计算NSDictionary对象的总大小?
如何计算NSDictionary
对象的总大小? 我有不同的密钥在NSDictionary
3000 StudentClass对象。 我想计算字典的总大小(以KB为单位)。 我用malloc_size()
但它总是返回24( NSDictionary
包含1对象或3000对象) sizeof()
也总是返回相同的。
您可以尝试获取数组中字典的所有键,然后遍历数组以find大小,它可能会给出字典中键的总大小。
NSArray *keysArray = [yourDictionary allValues]; id obj = nil; int totalSize = 0; for(obj in keysArray) { totalSize += malloc_size(obj); }
你也可以这样find:
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);
计算大NSDictionary
的大小的最好方法,我想,将它转换为NSData
并获得数据的大小。 祝你好运!
如果你的字典包含标准的类(例如NSString)而不是客户化的,那么转换成NSData可能会有用:
NSDictionary *yourdictionary = ...; NSData * data = [NSPropertyListSerialization dataFromPropertyList:yourdictionary format:NSPropertyListBinaryFormat_v1_0 errorDescription:NULL]; NSLog(@"size of yourdictionary: %d", [data length]);
- 在Collectionview中如何根据select设置颜色?
- UICollectionView在Swift 3迁移之后崩溃了?
- iOS:使用sizeWithFont的UILabeldynamic高度:constrainedToSize:lineBreakMode:不工作
- 内存泄漏时使用NSURLSession.downloadTaskWithURL
- 斯威夫特高斯模糊图像
- 我如何保持用户的iPhone的显示?
- 应用程序在iPhone 5c上的主线程上冻结,但在iPhone 6s上没有冻结
- xCode:如何修复线程5:EXC_BREAKPOINT(code = 1,subcode = 0x10025c76c)
- 加载期间在UIWebView上的黑色区域