Cannot assign value type mismatch swift - ios

I have the following code snippet.
camera.onDeviceChange = { [weak self] (camera: LLSimpleCamera!, device: AVCaptureDevice!) -> Void in
print("Device changed.")
}
This used to work fine in Swift 2, but now I am getting the following error message:
Cannot assign value of type '(LLSimpleCamera!, AVCaptureDevice!) -> Void' to type '((LLSimpleCamera?, AVCaptureDevice?) -> Void)!'
Not really sure how to change this, I tried matching the type by changing the ! to optionals and then adding ! after the void, however this didn't work.

Your error suggest type mismatch that means LLSimpleCamera! != LLSimpleCamera? .. there is no need to define type. .. you can use it something like
camera.onDeviceChange = { [weak self] (camera, device) -> Void in
print("Device changed.")
}

Related

How to get address using Google maps SDK?

I have a function where I try to get a users current address through their lat/long. But I am getting an error saying Cannot convert value of type '(GMSReverseGeocodeResponse, _) -> Void' to expected argument type 'GMSReverseGeocodeCallback' (aka '(Optional<GMSReverseGeocodeResponse>, Optional<Error>) -> ()')
func googleMapsReverseGeocoding(){
let gmsGeocoder = GMSGeocoder()
gmsGeocoder.reverseGeocodeCoordinate((CLLocationCoordinate2DMake(currentLocation.coordinate.latitude, currentLocation.coordinate.longitude))) { //This is where I get the error.
(gmsReverseGeocodeResponse: GMSReverseGeocodeResponse!, error: NSError!) -> Void in
let gmsAddress: GMSAddress = gmsReverseGeocodeResponse.firstResult()
print("\ncoordinate.latitude=\(gmsAddress.coordinate.latitude)")
print("coordinate.longitude=\(gmsAddress.coordinate.longitude)")
print("thoroughfare=\(gmsAddress.thoroughfare)")
print("locality=\(gmsAddress.locality)")
print("subLocality=\(gmsAddress.subLocality)")
print("administrativeArea=\(gmsAddress.administrativeArea)")
print("postalCode=\(gmsAddress.postalCode)")
print("country=\(gmsAddress.country)")
print("lines=\(gmsAddress.lines)")
}
}
Can someone let me know what I am doing wrong?
You need
gmsGeocoder.reverseGeocodeCoordinate((CLLocationCoordinate2DMake(currentLocation.coordinate.latitude, currentLocation.coordinate.longitude))) {
(response, error) in
}

How to fix Cannot convert value of type '()?' to expected argument type 'BeaconRealm?'

I'm creating an API Manager, using the tutorial in a link
but i got error while implementing with my function
i'm using swift 4, and xcode, and alamofire.
func beaconUpdate(handler: #escaping (_ beacon: BeaconRealm?, _ error: AlertMessage?)->()){
if let token = UserDefaults.standard.string(forKey: "token"){
self.api.shared().call(type: EndpointItem.beaconData) { (beacon, message) in
if var beacon = beacon{
handler(beacon,nil)
} else{
handler(nil,message!)
}
}
}
}
The code edited with new answer
and
the error is showing
Cannot convert value of type '()?' to expected argument type 'BeaconRealm?'

Unable to use then on ios swift promisekit

I created a promise which gets some data from API and returns it. Now what i am stuck is after getting the json data i need to store them on device storage (realm). After calling the promise when i try with then i get some error stating Cannot convert value of type '(Any) -> Void' to expected argument type '(_) -> _' . But when i try with done it works perfectly. But i need to use then to save the data on realm.
Following approch works fine
Translator().translators().done { data -> Void in
print(data[0].name)
}.catch { error in
print(error)
}
But following two approach fails
Translator().translators().then() { data -> Void in
print(data)
}.catch() {
(e: Error) in
print(e)
}
Above gives an error that says
Cannot convert value of type '(Any) -> Void' to expected argument type '(_) -> _'
and following approach gives another similar error
Translator().translators().then() {
(data: [Translator]) -> Void in
print(data[0].name)
}.catch() {
(e: Error) in
print(e)
}
error message:
Cannot convert value of type '([Translator]) -> Void' to expected argument type '([Translator]) -> _'
Promise i created:
func translators() -> Promise<[Translator]> {
return firstly {
Alamofire.request("http://192.168.0.107:3000/translatdors", method: .get).responseData()
}.map {
try JSONDecoder().decode([Translator].self, from: $0.data)
}
}
Image of my code and error
You have to return a promise when you do .then because until .done you are not finished. I don't see any issue in saving realm data in .done as your method has no return type.
Incase you still want to use .then, it can be done this way.
Translator().translators().then({ data -> Promise<[Translator]> in
return Promise.value(data)
}).done({ data in
// Save to realm
}).catch({ error in
print(error.localizedDescription)
})

Cast value to enum that inherits from Error

I have a service class that offers several methods that make calls to the backend, and a common theme for those methods is that I can pass callbacks for the success and error cases:
func makeCall(onSuccess: #escaping APIResponse, onError: #escaping APIError)
The type APIError is defined like so:
typealias APIError = (Error) -> Void
Furthermore I have an enum APICallError like this:
enum APICallError: Error {
case insufficientCredentials
case malformedResponse
case forbidden
}
So in my service methods I can call onError(response.result.error!) if the result contained an error object (the force unwrap is just for brevity, I'm not really doing that) and also pass my own enum value if the HTTP result is not specific enough, like onError(APICallError.insufficientCredentials).
This works fine.
The problem is, I can't figure out how to evaluate in my client code whether the error parameter that's coming in is of type APICallError and which one of those specifically. If I do:
if let callError = error as? APICallError {
if callError == .forbidden {
// ...
}
}
execution doesn't make it past the typecast. if error is APICallError also does not seem to work.
How can I cast the error parameter to my APICallError enum value that I know is in there, because when I print(error) it gives me myapp.APICallError.forbidden?
I tried to simulate what you have posted in your question in Playground, and what you are already doing should work fine for you.
if error is APICallError is also working. One possibility why the if let condition fails might be due to the error being nil. Check if that is the case by using breakpoints.
typealias APIError = (Error) -> Void
//The error type used in your question
enum APICallError: Error {
case insufficientCredentials
case malformedResponse
case forbidden
}
//A different error type for comparison
enum AnotherError: Error {
case error1
case error2
case error3
}
let apiCallError: Error = APICallError.insufficientCredentials
let anotherError: AnotherError = AnotherError.error1
//Closure definition
var onError: APIError? = { inputError in
if let apiError = inputError as? APICallError {
print("APICallError")
}
if let anotherError = inputError as? AnotherError {
print("AnotherError")
}
}
//Above defined closure is called here below...
onError?(apiCallError)
onError?(anotherError)
Console Output (Works as expected):
APICallError
AnotherError
You need to use the enum rawValue constructor.
if let callError = APICallError(rawValue: error) {
if callError == .forbidden {
...
}
} else {
// was not an APICallError value
}

completion handler's error in swift 3 and Xcode 8

I have working project in Xcode 7.3 with swift 2.2 version. Now I have updated Xcode 8 and migrated to swift 3. Now my project contains errors specially for blocks like success block of afnetworking.
Which gives error as
Cannot convert value of type '() -> ()' to expected argument type '((URLSessionDataTask, Any?) -> Void)?'
I don't understand how to solve this to work as per swift 3.
And there is also same like error in Facebook login.
Which gives error as
Cannot convert value of type '(FBSDKLoginManagerLoginResult!, NSError!) -> Void' to expected argument type 'FBSDKLoginManagerRequestTokenHandler!'
and
Cannot convert value of type '(_, _, NSError!) -> Void' to expected argument type 'FBSDKGraphRequestHandler!'
This all errors are related to handler blocks in swift 3. I don't understand the errors and so that can't able to solve. Any help will be appreciated. Thanks in advance.
For facebook - the problem is in new Swift rules about converting objective-c function parameters into Swift.
Previously, if parameters in objective-c code did not have nullability attributes(like nonnull or nullable), Swift converts it with ! making them non optional(forced unwrapping). Now it convert it with ? making them optional. That why you are getting an error. Before you were putting as a callback for login:
(FBSDKLoginManagerLoginResult!, NSError!) -> Void
Now you need to put:
(FBSDKLoginManagerLoginResult?, Error?) -> Void
Also, as you see, now you will not see NSError class. Instead of that Swift will put Error.This is also new rule. Now all "NS" prefixed in class names is removed in Swift(NSObject -> Object; NSError -> Error).
Example of working code for facebook login in Swift 3.0:
let manager = FBSDKLoginManager()
manager.logIn(withReadPermissions: ["public_profile"], from: self.controller) {
(loginResult: FBSDKLoginManagerLoginResult?, error: Error?) in
}
Example of working code for facebook request in Swift 3.0:
let request = FBSDKGraphRequest()
request.start {
(connection: FBSDKGraphRequestConnection?, result: Any?, error: Error?) in
}
As you see, now it is using Any type instead of objective-c id. In Swift 2.2 it was using AnyObject. It is also new Swift converting rule.
You do not need to specify callback parameters type. I did that in code for highlighting their real types. So you can just write code without them:
let manager = FBSDKLoginManager()
manager.logIn(withReadPermissions: ["public_profile"], from: self.controller) { (loginResult, error) in }
let request = FBSDKGraphRequest()
request.start { (connection, result, error) in }
But you need to remember that they are optional now.
In conclusion some converting rules that may affect you callback code:
Closure parameters are optional if in objective-c are not specified nullability attributes
All "NS" prefixes is removed for objective-c classes in Swift
If objective-c function had id parameter, in Swift 3.0 it will have type Any instead of AnyObject
Though I didn't know the error before that what Xcode want to inform me about the error, but I have removed type specification with object and it worked.
As
manager.post(methodname, parameters: param, progress: nil, success:{ (dataTask, responseObj) in
if let dict : NSDictionary = responseObj as? NSDictionary {
print("Response of \(methodname) : \(dict)")
if dict.object(forKey: "response") as? String == "success" {
CompletionHandler(true, dict)
} else {
CompletionHandler(false, dict)
}
}
})
Here with respect to question error is given at dataTask and responseObj which are with type specified. After removing type it worked fine.
Same as with facebook login
#IBAction func fbLoginClicked(_ sender: AnyObject) {
let app = UIApplication.shared.delegate as! AppDelegate
app.fbLoginManager = FBSDKLoginManager()
app.fbLoginManager.logOut()
app.fbLoginManager.loginBehavior = FBSDKLoginBehavior.native
app.fbLoginManager.logIn(withReadPermissions: ["email"], from: self, handler: { (result, error) -> Void in
if error != nil {
print(error?.localizedDescription)
} else {
if (result! as FBSDKLoginManagerLoginResult).isCancelled == true {
} else {
self.fetchFacebookUserDetail()
}
}
})
}
Here also I have removed type specification of result and error and problem solved. And followed this in whole app and it worked. I can run the project without error and also it is working. Thanks.

Resources