是否可以调用NSDictionary的valueForKeyPath:当一个键包含句点时?

Jon*_*kas 5 macos cocoa objective-c foundation

我试图在com.apple.scheduler plist中获取repeatInterval键的值.我想像这样使用NSDictionary的valueForKeyPath:方法:

CFPropertyListRef value;
value = CFPreferencesCopyValue(CFSTR("AbsoluteSchedule"),
                               CFSTR("com.apple.scheduler"),
                               kCFPreferencesCurrentUser,
                               kCFPreferencesCurrentHost);
NSNumber *repeatInterval = [(NSDictionary *)value valueForKeyPath:@"com.apple.SoftwareUpdate.SUCheckSchedulerTag.Timer.repeatInterval"];
Run Code Online (Sandbox Code Playgroud)

但问题是第一个键实际上是"com.apple.SoftwareUpdate",而不仅仅是"com".我可以通过单独获取第一个值来解决这个问题:

NSDictionary *dict = [(NSDictionary *)value valueForKey:@"com.apple.SoftwareUpdate"];
NSNumber *repeatInterval = [dict valueForKeyPath:@"SUCheckSchedulerTag.Timer.repeatInterval"];
Run Code Online (Sandbox Code Playgroud)

我只是想知道是否有办法在一个密钥路径中逃避时期,这样我就可以消除这个额外的步骤.

Jer*_*myP 1

NSDictionary 没有 valueforKeyPath: 方法。它只是调用 NSObject 实现,这是问题的根源。

也许你可以用你自己的转义字符在 NSDictionary 的类别中重新实现它。