我目前有一个 iOS 应用程序。根据用户位置,它使用一个类或另一个类。我如何为我的 iOS 应用程序执行此操作?我有一个protocol:
protocol ParseProtocol {
var urlToParse: URL? { get }
var parsedXMLData: [MyData] { get }
func parseFeed()
init()
}
然后ViewController当我检测到用户所在的城市时, parser调用函数并根据用户所在的 a 生成一个类String:
func parser(for city: String) -> ParseProtocol? {
let bundlePath = Bundle.main.bundlePath
let bundleFullName = bundlePath.components(separatedBy: "/").last
let bundleName = bundleFullName?.components(separatedBy: ".").first
let combinedName = bundleName! + "." + cityIn + "Parser"
let Class = NSClassFromString(combinedName) as? ParseProtocol.Type
print("CITY: \(city) \(combinedName)")
return Class?.init()
}
现在,我正在开发一个 watchOS 复杂功能,我想为 watchOS 和 iOS 使用与以前相同的类,因为它们是相同的。我的问题来了,因为它们是不同的捆绑包combinedName:
- watchOS
combinedName是 MyApp-watchOS Extension.LondonParser - iOS
combinedName是 MyApp.LondonParser
我被困在这里,不知道如何实现这一目标。