Skip to content

Instantly share code, notes, and snippets.

@hyperspacemark
Created December 8, 2014 03:28
Show Gist options
  • Save hyperspacemark/8a779bf25969bff50dfb to your computer and use it in GitHub Desktop.
Save hyperspacemark/8a779bf25969bff50dfb to your computer and use it in GitHub Desktop.
enum CameraConnectionResult {
case Success(CameraType)
case Failure(NSError) // Really don't want this to be Optional<NSError>
}
class CameraClient {
class func connect(completion: CameraConnectionResult -> ()) {
let URL = NSURL(string: "http://10.5.5.9:8080/videos/MISC/version.txt")
if let versionURL = URL {
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
let URLSession = NSURLSession(configuration: configuration)
URLSession.dataTaskWithURL(versionURL) { data, response, error in
if (data != nil) {
// NSErrors may be nil if an error did not occur
var JSONError: NSError?
if let JSON: AnyObject = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &JSONError) {
if let cameraTypeString = JSON["camera type"] as? String {
let cameraType = CameraType(cameraTypeString)
completion(CameraConnectionResult.Success(cameraType))
}
} else {
// Get an error here about the type mismatch. Is it acceptable to force unwrap? i.e. .Failure(JSONError!)
completion(CameraConnectionResult.Failure(JSONError))
}
} else {
completion(CameraConnectionResult.Failure(error))
}
}.resume()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment