0

See the function 'postRequest' in this UserModel class I have defined. Well the function is not used anymore anywhere and when I try to remove it XCode syntax highlighting crashes and my program will not compile with a segmentation fault error 11 which points to nowhere.

I moved the function into a struct called SharedModel. So this function is not called anywhere! Why can I not remove it from the file? It makes no sense, I just want to remove an unused function. But Xcode keeps crashing and it makes me keep it! What is going on.

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)
            }
        }

    }


}

UPDATE

Still and issue but I have narrowed it down to the exact line of code that if removed causes xcode to crash and causes the build to stop compiling due to a segment fault 11 error.

This is the offending line: let json = JSON(response.responseObject!)

This line is in the function inside the closure. When I remove this line Xcode crashes. I am running cocoapods beta version, this might be a bug.

Nearpoint
  • 7,202
  • 13
  • 46
  • 74
  • Even if I comment out the code within the function Xcode crashes. It looks like Xcode needs to have the code inside the function for some reason. As soon as I comment out the request.POST block of code inside that function XCode crashes. Why is this? – Nearpoint Feb 20 '15 at 07:13
  • I can even rename the function to anything I want. Even to 'sdgsdgdsgsdagsdagds' or any random name. But I just cannot delete it from my code, or XCode crashes. I even tried restarting my machine. – Nearpoint Feb 20 '15 at 07:19
  • Try cleaning your project and building it again – The Tom Feb 20 '15 at 07:23
  • I just tried to clean the project with command-option-shift-k. I even tried all the steps to clean outlined here: http://stackoverflow.com/questions/5714372/how-to-empty-caches-and-clean-all-targets-xcode-4 Still when I comment out the function, or the code inside the function Xcode crashes, and I can't build the project. Keep getting segmentation fault 11 – Nearpoint Feb 20 '15 at 07:33
  • I am using beta version of Cocoapods and SwiftHTTP. Maybe that could be related to the issue. – Nearpoint Feb 20 '15 at 07:39
  • this issue was a bug with a framework i was using called SwiftyJSON – Nearpoint Feb 20 '15 at 08:51

1 Answers1

0

This was a bug with the framework I was using called SwiftyJSON. The solution was to import SwiftyJSON.swift file directly into your project.

Here is the Github issue: https://github.com/SwiftyJSON/SwiftyJSON/issues/67

Nearpoint
  • 7,202
  • 13
  • 46
  • 74