Swift文档说可以在扩展中添加初始化程序,文档中的示例是关于向结构添加初始化程序.Xcode无法识别UIColor我的便捷初始化程序中的指定初始值设定项:
extension UIColor {
convenience init(rawValue red: CGFloat, green g: CGFloat, blue b: CGFloat, alpha a: CGFloat) {
// Can not find out the designated initializer here
self.init()
}
}
Run Code Online (Sandbox Code Playgroud)
有解决方案吗
我有一个HTTP方法的枚举:
enum HTTPMethod: String {
case GET = "GET"
case POST = "POST"
}
Run Code Online (Sandbox Code Playgroud)
我有一个请求类和一个请求包装类:
class Request {
let method: HTTPMethod = .GET
}
class RequestWrapper {
let request: Request
func compareToRequest(incomingRequest: NSURLRequest) -> Bool {
// Next line is where the conditional breakpoint set.
return request.method.rawValue == incomingRequest.HTTPMethod
}
}
Run Code Online (Sandbox Code Playgroud)
我在行上设置了一个条件断点:
return request.method.rawValue == incomingRequest.HTTPMethod
Run Code Online (Sandbox Code Playgroud)
条件:
self.request.method == HTTPMethod.POST
Run Code Online (Sandbox Code Playgroud)
然后调试器在该行停止并显示错误消息:
Stopped due to an error evaluating condition of breakpoint 1.1:
"self.request.method == HTTPMethod.POST"
Couldn't parse conditional expression:
<EXPR>:1:1: error: …Run Code Online (Sandbox Code Playgroud) 手动编写代码时,我们可以通过AppDelegate 中的构造函数注入将依赖项注入到 rootViewController 中:
UIViewController *vc = [[SomeViewController alloc] initWithDependency: dependency];
self.window.rootViewController = vc;
Run Code Online (Sandbox Code Playgroud)
但是,我找不到在使用Storyboard时注入依赖项的方法。将它们注入awakeFromNib或viewDidLoad等等似乎是不合适的。
那可以注射吗?