(我是一般的Swift和iOS開發新手)。在Swift中讀取NSNotification userInfo
我移植一些Objective-C代碼交給斯威夫特,我不知道如何翻譯這個:
-(void) fooDidBar:(NSNotification *)notification
{
Foo* foo = [[notification userInfo] objectForKey:BarKey];
// do stuff with foo
}
到目前爲止,我有這樣的:
private func fooDidBar(notification: NSNotification) {
var foo: Foo = notification.userInfo.objectForKey(BarKey)
}
,但我得到編譯錯誤:
/Users/me/src-me/project/FooBarViewController.swift:53:57: Value of type '[NSObject : AnyObject]?' has no member 'objectForKey'
作爲userInfo
被聲明爲NSDictionary
在UIApplicationShortcutItem.h
:
@property (nullable, nonatomic, copy) NSDictionary<NSString *, id <NSSecureCoding>> *userInfo;
...我想我會嘗試這樣的:
= notification.userInfo[BarKey]
但後來我得到這個錯誤:
/Users/me/src-me/project/FooBarViewController.swift:53:65: Type '[NSObject : AnyObject]?' has no subscript members