How to calculate total size of NSDictionary object?

后端 未结 4 734
野的像风
野的像风 2021-02-09 06:09

How to calculate total size of NSDictionary object? i have 3000 StudentClass objects in NSDictionary with different keys. And i want to calculate total

4条回答
  •  无人共我
    2021-02-09 06:54

    You can also find this way:

    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);
    

    Swift 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)
    

提交回复
热议问题