Parse Objective - C to Swift iOS - 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.

Related

Swift Block Syntax with Objective-C Function [Venmo-iOS-SDK]

I'm currently trying to use the Venmo-iOS-SDK for an application I am working on. The SDK is in objective-C, while I'm trying to use it with a swift app.
I'm having trouble translating the syntax of a completion obj-c block to swift. I found sample code implementing a function I want to use.
- (IBAction)logInButtonAction:(id)sender {
[[Venmo sharedInstance] requestPermissions:#[VENPermissionMakePayments,
VENPermissionAccessProfile]
withCompletionHandler:^(BOOL success, NSError *error) {
if (success) {
NSLog("Success")
} else {
NSLog("Failure")
}
}];
}
I've tried doing this
#IBAction func loginButtonAction(sender: AnyObject){
Venmo.sharedInstance().requestPermissions([VENPermissionMakePayments, VENPermissionAccessPhone], withCompletionHandler: { (success: Bool, error: NSErrorPointer) -> Void in
if success{
println("Yes")
}else{
println("No")
}
})
}
But get the error
"Cannot invoke 'requestsPermissions with an argument list of type
'([String], withCompletionHandler: (Bool, NSError) -> Void)'
Is this a problem with how i'm translating the block? Or something else. Looking at the Venmo-SDK the obj-C functions are defined like this
- (void)requestPermissions:(NSArray *)permissions withCompletionHandler:(VENOAuthCompletionHandler)handler;
and
- (void)requestPermissions:(NSArray *)permissions withCompletionHandler:(VENOAuthCompletionHandler)handler;
You can write it like this (note the lack of types on the completion handler params):
#IBAction func loginButtonAction(sender: AnyObject) {
Venmo.sharedInstance().requestPermissions([VENPermissionMakePayments, VENPermissionAccessPhone], withCompletionHandler: { (success, error) -> Void in
// code here
})
}
A bit more concise with Swift 2 syntax would be omitting the -> Void and explicit withCompletionHandler: parameter:
#IBAction func loginButtonAction(sender: AnyObject) {
Venmo.sharedInstance().requestPermissions([VENPermissionMakePayments, VENPermissionAccessPhone]) { (success, error) in
// code here
}
}
You'll also want to make sure you change your println statements to print.

Parse Swift Update Errors

After the recent Swift update, I have been trying to debut a few lines of code and don't seem to be understanding what's wrong..
The lines are
PFGeoPoint.geoPointForCurrentLocationInBackground {
with the error message "Cannot invoke 'geoPointForCurrentLocationInBackground' with an argument list of type '((PFGeoPoint", NSError!) -> Void)'"
The second line is
PFUser.logInWithUsernameInBackground(username:usernameTextField.text, password:passwordTextField.text, target: self) {
With the error "Extra argument 'target' in call"
I've tried looking online and debugging these, but I honestly have no idea what's going on. It seems to be an error in the parse code and I'm not sure why that is...
Edit: I fixed second error I was having. code:
PFUser.logInWithUsernameInBackground(usernameTextField.text, password:passwordTextField.text) {
Start from Swift 1.2, the Failable Casts is introduced. you can use the PFGeoPoint.geoPointForCurrentLocationInBackground method like the following:
If you're quite sure that the downcasting will succeed, you can use as! to force the cast:
PFGeoPoint.geoPointForCurrentLocationInBackground {
(point:PFGeoPoint!, error:NSError!) -> Void in
if (error == nil) {
println(point)
} else {
println(error)
}
}
If you're not sure if the casting will succeed, just use the as? operator. By using as?, it returns an optional value, but in case the downcasting fails, the value will be nil.
PFGeoPoint.geoPointForCurrentLocationInBackground {
(point:PFGeoPoint?, error:NSError!) -> Void in
if (error == nil) {
if let myPoint = point {
println(myPoint)
}
} else {
println(error)
}
}

parse sign up method error

I am trying to implement sign up method in my app and now I have problems with parse. i am trying the following:
func signUpSend() {
var user = PFUser()
user.username = email.text
user.password = email.text
user.password = password.text
user.signUpInBackgroundWithBlock {
(succeeded: Bool!, error: NSError!) -> Void in
if error == nil {
// 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.
}
}
}
the signUpInBackgroundWithBlock gives me an error "cannot invoke 'signUpInBackgroundWithBlock' with an argument list of type '((Bool!,NSError!) -> Void)' . I have tried to find answer in the parse docs, but thats exactly the code they advise to use. Anybody knows how to fix this?
Thanks!
Try changing:
(succeeded: Bool!, error: NSError!) -> Void in
to
(succeeded, error) -> Void in
I believe this change is required due to changes in Swift 1.2 update

Swift Parse create a user

I'm new to swift, and I'm having a difficult time figuring out why the code on https://www.parse.com/docs/ios_guide#users/iOS isn't working for me
var user = PFUser()
user.username = "myUsername"
user.password = "myPassword"
user.email = "email#example.com"
// other fields can be set just like with PFObject
user["phone"] = "415-392-0202"
user.signUpInBackgroundWithBlock {
(succeeded: Bool!, error: NSError!) -> Void in
if error == nil {
// 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.
}
}
This line
user.signUpInBackgroundWithBlock
gives me the following error
Cannot invoke 'signUpInBackgroundWithBlock' with an argument list of type '((Bool!, NSError!) -> Void)'
I can copy and paste the code for Objective-C into my project and it works perfectly, but when I try to do the same with the Swift code. I get this error message. Is there something else I need to do to get it to work properly in Swift?
I found the solution in case others are having a similar problem. The code below seems to working just fine. Now I'm just curious to know why parse.com gives the above code in their documentation which doesn't actually work
user.signUpInBackgroundWithBlock {
(succeeded: Bool, error: NSError?) -> Void in
if error == nil {
// Hooray! Let them use the app now.
} else {
}
}

Parse SDK methods not working in Xcode 6.3 Beta

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

Resources