Signing Up Parse User with Swift 1.2 - parse-platform

I use the signing up feature of Parse.com just as describe here. Here's my code:
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.
}
}
}
Unfortunately, I've updated my project from swift 1.1 to swift 1.2 and get the following compiler error:
Function signature '(Bool!, NSError!)->void is not compatible with
excepted type '#objc_block (Bool,NSError!)->Void'
it's on the following line:
user.signUpInBackgroundWithBlock {
(succeeded: Bool!, error: NSError!) -> Void in
Does anybody know how can I fox that ? Thanks !

Your succeeded variable is a 'Bool!' but what the block returns is a 'Bool' (without exclamation mark).
The solution would be:
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.
}
}
}
Too see more about optionals go to the apple doc

I had the same problem with save in background with block. It looks like parse returns a "Bool not a Bool!"...however error is an NSError? unless you "!" it.
something.saveInBackgroundWithBlock {
(succeeded: Bool, error: NSError?) -> Void in
code
}

Related

AFNetworking 3 and Swift 2- Cannot convert value NSURLSessionDataTask

When trying to access the GET function from AFHTTPSessionManager from , I get error, inputing the operation:
"Cannot convert value of type '(NSURLSessionDataTask!, AnyObject!) -> Void' to expected argument type '((NSURLSessionDataTask, AnyObject?) -> Void)?'"
return self.GET("search", parameters: parameters, success: {(operation:NSURLSessionDataTask!, response:AnyObject!) -> Void in
let dictionaries = response["businesses"] as? [NSDictionary]
if dictionaries != nil {
completion(Business.businesses(array: dictionaries!), nil)
self.appDelegate.businessesLoaded = true
}
}, failure: { (operation: AFHTTPSessionManager?, error: NSError!) -> Void in
completion(nil, error)
})!
AFHTTPSessionManager.m GET function:
- (NSURLSessionDataTask *)GET:(NSString *)URLString
parameters:(id)parameters
success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
{
return [self GET:URLString parameters:parameters progress:nil success:success failure:failure];
}
This all used to work with AFHTTPRequestOperation, which is no longer part of AFNetworking 3 from deprecated NSURLConnection. Hope someone can shed some light on this and thank you very much in advance!

logInWithReadPermissions(_:handler:)' is deprecated: use logInWithReadPermissions:fromViewController:handler: instead

Using the latest XCode, I'm getting this error:
'logInWithReadPermissions(_:handler:)' is deprecated:
use logInWithReadPermissions:fromViewController:handler: instead'
How would I alternatively re-format my code? here is the whole function that it is in:
#IBAction func fbBtnPressed(sender: UIButton!) {
let facebookLogin = FBSDKLoginManager()
facebookLogin.logInWithReadPermissions(["email"]) {
(facebookResult: FBSDKLoginManagerLoginResult!,facebookError: NSError!) in
print("Facebook login failed. Error \(facebookError)")
}
}
Xcode 8.2 beta (8C30a) :
fbLoginManager.logIn(withReadPermissions:["email"], from: self, handler: {
(result, error) -> Void in
if (error == nil){
let fbloginresult : FBSDKLoginManagerLoginResult? = result
if(fbloginresult?.isCancelled)! {
//Show Cancel alert
} else if(fbloginresult?.grantedPermissions.contains("email"))! {
//self.returnUserData()
//fbLoginManager.logOut()
}
}
})
Figured it out guys! If anyone is lurking on this post, here is the new code:
#IBAction func fbBtnPressed(sender: UIButton!) {
let facebookLogin = FBSDKLoginManager()
facebookLogin.logInWithReadPermissions(["email"], fromViewController: self) { (facebookResult: FBSDKLoginManagerLoginResult!, facebookError: NSError!) -> Void in
print("Facebook login failed. Error \(facebookError)")
}
}
If your fbBtnPressed function is in a view controlle class, just pass self to the fromViewController parameter.
facebookLogin.logInWithReadPermissions(["email"], fromViewController: self) { ... }
A note though, it's encouraged in Swift and Obj-C that your function names prioritize readability over being compact. For example, I would name your button handler facebookLoginButtonPressed. It's longer but much more readable.

Xcode Error: Command failed due to signal: Segmentation fault: 11

I am getting an error "Command failed due to signal: Segmentation fault: 11" at runtime. I believe this is a compile error because when I clean my code it does not have any errors, only when I run & build. I am using the new Xcode beta 7.1. I also built it through my Xcode 7.0 but received same error.
The Swift code has previously built with no errors. This code is dealing with a Parse backend and querying some info to display to a user; here is the logs below:
1. While type-checking 'viewDidLoad' at /Users/User/Documents/Documents/ProjectName/UserProfile.swift:27:14
2. While type-checking expression at [/Users/User/Documents/Documents/ProjectName/UserProfile.swift:34:9 - line:55:9] RangeText="query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects as? [PFObject] {
for object in objects {
let userImageFile: PFFile = object.objectForKey("ProfPhoto") as! PFFile
userImageFile.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
self.ProfileImage.image = UIImage(data:imageData)
}
}
}
}
}
} else {
// Log details of the failure
print("Error: \(error!) \(error!.userInfo)")
}
}"
3. While type-checking expression at [/Users/User/Documents/Documents/ProjectName/UserProfile.swift:34:9 - line:55:9] RangeText="query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects as? [PFObject] {
for object in objects {
let userImageFile: PFFile = object.objectForKey("ProfPhoto") as! PFFile
userImageFile.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
self.ProfileImage.image = UIImage(data:imageData)
}
}
}
}
}
} else {
// Log details of the failure
print("Error: \(error!) \(error!.userInfo)")
}
}"
4. While type-checking expression at [/Users/User/Documents/Documents/ProjectName/UserProfile.swift:34:48 - line:55:9] RangeText="{
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects as? [PFObject] {
for object in objects {
let userImageFile: PFFile = object.objectForKey("ProfPhoto") as! PFFile
userImageFile.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
self.ProfileImage.image = UIImage(data:imageData)
}
}
}
}
}
} else {
// Log details of the failure
print("Error: \(error!) \(error!.userInfo)")
}
}"
I've searched why The error: "Command failed due to signal: Segmentation fault: 11" is causing problems in my app... My app is Parse dependent. I found out that Parse made changes to method:
query.findObjectsInBackgroundWithBlock({ (objects : [AnyObject]?, error : NSError?) -> Void in
to
query.findObjectsInBackgroundWithBlock({ (objects : [**PFObject**]?, error : NSError?) -> Void in
I've changed it all, and now it works. Hope this will help someone using Parse. Thanks to user Babac.
The good answer for this question is to change the methode :
query.findObjectsInBackgroundWithBlock({ (objects : [PFObject]?, error : NSError?) -> Void in
to this :
query.findObjectsInBackgroundWithBlock({ (objects : [AnyObject]?, error : NSError?) -> Void in
because the only thing to change is [PFObject]? to [AnyObject]? then your code will work fine ;-)

SWIFT + Parse signUpInBackgroundWithBlock no longer works: Xcode 6.3.1 [duplicate]

This question already has answers here:
Parse SDK methods not working in Xcode 6.3 Beta
(5 answers)
Closed 7 years ago.
I'm using the latest parse code from the parse.com for user.signupInBackgroundWithBlock
user.signUpInBackgroundWithBlock {
(succeeded: Bool?, error: NSError?) -> Void in
if let error = error {
let errorString = error.userInfo?["error"] as? NSString
self.showAlertWithText(message: "\(error)")
} else {
self.performSegueWithIdentifier("createNewUserAndGoToDashboard", sender: self)
}
I just upgraded to x-code 6.3.1 and it no longer works. This is copied directly from Parse.com, but I'm getting an error on the user.signUp line:
1.0/SIgnUpViewController.swift:48:46: Function signature '(Bool?, NSError?) -> Void' is not compatible with expected type
'#objc_block (Bool, NSError!) -> Void'
any tips?
have you tried it without the "?" after the Bool
user.signUpInBackgroundWithBlock {
(succeeded: Bool, error: NSError?) -> Void in
if let error = error {
let errorString = error.userInfo?["error"] as? NSString
self.showAlertWithText(message: "\(error)")
} else {
self.performSegueWithIdentifier("createNewUserAndGoToDashboard", sender: self) }
Try this.
user.signUpInBackgroundWithBlock { (returnedResult, returnedError) -> Void in
if returnedError == nil
{
self.dismissViewControllerAnimated(true, completion: nil)
}
else
{
self.showAlert("There was an error with your sign up", error: returnedError!)
}
}

Parse/Swift getObjectInBackgroundWithId query not working

I am trying to run the query to get object in background with ID but when I run the method query.getObjectInBackgroundWithId , I am getting the error message:
"Cannot invoke 'getObjectInBackgroundWithId' with an argument list of type (string, block: (PFObject!,NSError?) -> Void"
The same thing happens when I use user.signUpInBackgroundWithBlock so I'm thinking maybe Xcode updated some of the features while using 'block's and now maybe the syntax is different? Any ideas?
Here's a snippet of my code:
http://imgur.com/1CvfhbU
YES!!! Thank you!
The new sample code for getObject is:
query.getObjectInBackgroundWithId("Oaq79bhv53") {
(gameScore: PFObject?, error: NSError?) -> Void in
if error == nil && gameScore != nil {
println(gameScore)
} else {
println("error")
}
}
I figured it out!
user.signUpInBackgroundWithBlock{(succeeded: Bool, signUpError: NSError?) -> Void in
Swift 4.2:
query.getObjectInBackground(withId: "YourId") { (gameScore, error) in
if error == nil {
// Success!
print(gameScore)
} else {
// Fail!
}
}

Resources