readWithCallback function not working in swift 3 - swift

Before the swift update(in swift 2) my code was like the following and it worked successfully with no errors.
client.me.events.readWithCallback({
(list: Array<AnyObject>!, error: MSOrcError!) -> Void in
}
But after the swift update (in Swift 3) I get errors in the previous code, so I changed it to the below code. It has no errors but when I run the app it gets crashes and terminates. When I remove the last line I get an error saying:
Cannot convert value of type 'Any?' to expected argument type
'(([Any]?, MSOrcError?) -> Void)!'
client.me.events.read(callback: Any?{
(list: Array<AnyObject>!, error: MSOrcError!) -> Void in
} as! (([Any]?, MSOrcError?) -> Void)! )
How do I solve this error?

The error message says that the expected argument type is (([Any]?, MSOrcError?) -> Void)!. Why don't you follow it?
client.me.events.read(callback: {(list: [Any]?, error: MSOrcError?) in
//...
})
Or simply:
client.me.events.read {list, error in
//...
}

Related

How can I throw and handle error based on data in Swift?

Code :
enum PwdError : Error
{
case obvious;
}
func chkPwd(_ pwd : String) throws -> Bool
{
if(pwd == "pwd")
{
throw PwdError.obvious;
}
return true;
}
print(chkPwd("pwd"));
The output from REPL.it :
Swift version 5.0.1 (swift-5.0.1-RELEASE)
 swiftc -o main main.swift
main.swift:10:5: error: expected expression
throws PwdError.obvious;
^
main.swift:16:7: error: call can throw but is not marked with 'try'
print(chkPwd("pwd"));
^~~~~~~~~~~~~
main.swift:16:7: note: did you mean to use 'try'?
print(chkPwd("pwd"));
^
try
main.swift:16:7: note: did you mean to handle error as optional value?
print(chkPwd("pwd"));
^
try?
main.swift:16:7: note: did you mean to disable error propagation?
print(chkPwd("pwd"));
^
try!
compiler exit status 1
In the above code, I'm trying to handle an error, but all I'm getting are errors. I'm learning Swift, so I'm new to it. I do work in Java as of now, so if someone explains in terms of Java, that would be awesome for me.
You have to invoke the function using try keyword
do {
try chkPwd("pwd")
}
catch {
print(error) // prints obvious
}

Pinterest iOS SDK w/ Swift always returns "Cant convert value of type '(PDKResponseObject) -> ()' to expected argument type 'PDKResponseObject!'"

I'm trying to build a Pinterest iOS clone as an exercise -- first day using xcode and swift. I can't get any function that uses the Pinterest SDK to work properly. Every function gives a Type Error.
For example, here's the code I'm using for login:
#IBAction func login(_ sender: UIButton) {
PDKClient.sharedInstance().authenticate(
withPermissions: [PDKClientReadPublicPermissions],
from: self,
withSuccess: { (success: PDKClientSuccess!) in
print(success)
},
andFailure: { (failure: PDKClientFailure!) in
print(failure)
}
)
}
I get back the following error:
Cannot convert value of type '(PDKClientSuccess!) -> ()' to expected argument type 'PDKClientSuccess!'
If I do withSuccess: PDKClientSuccess! then I get the following error:
ViewController.swift:28:26: Cannot convert value of type 'PDKClientSuccess!.Type' (aka'ImplicitlyUnwrappedOptional<(Optional<PDKResponseObject>) -> ()>.Type') to expected argument type 'PDKClientSuccess!'
If I just do withSuccess: { (PDKClientSuccess) in print("success") } xcode doesn't complain, but it also doesn't work properly.
The documentation seems to be a bit outdated, but here's how they expect the function to look:
- (void)authenticateWithPermissions:(NSArray *)permissions
fromViewController:(UIViewController *)presentingViewController
withSuccess:(PDKClientSuccess)successBlock
andFailure:(PDKClientFailure)failureBlock;
Though based on the error messages I'm getting from xcode, the function is now called 'authenticate', 'withPermissions' is a parameter, and 'fromViewController' is now just 'from'.
So the issue was I was passing a function when it's expecting a closure. So the TypeError makes sense.
The correct code is:
PDKClient.sharedInstance().authenticate(
withPermissions: [PDKClientReadPublicPermissions],
from: self,
withSuccess: { (success) in print(success ?? "succeeds") },
andFailure: { (failure) in print(failure ?? "fails") }
)
Unfortunately this hasn't solved everything... I never hit the withSuccess or withFailure, but at least xcode doesn't complain.

Error testing a method that throws an Error in Quick with Nimble

I'm having a problem getting a Nimble matcher correct in testing a method that throws an exception. According to the docs it should be simple. I just need an expectation like this
expect( try somethingThatThrows() ).toNot( throwError() )
However with Swift 3 and Xcode 8.2 I'm getting a compiler editor. Here's the context.
describe("Using RealmDatasource") {
let datastore = RealmDatasource() as Datasource
it("can retrieve an object") {
expect( try datastore.getCurrentObject() ).to( throwError() )
}
}
I get the following error on the 'it' declaration line
Invalid conversion from throwing function of type '() -> () throws to non-throwing function of type '() -> ()'
try using expect with curly brackets { }
expect { try datastore.getCurrentObject() }.to( throwError() )
should be working

Calling Objective C method with completion handler from Swift3 (completion is IUO?)

so I'm finally getting around to doing my Swift3 conversion. I'm getting MANY of the following errors since we have a legacy codebase that was written in ObjC.
The ObjC definition is here:
-(void)getRecommendationHintsWithCompletion:(void(^)(NSArray *recommendationHints, NSError *error))completion;
in Swift 2.2, we called it like this:
manager.getRecommendationHints { (hints:[AnyObject]!, error: NSError!) in
//code
})
After the swift 3 migrator ran, that line of swift code wasn't changed, but I got the error:
Cannot convert value of type '([AnyObject]!, NSError!) -> ()' to expected argument type '(([Any]?, Error?) -> Void)!'
So I tried:
manager.getRecommendationHints { (hints:[Any]?, error: Error?) in
//code
})
But I still get:
Cannot convert value of type '([AnyObject]?, Error?) -> ()' to expected argument type '(([Any]?, Error?) -> Void)!'
It looks like there's a Implicity Unwrapped Optional on the expected argument, but I'm not sure how to deal with that.
What should I do? Thanks!
Try using Error instead of NSError
manager.getRecommendationHints { (hints:[Any]?, error: Error?) in
//code
}
Hopefully this will solve your problem

'Cannot create a variadic tuple' error

Here's a function from the Dollar framework for Swift:
public class func bind<T, E>(function: (T...) -> E, _ parameters: T...) -> (() -> E) {
return { () -> E in
typealias TType = (T...)
return function(unsafeBitCast(parameters, TType.self))
}
}
In the line with the typealias I get the Cannot create a variadic tuple error. When I remove the braces around T... then I receive the Consecutive statements on a line must be separated by ';' error. So, this is not a solution.
Does anyone know a workaround to get the error away?
This happens since XCode 6 Beta 6, which was (really) released today.
This is fixed in the Dollar project now. But for someone who encounters this issue in another project the way to solve it is by doing a unsafeBitCast on the function itself as such which will resolve the compile issue.
typealias Function = [T] -> E
let f = unsafeBitCast(function, Function.self)
f(params)