我在 Swift 2.3 到 Swift 3.0 中遇到了一个不寻常的问题。一旦我将代码从 2.3 转换为 3.0,我遇到了这个问题:'对成员'下标'的引用不明确,
代码是:
dynamic func onDataNotification(notification: NSNotification) {
var data = notification.userInfo as! Dictionary<NSString, ARoutedMessage>
if let packet = data[AEnginePacketDataKey] as? AEngineMessage,
currentDevice = self.currentDevice() {
if packet.messageId == MessageId.message && currentDevice.isDevice() {
// Some code
}
}
}
在 let packet = data[AEnginePacketDataKey] 上出现错误,因为对成员“下标”的引用不明确,我不明白为什么?其他:
// String
extern NSString *AEnginePacketDataKey;
//ARoutedMessage Class
@interface ARoutedMessage : NSObject
@property NSMutableArray *payloadParameters;
@end
//AEngineMessage Class
@interface AEngineMessage : ARoutedMessage
@property (readonly)MessageId messageId;
- (id) initWithMessageId:(MessageId) mId;
@end
请帮帮我。
最佳答案
“模糊引用”错误告诉您尝试用作下标的变量类型错误。
在本例中,data
是一个需要 NSString
下标的字典。看起来 AEnginePacketDataKey
被定义为 Objective C 代码中指向 NSString
的指针,但您没有显示在何处(如果)为其分配了任何内容。在尝试将其用作 data
的下标之前,请确保为其分配一个实际的 NSString
。
关于swift3 - swift 3.0 : Ambiguous reference to member 'Subscript' issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40415338/