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
Related
The Code below returns an error:
Cannot Covert value of type '(PDUser!, NSError!) -> Void' to expected argument type 'PFUserResultBlock'
PFAnonymousUtils.logIn{ (user: PFUser?, error: NSError?) -> Void in
if let user = user {
if user != nil {
println("Success")
}
} else {
println("Failed")
}
}
I looked at a few examples below for reference to see if there was a solution:
(1)Parse Facebook logInInBackgroundWithReadPermissions
(2)PFFacebookUtils login background cannot convert value to PFUserResultBlock
(3) iOS Developers Guide
The type inference is being thrown off. Try without the types. So PFAnonymousUtils.logIn{ (user, error) in This might fix it.
So With some help here's a complete solution:
Taking a suggestion where I would have to place :
PFAnonymousUtils.logIn{ (user, error) in
over
PFAnonymousUtils.logIn{ (user: PFUser?, error: NSError?) -> Void
the next error would be this line:
if let user = user {
where the compiler assumes that this isn't an optional, so the final solution in code would be
PFAnonymousUtils.logIn{ (user, error) in
if error == nil {
print("Success")
}
else {
print("Failed")
}
}
This way, it checks for an error, if there is an error, the code will print a line indicating as such.
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 {
}
}
I'm working on integrating touchID into my application. The process was fairly simple, but even just using my dummy data, it takes about 5 seconds after it authenticated my fingerprint, before it performs it's task.
Here's my code:
func requestFingerprintAuthentication() {
let context = LAContext()
var authError: NSError?
let authenticationReason: String = "Login"
if context.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error: &authError) {
context.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: authenticationReason, reply: {
(success: Bool, error: NSError?) -> Void in
if success {
println("successfull signin with touchID")
self.emailInputField.text = "john.doe#gmail.com"
self.passwordInputField.text = "password"
self.signIn(self.signInButton)
} else {
println("Unable to Authenticate touchID")
}
})
}
}
even with the dummy data, it takes waaay too long.
When I login normally, by typing the email and the password into my inputfields, the signIn() function runs instantly.
To see if it was a problem with that. I tried replacing that, with 2 lines that simply takes me to the correct viewController. But it still takes several seconds after it's authenticated my fingerprint.
I know it's not the phone, nor touchID. Cause it runs my println("successfull signin with touchID") immediately. It's what comes after that, that for some reason takes several seconds for it to run?
Any help explaining this would be greatly appreciated!
The documentation states:
This method asynchronously evaluates an authentication policy.
You are running UI code on a thread that is not the main. Wrap your code to get it to perform on the main thread:
func requestFingerprintAuthentication() {
let context = LAContext()
var authError: NSError?
let authenticationReason: String = "Login"
if context.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error: &authError) {
context.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: authenticationReason, reply: {
(success: Bool, error: NSError?) -> Void in
if success {
NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
println("successfull signin with touchID")
self.emailInputField.text = "john.doe#gmail.com"
self.passwordInputField.text = "password"
self.signIn(self.signInButton)
})
} else {
println("Unable to Authenticate touchID")
}
})
}
}
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
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.