#objc func expression resolves to an unused function - objective-c

I'm new to swift and trying to call a method when the user presses some hotkey shortcut.
I have the following code: https://pastebin.com/pF8wk7jL
I'm trying to run togglePopover on this handler: hotKey.keyDownHandler.
This:
hotKey.keyDownHandler = {
self.togglePopover(_:)
}
returns the error "Expression resolves to an unused function"
and this:
hotKey.keyDownHandler = {
let call = self.togglePopover(_:)
call(_:)
}
returns the error "Cannot find call in scope".
Cannot fix the issue, can someone help?
If I do:
hotKey.keyDownHandler = {
print("teste")
}
works good!

Related

How to handle simple error in Kotlin function Android

I'm having trouble handling error in the following function. I'm basically new to Kotlin. Here's my RevenueCat Login Code and I want to handle ::error in this code:
Purchases.sharedInstance.logInWith(
myUserID,
::error // <- How to handle this? I want to retrieve error Code and Error Message.
)
{ customerInfo, created ->
// Handle Successful login here
}
Here's the code behind the function (within RevenueCat SDK)
#Suppress("unused")
fun Purchases.logInWith(
appUserID: String,
onError: (error: PurchasesError) -> Unit = ON_ERROR_STUB,
onSuccess: (customerInfo: CustomerInfo, created: Boolean) -> Unit
) {
logIn(appUserID, logInSuccessListener(onSuccess, onError))
}
The double colon in ::error is a function reference. It is basically a reference to the function error().
And from your logInWith() function, we have onError: (error: PurchasesError) -> Unit = ON_ERROR_STUB, meaning that the function should take PurchasesError as input parameter and does not need to return.
So we can derive a function as the following:
fun error(error: PurchasesError) {
// And you can do something with the error here
}
I solved it like this:
Purchases.sharedInstance.logInWith(
myUserID,
onError = { error ->
// Handle error here
}

RxAlamofire: Ambiguous reference to member 'json(_:_:parameters:encoding:headers:)'

When I try to compile the code below, I get an error:
Ambiguous reference to member 'json(::parameters:encoding:headers:)'
The code was copied and pasted from a RxAlamofire Github repository page
import RxSwift
import RxAlamofire
class CurrencyRest {
static func getJson() {
let stringURL = "https://api.fixer.io/latest"
// MARK: NSURLSession simple and fast
let session = URLSession.init()
_ = session.rx.json(.get, stringURL)
.observeOn(MainScheduler.instance)
.subscribe { print($0) }
}
}
To fix the error, session.rx.json(url:) is the way to go, it's from RxCocoa, although for RxAlamofire, you don't have to use URLSession rx extension, instead, use json(::parameters:encoding:headers:), e.g. json(.get, stringURL), which returns an Observable<Any> that you can use as JSON.

Objective C Array completionHandler used in Swift

I have a completion handler in a framework written in Objective C...
This is a typedef for a block type. It takes an array of PHErrors.
typedef void (^PHBridgeSendErrorArrayCompletionHandler)(NSArray *errors);
When I try to use this in Swift, I'm doing....
anObject.aMethod(completionHandler: { (errors: [ AnyObject?]) -> () in
...rest of code
}
But I keep getting this error:
Cannot convert value of type '([AnyObject?]) -> ()' to expected argument type 'PHBridgeSendErrorArrayCompletionHandler!'
Can anyone help, I'm baffled, it looks like it should work to me.
Or better yet, you can still use your typedef as typealias.
DEFINE
typealias PHBridgeSendErrorArrayCompletionHandler = (_ errors: [Error]?) -> Void
IMPLEMENTATION
func myFunctionWithErrorCompletion(completion: PHBridgeSendErrorArrayCompletionHandler) {
// Define empty array to add errors to
var errors:[Error]?
// Do Your Logic that may store errors to array
// Completion and pass errors
completion(errors)
}
USAGE
func anotherOfMyFunctions() {
// Call the function
myFunctionWithErrorCompletion { (errors) in
if let completionErrors = errors {
// React to errors
}
}
}
anObject.aMethod(completionHandler: { (errors: [ AnyObject?]) -> () in
}
should be
anObject.aMethod() { errors in
}
In order to dig any deeper, I have to know what PHBridgeSendErrorArrayCompletionHandler is
So my friend solved this problem by simply changing AnyObject to Any
(errors: [Any]?) in
Which baffles me because all objects in an NSArray are objects! So didn't think to try Any.
Im pretty new to Swift mind
Try this..
anObject.aMethod(completionHandler: { (errors:NSArray?) -> () in
...rest of code
}

Having trouble converting a simple Objective-C block to Swift

Not sure what I'm doing wrong. I'm using this module https://github.com/wenzhaot/InstagramPhotoPicker (the header file for the block is here https://github.com/wenzhaot/InstagramPhotoPicker/blob/master/TWPhotoPicker/TWPhotoPickerController.h)
In Objective-C, the syntax is:
photoPicker.cropBlock = ^(UIImage *image) {
//do something
};
I tried converting it to swift, but I'm getting an error
photoPicker.cropBlock{ (image:UIImage) -> () in
//Do something
//error: cannot invoke 'cropBlock' with an argument list of type '((UIImage) -> ())'
}
How to get it to work?
You're just missing = sign. Also image is implicitly unwrapped optional:
photoPicker.cropBlock = { (image: UIImage!) -> () in
//Do something
}
or
photoPicker.cropBlock = { image in
//Do something
}

How do I get the Event.Complete event in a file upload to fire in Flash Builder

The file is getting uploaded properly and my ProgessEvent.Progress method is showing 100% complete, but my Event.Complete method does not fire. Do I have to send something specific back from the server? I am simply sending back a success message. I am not getting any errors.
I suppose I could just proceed when the progress method hits 100%. Shouldn't the Event.Complete method be firing after the data is sent and a response is received from the server?
**Update: I am getting an error with my Event.Complete method....
TypeError: Error #1034: Type Coercion failed: cannot convert flash.events::Event#1277971f1 to flash.events.DataEvent.
* I fixed the problem by changing my onLoadComplete(event:DataEvent) method to onLoadComplete(event:Event). The error went away and I my method is now firing. I will answer my own question when the system allows me to.
Relative code follows:
fileRef = new FileReference();
fileRef.addEventListener(Event.SELECT, onFileSelected);
fileRef.addEventListener(Event.COMPLETE, onUploadComplete);
fileRef.addEventListener(ProgressEvent.PROGRESS,onUploadProgress);
fileRef.addEventListener(IOErrorEvent.IO_ERROR, onUploadError);
fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onUploadError);
private function onUploadProgress(event:ProgressEvent):void {
status = ((event.bytesLoaded * 100) / event.bytesTotal).toString();
}
private function onUploadComplete(event:DataEvent): void {
status = "Complete";
}
private function onUploadError(event:Event):void {
if (event is IOErrorEvent) {
Alert.show((event as IOErrorEvent).text.toString());
} else if (event is SecurityErrorEvent) {
Alert.show((event as SecurityErrorEvent).text.toString());
} else {
status = "Unknown error";
}
}
I changed ...
private function onUploadComplete(event:DataEvent):void {
status = "Complete: "+event.toString();
}
To...
private function onUploadComplete(event:Event):void {
status = "Complete: "+event.toString();
}
I am guessing this is because I am not sending data back, only a simple xml block. Hope this helps someone else.