最近,我将代码重构为基本的coapapod,以用于多个项目。其中一个类是Alamofire的一个非常典型的路由枚举。它一直工作良好,直到被转移到cocoapod中为止。一个示例实现:

public enum Router: URLRequestConvertible {

    static var baseURLString = "http://myserver.com/api"

    case Endpoint1(String, String)
    case Endpoint2(String, String, Date, Date)
    case Endpoint3(String, String)

    public var URLRequest: NSMutableURLRequest {
        var path : String! {
            switch self {
            case .Endpoint1(let id, _):
                return "/endpoint1/" + id
            case .Endpoint2(let id, _, _, _):
                return "/booking/\(id)/thing"
            case .Endpoint3(_, _):
                return "/authenticate"
            }
        }

        var parameters: [String: AnyObject] {
            switch self {
            case .Endpoint2(_, _, let startDate, let endDate):
                let params = ["startDate": startDate, "endDate" : endDate]
                return params
            default:
                let params = [String: AnyObject]()
                return params
            }
        }

        var method :  Alamofire.Method {
            switch self {
            case .Endpoint2:
                return .POST
            default:
                return .GET
            }
        }

        var body : [String : AnyObject]?{
            switch self{
            case .Endpoint3(let email, let password):
                return ["email" : "\(email)", "password" : "\(password)"]
            default:
                return nil
            }
        }

        var header : [String: String]?{
            switch self {
            case .Endpoint1(_, let token):
                return ["x-access-token" : token]
            default:
                return nil
            }
        }

        let URL = NSURL(string: Router.baseURLString)?.URLByAppendingPathComponent(path)
        let request = NSURLRequest(URL: URL!)

        let encoding = Alamofire.ParameterEncoding.URL

        let mutable = (encoding.encode(request, parameters: parameters).0)
        mutable.HTTPMethod = method.rawValue

        if let bodyParams = body{
            mutable.setValue("application/json", forHTTPHeaderField: "Content-Type")
            mutable.addJsonArrayToRequestBody(bodyParams)
        }

        if let headerParams = header{
            for param in headerParams{
                mutable.setValue(param.1, forHTTPHeaderField: param.0)
            }
        }

        return mutable
    }
}


失败行是:

for param in headerParams{


但是正如在开发过程中一样,我无法在这一点上附加调试器,错误也不会超过EXEC_BAD_INSTRUCTION,这没有什么用。如何进一步调试导致错误的原因?

最佳答案

使用开发cocoapod时,这是xcode调试器中的错误,实际错误是:

让URL = NSURL(string:Router.baseURLString)?. URLByAppendingPathComponent(path)

调用此方法时,尚未设置静态baseURLString,导致URL为nil。在现在共享的代码中,我看到调试器多次出现,指出错误的行,不确定如何解决。

10-06 02:56