Parse SDK methods not working in Xcode 6.3 Beta - ios

So far I am having issues with blocks like this:
user.signUpInBackgroundWithBlock {
(succeeded: Bool!, error: NSError!) -> Void in
if error == nil {
println("success")
} else {
println("\(error)");
// Show the errorString somewhere and let the user try again.
}
}
When I add this into Xcode I get this:
Cannot invoke 'signUpInBackgroundWithBlock' with an argument list of type '((Bool!, NSError!) -> Void)'
When I run this code in Xcode 6.3 (non beta) it works fine. But in the Beta it fails and wont allow me to build. Any ideas if this will be cleared up or if there is a different implementation that I could use. Ive tried using just the signUpInBackgroundWithTarget but Im just not able to access the error correctly if one is received.

be sure you are using SDK version 1.7.1, then removing the types from your closure should do the trick:
user.signUpInBackgroundWithBlock { (succeeded, error) -> Void in
if error == nil {
println("success")
} else {
println("\(error)");
// Show the errorString somewhere and let the user try again.
}
}

Due to the new addition of "Nullability Annotations" to Swift 1.2, you have to rewrite the code above like this (using Parse 1.7.1+):
user.signUpInBackgroundWithBlock { (succeeded: Bool, error: NSError?) -> Void in
if let error = error {
println(error) // there is an error, print it
} else {
if succeeded {
println("success")
} else {
println("failed")
}
}
}
Parse is now returning optionals (?) instead of explicitely unwrapped objects (!).

Notation of Swift is changed
class AAPLList : NSObject, NSCoding, NSCopying {
// ...
func itemWithName(name: String!) -> AAPLListItem!
func indexOfItem(item: AAPLListItem!) -> Int
#NSCopying var name: String! { get set }
#NSCopying var allItems: [AnyObject]! { get }
// ...
}
After annotations:
class AAPLList : NSObject, NSCoding, NSCopying {
// ...
func itemWithName(name: String) -> AAPLListItem?
func indexOfItem(item: AAPLListItem) -> Int
#NSCopying var name: String? { get set }
#NSCopying var allItems: [AnyObject] { get }
// ...
}
So you can change
(succeeded: Bool!, error: NSError!) -> Void in
to
(success: Bool, error: NSError?) -> Void in

Which Parse SDK are you using? They released version 1.7.1 a few days ago that should fix your issue.

Change:
(succeeded: Bool!, error: NSError!) -> Void in
to
(succeeded, error) -> Void in
This change is required due to changes in the Parse SDK

Related

Unable to perform functions from Particle iOS Cloud SDK after downloading Xcode 8 / Swift 3.0

Prior to downloading Xcode 8 I was able to perform functions from the Particle iOS Cloud SDK (Spark SDK) without any problem. Now, I am being given multiple errors all similar to the ones below.
SparkCloud.sharedInstance().loginWithUser(username!, password: password!) { (error:NSError?) -> Void in
// Deactivates activity indicator.
activityIndicator.stopAnimating()
// Reallows interaction events.
UIApplication.sharedApplication().endIgnoringInteractionEvents()
if error != nil {
Utils.showAlertOnVC(self, title: "Invalid parameters.", message: "Please try again.")
self.clearText()
} else {
self.performSegueWithIdentifier("loginUser", sender: self)
}
}
Error: Cannot convert value of type '(NSError?) -> Void' to expected argument type 'SparkCompletionBlock?'
SparkCloud.sharedInstance().getDevices { (sparkDevices: [AnyObject]?, error: NSError?) -> Void in
if let sparkDevices = sparkDevices as? [SparkDevice] {
for device in sparkDevices {
self.myPhotons.append(device)
self.tableView.reloadData()
}
}
}
Error: Cannot convert value of type '([AnyObject]?, NSError?) -> Void' to expected argument type '(([Any]?, Error?) -> Void)?'
I've tried updating my Podfile and toying with the function calls but nothing seems to work. I'm wondering if it has something to do with the updates in Swift 3.0 but I can't find anything that would indicate so. Any help with this issue would be much appreciated.
Instead of NSError, use Error:
SparkCloud.sharedInstance().getDevices { (sparkDevices: [AnyObject]?, error: Error?)
This did the trick for me.

Parse signup error with swift 2 (xcode 7 beta 5)

I am using Parse and I have a signup page where I call:
user.signUpInBackgroundWithBlock { (succeeded: Bool, error: NSError?) -> Void in
I checked and this works on previous versions of Xcode, however there was a similar problem when Swift 1.2 came out, though it doesn't solve my problem.
The error I get is:
Cannot invoke 'signupInBackgroundWithBlock' with an argument list of type: '((Bool, NSError?) -> Void )'
I'd be grateful for any help.
You should change the type of succeeded value to ObjCBool. The signature of PFBooleanResultBlock now changed (Bool, NSError?) -> Void to (ObjCBool, NSError?) -> Void
So you should change the type Bool to ObjCBool, like below:
user.signUpInBackgroundWithBlock { (succeeded: ObjCBool, error: NSError?) -> Void in
print(succeeded)
print(error)
}
or just remove the type to make the compiler inferring the type.
user.signUpInBackgroundWithBlock { (succeeded, error) -> Void in
print(succeeded)
print(error)
}

The significance of the Bool value of PFUser signUpInBackgroundWithBlock

I am wondering what is the significance of the succeeded Bool value in the function block? As I have seen sample codes from parse ignores it and only check if error is nil or not.
If the bool value is not required, why is it there in the first place?
user.signUpInBackgroundWithBlock({ (succeeded: Bool, error: NSError?) -> Void in
if error == nil {
if succeeded { // IS THIS REQUIRED AT ALL??
}
}
else {
}
});
Or we could just do this?
user.signUpInBackgroundWithBlock({ (succeeded: Bool, error: NSError?) -> Void in
if error == nil {
//Do something
}
else {
}
});

Global handling of 401 Responses with RestKit and Swift

I am currently working on an iOS App that is developed using Swift. For the REST calls, I am using the RestKit framework.
The next stage of my project is to start using authentication against the services. An issue that I have come up against is handling 401 (Not Authenticated) responses from the service. In all of these circumstances, I would like to display a login page. I want to avoid implementing the error handling of this multiple times.
I followed the tutorial at http://blog.higgsboson.tk/2013/09/03/global-request-management-with-restkit/. However, this is in Objective-C and I would like to do things slightly differently.
As such, I want to build a class that extends RKObjectRequestOperation as in the tutorial but using Swift. I have come up with an issue as I am receiving the error
Overriding method with selector 'setCompletionBlockWithSuccess:failure:' has incompatible type '((RKObjectRequestOperation, RKMappingResult) -> Void (RKObjectRequestOperation, NSError) -> Void) -> Void'
I am a bit stuck on this and so was hoping someone could help. The code for the method that is failing is below.
class CustomRequestOperation : RKObjectRequestOperation {
func setCompletionBlockWithSuccess(success: (operation: RKObjectRequestOperation, mappingResult: RKMappingResult) -> Void, failure: (operation: RKObjectRequestOperation, error: NSError) -> Void) -> Void {
}
}
Can anyone point out what is wrong with my method signature?
You're overriding the method so you can get Xcode to add the signature for you if you start typing the method name and hie escape.
It should be
func setCompletionBlockWithSuccess(success: (operation: RKObjectRequestOperation, mappingResult: RKMappingResult) -> Void, failure: (operation: RKObjectRequestOperation, error: NSError) -> Void) {
(you are adding a return spec that doesn't exist in the superclass method)
Here is the full class in swift...
class CustomRKObjectRequestOperation : RKObjectRequestOperation
{
override func setCompletionBlockWithSuccess(success: ((RKObjectRequestOperation!, RKMappingResult!) -> Void)!, failure: ((RKObjectRequestOperation!, NSError!) -> Void)!) {
super.setCompletionBlockWithSuccess({ (operation, RKMappingResult mappingResult) -> Void in
if ((success) != nil) {
success(operation, mappingResult);
}
}, failure: { (RKObjectRequestOperation operation, NSError error) -> Void in
NSNotificationCenter.defaultCenter().postNotificationName("connectionFailure",object:operation)
if ((failure) != nil) {
failure(operation, error);
}
})
}
}
App Delegate
Register notification
NSNotificationCenter.defaultCenter().addObserver(self, selector:"connectionFailedWithOperation:",name:"connectionFailure", object:nil)
func connectionFailedWithOperation(notification: NSNotification ){
let operation = notification.object as! RKObjectRequestOperation?;
if ((operation) != nil) {
let statusCode = operation!.HTTPRequestOperation.response.statusCode;
if (statusCode == 401) {
// log user out
}
}
}

Parse Objective - C to Swift iOS

I am having trouble trying to change Objective - C code to Swift. This is with the Parse framework. If anyone knows how the following code should be written in Swift, it would help me a lot.
[user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
//The registration was successful, go to the wall
[self performSegueWithIdentifier:#"SignupSuccesful" sender:self];
}
[NSObject: Anyobject]? does not have a member named subscript parse compile error will be thrown.
The code should be like that because userInfor maybe be nil.
user.signUpInBackgroundWithBlock {
(succeeded: Bool!, error: NSError!) -> Void in
if !(error != nil) {
// Hooray! Let them use the app now.
} else {
if let errorString = error.userInfo?["error"] as? NSString {
println(errorString)
}
}
}
Parse happens to have an example of this exact method in their documentation. I think they'll be OK if I excerpt it here:
user.signUpInBackgroundWithBlock {
(succeeded: Bool!, error: NSError!) -> Void in
if !error {
// Hooray! Let them use the app now.
} else {
let errorString = error.userInfo["error"] as NSString
// Show the errorString somewhere and let the user try again.
}
}
It would be something like this.
user.signUpInBackground {
(success: Bool!, error: NSError!) -> Void in
if !error {
[unowned self] in
self.performSegue("SignupSuccesful", sender:self);
}
Edit: Parse actually has Swift support. Here's a tutorial for it.
Edit2: You shouldn't refer to self inside a block. Use unowned self instead.

Resources