如何解决Swift中‘Ambiguous use of 'json'’编译歧义错误?
解决「Ambiguous use of 'json'」编译错误
编译时遇到「Ambiguous use of 'json'」错误,原因是项目中存在多个.json(atKeyPath:)方法的实现,编译器无法推断你要调用的具体版本。
你的代码如下:
public struct QubitDisriptionInfo { public let id: String? public let popupId: String? public let journeyOrigins: String? public let journeyDestinations: String? public let departureDateFrom: String? public let departureDateTo: String? public let departureTimeFrom: String? public let departureTimeTo: String? public let titleCopy: String? public let bodyCopy: String? public let primaryKey: String? } extension QubitDisriptionInfo: JSONObjectConvertible { public init(jsonDictionary: JSONDictionary) throws { let notifications = jsonDictionary.json(atKeyPath: "data.notifications") as! [[String: String]] // 错误发生位置 let notification = notifications.first! id = notification.json(atKeyPath: "id") as! String popupId = notification.json(atKeyPath: "popup-id") as! String journeyOrigins = notification.json(atKeyPath: "journey-origins") as! String journeyDestinations = notification.json(atKeyPath: "journey-destinations") as! String departureDateFrom = notification.json(atKeyPath: "departure-date-from") as! String departureDateTo = notification.json(atKeyPath: "departure-date-to") as! String departureTimeFrom = notification.json(atKeyPath: "departure-time-from") as! String departureTimeTo = notification.json(atKeyPath: "departure-time-to") as! String titleCopy = notification.json(atKeyPath: "title-copy") as! String bodyCopy = notification.json(atKeyPath: "body-copy") as! String primaryKey = notification.json(atKeyPath: "primary-key") as! String } }
待解析的JSON数据:
{ "data": { "notifications": [ { "id": "118", "popup-id": "3", "journey-origins": "EUS", "journey-destinations": "MAN", "departure-date-from": "20230226", "departure-date-to": "20230226", "departure-time-from": "1900", "departure-time-to": "2130", "title-copy": "Busy trains expected", "body-copy": "Due to the EFL Cup Final at Wembley, trains from Manchester to London in the morning and returning north in the evening are expected to be very busy with football fans. Trains showing 'sold out' are likely to be at full capacity. If you're not heading to the game, we recommend travelling outside of these periods - heading south after 12:00 or heading north before 19:00 is expected to be quieter.", "primary-key": "EUS-MAN" } // 更多通知数据 ] } }
解决方法
1. 显式标注变量类型
给变量加上明确的类型声明,帮助编译器锁定对应的方法实现:
// 显式标注notifications的类型 let notifications: [[String: Any]] = jsonDictionary.json(atKeyPath: "data.notifications") as! [[String: Any]]
或者在类型转换时更明确,避免编译器歧义:
let notifications = jsonDictionary.json(atKeyPath: "data.notifications") as [[String: Any]]? ?? []
2. 强制转换为目标类型后调用方法
如果知道.json(atKeyPath:)是针对[String: Any]这类字典的扩展,可以先将jsonDictionary转换为该类型再调用:
let notifications = (jsonDictionary as [String: Any]).json(atKeyPath: "data.notifications") as! [[String: String]]
3. 优化代码安全性(额外建议)
你的代码中大量使用强制解包!,一旦JSON结构不符会直接崩溃,建议改用可选绑定或guard let处理:
public init(jsonDictionary: JSONDictionary) throws { guard let notifications = jsonDictionary.json(atKeyPath: "data.notifications") as? [[String: Any]], let notification = notifications.first else { throw NSError(domain: "QubitParsingError", code: 1, userInfo: [NSLocalizedDescriptionKey: "无法解析通知数据"]) } id = notification.json(atKeyPath: "id") as? String popupId = notification.json(atKeyPath: "popup-id") as? String journeyOrigins = notification.json(atKeyPath: "journey-origins") as? String journeyDestinations = notification.json(atKeyPath: "journey-destinations") as? String departureDateFrom = notification.json(atKeyPath: "departure-date-from") as? String departureDateTo = notification.json(atKeyPath: "departure-date-to") as? String departureTimeFrom = notification.json(atKeyPath: "departure-time-from") as? String departureTimeTo = notification.json(atKeyPath: "departure-time-to") as? String titleCopy = notification.json(atKeyPath: "title-copy") as? String bodyCopy = notification.json(atKeyPath: "body-copy") as? String primaryKey = notification.json(atKeyPath: "primary-key") as? String }
内容的提问来源于stack exchange,提问作者SJR5
相关产品推荐
相关产品推荐

