简体   繁体   English

为什么NSMutableDictionary的setValue:forKey不起作用

[英]Why NSMutableDictionary's setValue:forKey doesn't work

I want to write a timer class that records average elapsed time of a function. 我想编写一个计时器类,记录一个函数的平均经过时间。 So I want to save how many times the function is ran. 所以我想保存该函数运行了多少次。 But the item in dictionary isn't changed. 但是字典中的项目没有改变。

here is my code: 这是我的代码:

@implementation Timer {
    NSDate *startTime;
    // NSString *item;
    NSMutableDictionary *itemCounts;
    NSMutableDictionary *itemTimes;

}

- (void)tic {
    startTime = [NSDate date];
}
- (void)tocWithMessage:(NSString*)message {
    if (itemCounts[message] == nil) {
        // itemCounts[message] = @1;
        [itemCounts setValue:@1 forKey:message];
        NSLog(@"%d", [itemCounts[message] integerValue]);
    } else {
        itemCounts[message] = @([itemCounts[message] integerValue] + 1);
    }
    double totalTime = [itemTimes[message] doubleValue];
    double thisTime = -[startTime timeIntervalSinceNow] * 1000;
    totalTime += thisTime;
    itemTimes[message] = @(totalTime);
    int count = [itemCounts[message] integerValue];
    double averageTime = totalTime / count;
    NSLog(@"%@: No.%d, average time: %lfms", message, count, averageTime);
    // std::cout << message << ": " << 1. * (clock() - timerTimestamp) / CLOCKS_PER_SEC * 1000 << "ms" << std::endl;
}

@end

But the itemCounts[message] is always nil. 但是itemCounts [message]始终为零。 Please help me :) 请帮我 :)

Thanks in advance! 提前致谢!

You have only declared the dictionaries. 您只声明了字典。

Before using them you have to initialize them somewhere: 在使用它们之前,您必须在某个地方初始化它们:

itemCounts = [[NSMutableDictionary alloc] init];
itemTimes = [[NSMutableDictionary alloc] init];

You forgot to initialize the dictionaries. 您忘记了初始化字典。 In Swift you would have seen a crash if you do like this :) 在Swift中,如果您这样做的话,可能会崩溃:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NSMutableDictionary中的setValue forKey崩溃 - setValue forKey in NSMutableDictionary crashes NSMutableDictionary setValue:forKey有时会失败 - NSMutableDictionary setValue:forKey failing sometimes 更新NSMutableDictionary的字段时出现&#39;-setValue:forKey:&#39;异常 - '-setValue:forKey:' exception when updating the fields of an NSMutableDictionary 为什么UIView Animation不触发CALayer的addAnimation:forKey :? - Why UIView Animation doesn't trigger CALayer's addAnimation:forKey:? [NSMutableDictionary setValue: value forKey: key] 是否保留 NSString 键? - Does [NSMutableDictionary setValue: value forKey: key] retain NSString key? 对于NSMutableArray,setValue:forKey:和setObject:forKey:有什么区别? - What's the difference between setValue:forKey: and setObject:forKey: for NSMutableArray? NSMutableDictionary setObject forKey forKey…等 - NSMutableDictionary setObject forKey forKey…etc 尝试将NSString添加到NSMutableDictionary不起作用 - Trying to add NSString to NSMutableDictionary doesn't work NSMutableDictionary是否通过“ setValue:forKey:”对我放入的对象保持强烈引用? - Does NSMutableDictionary keep a strong reference to an object I put in it via 'setValue:forKey:'? setObject:forKey:NSMutableDictionary的方法 - setObject:forKey: method of NSMutableDictionary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM