Questions tagged [do-catch]

29 questions
1
vote
2 answers

do try catch swift 2

HI i am a little confused about how to transfer if_else error handling to do try catch successfully. Here is my code. let error : NSError? if(managedObjectContext!.save()) { …
J.Chang
  • 35
  • 5
0
votes
1 answer

Why is code execution not leaving Task’s do block? Swift

Update: I've posted an image of the stack trace and the text for the full stack trace (gotten after pausing the program, and typing "bt" into the terminal/debug console) at the bottom of this post. The return print statements at the bottom of this…
coder
  • 19
  • 4
0
votes
1 answer

What error do I need to throw to for a specific catch block to handle + Swift error handling

I want to add code to throw an error that requires the "catch let printerErrorsecond" and the last catch block to handle an error. I have tried updating the value for the toPrinter parameter to a number of values without any luck. I also tried a…
mrmillmill
  • 36
  • 9
0
votes
1 answer

Best practice for accessing variables from a do-catch block

A standard do-catch block looks like this in Swift: let jsonEncoder = JSONEncoder() do { let file = try jsonEncoder.encode(pets) } catch { return } // want to access file here My question is what is the best practice for accessing the variable…
Carl
  • 683
  • 5
  • 18
0
votes
2 answers

Access variable do-catch statement on Swift

I am developing an application that json parse. I'm using the AlertView for json messages. But I can not access the jsonmessage variable in the AlertView. if I put the AlertView in DO I get this error: "libc++abi.dylib: terminating with uncaught…
Fatih Lale
  • 51
  • 1
  • 8
0
votes
1 answer

Catch pattern changes callback signature

I am trying to use JSONDecoder to decode a json response from my server using Alamofire. When I decode the response with a guard, it works without any issues. The side-effect of this approach is that I can't tell what the issue is when the decode…
Johnathon Sullinger
  • 7,097
  • 5
  • 37
  • 102
0
votes
1 answer

Use of unresolved identifier when referencing variable in do/catch block

I’m assigning a variable in a do / catch block, and then trying to reference that variable further down in my file. But when I do, I get the following error in Xcode: Use of unresolved identifier 'captureDeviceInput' This is my code: do { let…
Martin Bean
  • 38,379
  • 25
  • 128
  • 201
0
votes
1 answer

app crashes because of null values, swift 2

I'm trying to get data using JSON ad there is a null value causes crash. let jsonData:NSDictionary = try NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers ) as! NSDictionary let success:NSInteger =…
vahidgr
  • 81
  • 10
0
votes
2 answers

How Properly to Get the Value Outside of the Scope Do-Catch (using Try) in Swift

I am trying to parse JSON data to a dictionary, for parsing I am using the separate method, and later would like to use the results (dictionary) for other operations in another method, not just to print it out as it is given in many examples online,…
Darius Miliauskas
  • 3,391
  • 4
  • 35
  • 53
0
votes
1 answer

Method that returns Void which also throws causes annoying warning in editor

I use this method: public func setCategory(category: String, withOptions options: AVAudioSessionCategoryOptions) throws It's an AVAudioSession method and as you can see it doesnt return anything but it should throw and error. I do like this: try?…
Arbitur
  • 38,684
  • 22
  • 91
  • 128
-1
votes
1 answer

Try Catch in Swift, Can't figure it out

I'm trying to put the following in a do try catch block, but I always end up getting a coding error. Can someone help me? let here = CGRect(x: UIScreen.main.bounds.width - 30, y: 80, width: 10, height: 10) …
ibyte
  • 463
  • 4
  • 17
-1
votes
3 answers

Manually go to catch statement of do...catch

I want to test if an array's count is greater than 0, otherwise dismiss the current view. Right now I'm doing it like this: do { let pets = try self.managedObjectContext.fetch(request) guard pets.count > 0 else { …
MarksCode
  • 8,074
  • 15
  • 64
  • 133
-1
votes
2 answers

Error: 'Call can throw but is not marked with try and error not handled'

I received the error stated above and have tried to amend this by adding in a do / catch block. For some reason the error won't go away. Does anyone know why this might be ? override func viewDidAppear(animated: Bool) { …
Jamie Baker
  • 157
  • 6
-3
votes
1 answer

scope of do-catch in swift - cannot assign value to outside variable

I have made some code to make POST request to my php script which is placed on my servers. I have tested and that part is working fine. I got the problem with the returning result from the server - I get it in JSON format, and print in inside…
1
2