I’m experimenting with Parse in a swift app and running through some of the examples but I can’t get the query to run. I’m getting the “ambiguous without more context” error in XCode. Here’s the code:
var query = PFQuery(className: "MyFirstClass")
query.getObjectInBackground(withId: "Jl0SUJWCQg") {
(myFirstClass: PFObject?, error: NSError?) -> Void in
if error == nil && myFirstClass != nil {
print(myFirstClass)
} else {
print(error)
}
}
Related
I got confused error handling in swift3. I try to do like "if XX function got error then try YY function"
Let me show you what I try:
class MyClass {
enum error: Error
{
case nilString
}
func findURL() {
do {
let opt = try HTTP.GET(url_adr!)
opt.start { response in
if let err = response.error {
print("error: \(err.localizedDescription)")
return //also notify app of failure as needed
}
do
{
/* This is func1. and got error. I want to if this function has error then go next function. */
try self.stringOperation(data: response.description)
}
catch{
print("doesn't work on func1. trying 2nd func")
self.stringOperation2(data:response.descritption)
}
}
} catch let error {
print("got an error creating the request: \(error)")
}
}
func stringOperation(data:String)throws -> Bool{
do{
/** 1 **/
if let _:String = try! data.substring(from: data.index(of: "var sources2")!){
print("its done")
}else{
throw error.nilString
}
IN 1: I got this fatal error on this line:
"fatal error: unexpectedly found nil while unwrapping an Optional value" and program crashed.
I googled error handling try to understand and apply to in my code. However not succeed yet. Can someone explain where did I wrong?
Additional info: I got String extension for .substring(from:...) , and .index(of:"str"). So these lines doesn't got you confused.
As a general rule, try avoiding using force unwrapping (!), where you have
if let _: String= try! data.substring...
Instead use
if let index = data.index(of: "var sources2"),
let _: String = try? data.substring(from: index) { ... } else { ... }
That way you remove the two force unwraps that may be causing your crash. You already have the if let protection for catching the nil value, so you can make the most of it by using the conditional unwrapping.
I am New for Swift and I Have Implement File Manager Concept in my Project but it shows the issue and I don't for how to solve this please any body help me for fix the issue.
Here I Post My Code.
class func addSkipBackupAttributeToItemAtPath(filePathString: String) throws -> Bool
{
let URL: NSURL = NSURL.fileURLWithPath(filePathString)
assert(NSFileManager.defaultManager().fileExistsAtPath(URL.path!))
let err: NSError? = nil
let success: Bool = try URL.setResourceValue(Int(true), forKey: NSURLIsExcludedFromBackupKey) //---- This Line Shows the Issue.
if !success {
NSLog("Error excluding %# from backup %#", URL.lastPathComponent!, err!)
}
return success
}
The benefit of the new error handling in Swift 2 is the omission of the quasi-redundant return values Bool / NSError. Therefore setResourceValue does not return a Bool anymore which is the reason of the error message.
As the function is marked as throws I recommend this syntax which just passes the result of setResourceValue
class func addSkipBackupAttributeToItemAtPath(filePathString: String) throws
{
let url = NSURL.fileURLWithPath(filePathString)
assert(NSFileManager.defaultManager().fileExistsAtPath(URL.path!))
try url.setResourceValue(true, forKey: NSURLIsExcludedFromBackupKey)
}
Handle the error in the method which calls addSkipBackupAttributeToItemAtPath
The method setResourceValue is a throw function and does not return a Bool.
Try running your function using a do-catch:
do {
try URL.setResourceValue(Int(true), forKey: NSURLIsExcludedFromBackupKey)
}
catch {
NSLog("Error excluding %# from backup %#", URL.lastPathComponent!, err!)
}
I have searched through a number of similar topics but have not found a solution as of yet. I am using Parse social and using the login files.
I get the following error:
"AnyObject?" is not convertible to 'String'
I am very new to Swift & Parse - I believe this is the correct method of retrieving data, so please correct me if I am wrong.
var userObjectID = PFUser.currentUser()!.objectId!
var query = PFQuery(className:"User")
query.getObjectInBackgroundWithId("\(userObjectID)") {
(userInfo: PFObject?, error: NSError?) -> Void in
if error == nil && userInfo != nil {
println(userInfo)
let userScore = userInfo["level"] as! String
} else {
println(error)
}
}
Below is the database on Parse
I think you need to unwrap the PFObject you receive:
let userScore = userInfo!["level"] as! String
I have the following statement:
func sessionStateChanged(session:FBSession, state:FBSessionState, error:NSError?) {
// If the session was opened successfully
if error == nil && state == FBSessionStateOpen {
println("Session opened");
}
}
which has the following error on the line of the if-statement:
Type NSError? does not conform to protocol '_RawOptionSetType'
However if I remove the second condition, so the code reads:
if error == nil {
println("Session opened");
}
There is no error. This leads me to believe tat the NSError? type is not the issue and that it has something to do with the multiple conditions.
One way to get clearer error messages might be to start a new if statement inside the first rather than combining with && .
FBRequestConnection.startForPostOpenGraphObject(graphObject, {connection, result, error in
if(!error) {
var objectID : NSString = result.objectforKey("id")
println(objectID)
} else {
println(error.description)
}
})
I'm getting the following error on the expression:
Cannot convert the expression's type 'FBRequestConnection!' to 'Void'
I have looked all over SO but couldn't find a solution. Any ideas?
Help would be much appreciated. Thanks.
Maybe check your handler syntax. It should be something like:
let handler:FBRequestHandler = { (connection : FBRequestConnection!, result : AnyObject!, error : NSError!) -> Void in
if (error != nil) {
println(error)
} else {
println(result)
}
}
Myself, I'm struggling with getting FBOpenGraphObject from FBGraphObject.openGraphObjectForPost()