为什么我不能从Swift类中删除未使用的函数?

时间:2015-02-20 07:08:07

标签: xcode swift

在我定义的UserModel类中查看函数'postRequest'。好吧,这个功能在任何地方都不再使用,当我尝试删除它时,XCode语法突出显示崩溃,我的程序将无法使用指向无处的分段错误11进行编译。

我将该函数移动到名为SharedModel的结构中。所以这个功能不会在任何地方调用!为什么我不能从文件中删除它?没有意义,我只是想删除一个未使用的功能。但Xcode不断崩溃,它让我保持它!发生了什么事。

import Foundation
import SwiftHTTP
import SwiftyJSON

public enum UserProperties : String {
    case user_id = "user_id", single_access_token = "single_access_token", first_name = "first_name",
    last_name = "last_name", role = "role", email = "email", username = "username", barn_id = "barn_id",
    location_id = "location_id", farm_id = "farm_id"

    static let allValues = [user_id, single_access_token, first_name, last_name, role, email, username, barn_id, location_id, farm_id]
}

class UserModel {

    var userPropertiesJSON: JSON?

    init () {

    }

    func postRequest(url: String, params: Dictionary<String,AnyObject>, completionHandler: (JSON?) -> ()) {
        var request = HTTPTask()

        request.requestSerializer = JSONRequestSerializer()
        //The expected response will be JSON and be converted to an object return by NSJSONSerialization instead of a NSData.
        request.responseSerializer = JSONResponseSerializer()

        //we have to add the explicit type, else the wrong type is inferred. See the vluxe.io article for more info.
        //let params: Dictionary<String,AnyObject> = ["username": username, "password": password]

        request.POST(url, parameters: params, success: {(response: HTTPResponse) in
            println(response.responseObject!)
            let json = JSON(response.responseObject!)
            completionHandler(json)
            },failure: {(error: NSError, response: HTTPResponse?) in
                completionHandler(false)
        })
    }


    func login(username: String, password: String, completionHandler: (Bool) -> ()) {

        let params: Dictionary<String,AnyObject> = ["username": username, "password": password]

        SharedModel.postRequest("http://farmcentral.softimum.com/user_sessions.json", params: params) { jsonOrError in
            if let json: JSON = jsonOrError {
                self.userPropertiesJSON = json
                SharedModel.userPropertiesJSON = json
                completionHandler(true)
            } else {
                completionHandler(false)
            }
        }

    }


}

更新

仍然和问题,但我已经缩小到确切的代码行,如果删除导致xcode崩溃并导致构建停止编译由于段错误11错误。

这是违规行:让json = JSON(response.responseObject!)

这一行是在闭包内的函数中。当我删除此行Xcode崩溃。我正在运行cocoapods测试版,这可能是一个bug。

1 个答案:

答案 0 :(得分:0)

这是我使用的框架名为SwiftyJSON的错误。解决方案是将SwiftyJSON.swift文件直接导入到您的项目中。

以下是Github问题:https://github.com/SwiftyJSON/SwiftyJSON/issues/67