按预期工作。
…Objects:
和forKeys:
应该对齐,因为它们形成相同方法签名的一部分。
这可能是更容易,如果你使用新的对象文本语法来格式化你的代码:
- (int)minBrokenPieces {
NSDictionary *mapping = [NSDictionary dictionaryWithObjects:@[@"3", @"4", @"4", @"5", @"6", @"7", @"8"]
forKeys:[Note types]];
[(NSString *)mapping[self.note.type] integerValue];
}
至于代码本身,似乎有点危险的定义在一个地方,这些常数和纪录类型别处。另外,为什么使用字符串,当NSNumbers足够?
(这段代码假设这个函数只能从一个线程中调用)。
- (int)minBrokenPieces {
static NSDictionary *mappings;
if (!mappings) {
mappings = @{
noteType1 : @3,
noteType2 : @4,
noteType3 : @4,
noteType4 : @5,
noteType5 : @6,
noteType6 : @7,
noteType7 : @8,
};
}
NSAssert(mappings[self.note.type] != nil, @"Used a note type for which there is no minBrokenPieces defined");
return [mappings[self.note.type] intValue];
}
当您粘贴使用Shift-Option-Command-V而不是Command-V并且您的格式将被保留。 –
你会喜欢这个技巧:在函数的范围内创建一行代码。确保它是半长的。在它下面放一个{关闭它,关闭它,没关系。然后,将光标放在第一行的末尾,点击空格(或制表符),然后按回车键。我只是喜欢重现那个错误。如果你没有得到它,请添加更多的字符,但不要让该线足够长以超过新的换行线。 您的光标现在会显示在{之前和之下,但文本显示在右侧区域。无害,但很有趣,我可以在XCode 4上重新制作一个有趣的聚会炫耀技巧 –