Swift 2: Call can throw, but it is not marked with 'try' and the error is not handled
You have to catch the error just as you're already doing for your save()
call and since you're handling multiple errors here, you can try
multiple calls sequentially in a single do-catch block, like so:
func deleteAccountDetail() {
let entityDescription = NSEntityDescription.entityForName("AccountDetail", inManagedObjectContext: Context!)
let request = NSFetchRequest()
request.entity = entityDescription
do {
let fetchedEntities = try self.Context!.executeFetchRequest(request) as! [AccountDetail]
for entity in fetchedEntities {
self.Context!.deleteObject(entity)
}
try self.Context!.save()
} catch {
print(error)
}
}
Or as @bames53 pointed out in the comments below, it is often better practice not to catch the error where it was thrown. You can mark the method as throws
then try
to call the method. For example:
func deleteAccountDetail() throws {
let entityDescription = NSEntityDescription.entityForName("AccountDetail", inManagedObjectContext: Context!)
let request = NSFetchRequest()
request.entity = entityDescription
let fetchedEntities = try Context.executeFetchRequest(request) as! [AccountDetail]
for entity in fetchedEntities {
self.Context!.deleteObject(entity)
}
try self.Context!.save()
}
Call can throw, but it is not marked with 'try' and the error is not handled
SwiftyJSON's JSON(data:)
can throw an exception so you have to mark it with try
.
Strict solution:
.map { (data) in
do {
return try JSON(data: data)
}
catch {
fatalError("unable to convert data to JSON")
}
}
Loose solution:
.compactMap { try? JSON(data: $0) }
Error Call can throw, but is not marked with 'try' and the error is not handled
Swift 2.0 introduces error handling. The error indicates that logInWithUsername:password:
can potentially throw an error, and you must do something with that error. You have one of a few options:
Mark your checkUserCredentials()
functional as throws
and propagate the error to the caller:
func checkUserCredentials() throws -> Bool {
try PFUser.logInWithUsername(userName!, password: password!)
if (PFUser.currentUser() != nil) {
return true
}
return false
}
Use do
/catch
syntax to catch the potential error:
func checkUserCredentials() -> Bool {
do {
try PFUser.logInWithUsername(userName!, password: password!)
}
catch _ {
// Error handling
}
if (PFUser.currentUser() != nil) {
return true
}
return false
}
Use the try!
keyword to have the program trap if an error is thrown, this is only appropriate if you know for a fact the function will never throw given the current circumstances - similar to using !
to force unwrap an optional (seems unlikely given the method name):
func checkUserCredentials() -> Bool {
try! PFUser.logInWithUsername(userName!, password: password!)
if (PFUser.currentUser() != nil) {
return true
}
return false
}
IOS: Call can throw, but is not marked with 'try' and the error is not handled
What you want to do is the following.
var json: [AnyHashable:Any]?
do {
json = try JSON(data: data)
} catch _ {
json = nil
}
What xcode is trying to say is that calling the JSON(data: data)
can throw an error and you should put it inside a try-catch
Call can throw, but it is not marked with 'try' and the error is not handled / Initializer for conditional binding must have Optional type, not 'JSON'
It would seem that the JSON
initialiser throws
, so the compiler is telling you you need to handle this.
You are also trying to access local variables declare in the .success
code block outside of that block, so they are unknown. You also have the wrong name for the id
variable.
AF.request(URL_USER_ADD, method: .post, parameters: body, encoding: JSONEncoding.default, headers: header).responseJSON { (response) in
switch response.result {
case .success(let result):
if let json = result as? Data {
guard let data = response.data else { return }
do {
let json = try JSON(data: data)
let id = json["_id"].stringValue
let color = json["avatarColor"].stringValue
let avatarName = json["avatarName"].stringValue
let email = json["email"].stringValue
let name = json["name"].stringValue
UserDataService.instance.setUserData(id: id, color: avatarColor, avatarName: avatarName, email: email, name: name)
completion(true)
} catch {
print(error)
completion(false)
}
}
case .failure(let result):
completion(false)
debugPrint(response.result as Any)
}
}
}
}
As a point of style, a completion handler that simply returns true
or false
isn't particularly useful as it doesn't provide any information on what went wrong when false
is returned, but since you mention a lesson, I will assume that this isn't your design choice.
swiftyjson - Call can throw, but it is marked with 'try' and the error is not handled
You should wrap it into a do-catch
block. In your case:
do {
let session = URLSession.shared.dataTask(with: url) {
(data, response, error) in
guard let data = data else {
print ("data was nil?")
return
}
let json = JSON(data: data)
print(json)
}
} catch let error as NSError {
// error
}
Swift ERROR : : Call can throw, but it is not marked with 'try' and the error is not handled
The compiler correctly tells you that the initializer can throw an error as the method declaration correctly states (e.g. when the NSURL
does not exist):
init(contentsOfURL url: NSURL) throws
Therefore you as the developer have to handle an eventually occuring error:
do {
let buttonAudioPlayer = try AVAudioPlayer(contentsOfURL: buttonAudioURL)
} catch let error {
print("error occured \(error)")
}
Alternatively you can tell the compiler that the call will actually always succeed via
let buttonAudioPlayer = try! AVAudioPlayer(contentsOfURL: buttonAudioURL)
But that will cause your app to crash if the creation actually does not succeed - be careful. You only should use the second approach if you are trying to load app resources that have to be there.
Related Topics
Scaling Physics Bodies in Xcode Spritekit
Drag Uibutton Without It Shifting to Center [Swift 3]
Swift - Update/Refresh Label That Displays Time
Writing JSON File Programmatically Swift
How Does Uibutton Addtarget Self Work
How to Remove Single Object in Array from Multiple Matching Object
Swift:'(Nsobject, Anyobject)' Does Not Have a Member Named 'Subscript'
Which View Controller Should I Pass in My Swift Native Code for a Flutter Plugin
How to Add the Same Background Image to All Views in My iOS App Using Swift
Exception: Cannot Manually Set the Delegate on a Uinavigationbar Managed by a Controller
How to Access Property or Method from a Variable
Swift 2 - Separating an Array into a Dictionary with Keys from a to Z
Swift: Triggering Tableviewcell to Lead to a Link in a Uiwebview in Another Viewcontroller
Avaudioplayer Working on Simulator But Not on Real Device
Swift: Triggering Tableviewcell to Lead to a Link in a Uiwebview in Another Viewcontroller
Swiftui Animation and Subsequent Reverse Animation to Original State