【发布时间】:2018-03-09 14:53:31
【问题描述】:
我目前有一个 iOS 应用程序。根据用户位置,它使用一个类或另一个类。我如何为我的 iOS 应用程序执行此操作?我有一个protocol:
protocol ParseProtocol {
var urlToParse: URL? { get }
var parsedXMLData: [MyData] { get }
func parseFeed()
init()
}
然后在ViewController 上,当我检测到用户所在的城市时,会调用parser 函数并根据用户所在的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
我很困在这里,不知道如何实现这一点。
【问题讨论】:
-
如何将
LondonParser和其他类似的类添加到 WatchKit 扩展目标中?一个类可以在多个目标中
标签: ios swift watchos watchos-4