29 questions
1
vote
2
answers
285
views
How can you also get the error **instance** when do/catching specific error types?
I love when simple things you think will take all of two minutes ends up taking over an hour! 🤦🏻
When writing unit tests, we need to test for several specific error types, some of which have ...
0
votes
1
answer
375
views
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 ...
0
votes
1
answer
2k
views
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 ...
-1
votes
1
answer
253
views
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, ...
1
vote
1
answer
625
views
Best practice for accessing variables from a do-catch block [closed]
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 ...
2
votes
1
answer
44
views
How to get the correct error when hitting a do catch block for a function that throws when String does not conform to Error?
import Foundation
enum ErrorScenarios: Error {
case invalidAge
case invalidEmail
case incorrectData
}
func age(age:Int) throws {
if age < 20 {
throw ErrorScenarios....
0
votes
2
answers
546
views
Access variable do-catch statement on Swift [duplicate]
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:...
5
votes
2
answers
5k
views
NSError and Error in Swift
I've seen that there is already a question on the difference between NSError and Error in Swift and I am aware of the differences. However, I do not understand the behavior of the code snippet below, ...
9
votes
0
answers
3k
views
Error handling for URLSession.shared.datatask
I have a function that queries an API and then populates an array of objects based on the results(see code below). From the JSON retrieved I can use guard statements to catch any error meaning that if ...
4
votes
2
answers
6k
views
Returning String from a do-catch statement
I was trying to translate the code from swift 2 to swift 4 and came across this error
Errors thrown from here are not handled
So I did this but now it tells me to return a string. Any idea how to ...
2
votes
0
answers
31
views
Where to write try statment? [duplicate]
What are the differences between writing try before variable or after it assignment sign? I mean this:
do {
audioPlayer = try AVAudioPlayer(contentsOf: soundUrl!)
} catch {
print(error)
}
and ...
0
votes
1
answer
270
views
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 ...
14
votes
3
answers
5k
views
Nested do catch swift 3.0
I'd like to use consecutive try statements. If one returns an error I'd like to proceed to the next one, otherwise return the value.
The code below seems to work fine, however I'll end up with a big ...
1
vote
2
answers
790
views
Swift How to returning a tuple from a do catch where conditional binding must have optional type?
I am wanting to put a swift 3 do-catch inside a function rather than constantly writing it everywhere I need it; inside this function I wish to return a tuple with a boolean, and an optional error.
I ...
1
vote
3
answers
3k
views
When to use do-catch block using Swift
In the following scenario when reading JSON data from a file, I have the following block of code:
// Fetch URL
let url = Bundle.main.url(forResource: "sampleJSON", withExtension: "json")!
// Load ...
0
votes
1
answer
218
views
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 '...
1
vote
1
answer
1k
views
The do statement [closed]
Let's say this is our function for delete objects from the model:
func delete(indexPath: IndexPath) {
let managedObject = self.fetchedResultsController.object(at: indexPath)
self....
-1
votes
3
answers
425
views
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)
...
1
vote
1
answer
778
views
Do-Catch and overflow [duplicate]
How I can wrap in do catch overflow error
let increasePerSecond: UInt32 = UInt32.max
let offset: UInt32
do {
offset = try ((nowTimeInterval - calculatedTimeInterval) * ...
22
votes
6
answers
14k
views
Swift Error Handling For Methods That Do Not Throw [duplicate]
How to handle errors for methods or code that does not explicitly throw?
Wrapping it a do / catch block results in a compiler warning:
"'catch' block is unreachable because no errors are thrown in '...
2
votes
2
answers
595
views
Extra argument 'error' in call
I am getting this error Extra argument 'error' in call
Code in Context
var post:NSString = "name=\(Username)&email=\(Email)&phone=\(phonenumb)&password=\(Password)&address=\(...
-3
votes
1
answer
517
views
scope of do-catch in swift - cannot assign value to outside variable [duplicate]
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 ...
0
votes
1
answer
516
views
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....
0
votes
2
answers
1k
views
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 ...
1
vote
2
answers
2k
views
Getting Variable out of do-catch statement in swift
I want to know how to use a variable which is staying in a do-catch statement. I'm parsing some JSON from the web and filling an object with it but then I need that object outside to fill a ...
4
votes
1
answer
906
views
How to avoid nesting do/catch statements in Swift2
I keep wanting to do this:
do {
let result = try getAThing()
} catch {
//error
}
do {
let anotherResult = try getAnotherThing(result) //Error - result out of scope
} catch {
//error
}
...
-1
votes
2
answers
643
views
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 ...
1
vote
2
answers
564
views
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()) {
NSNotificationCenter....
0
votes
1
answer
135
views
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 ...