Decodable icon indicating copy to clipboard operation
Decodable copied to clipboard

Xcode warning 'no calls to throwing functions occur within 'try' expression with classes

Open chrismanderson opened this issue 8 years ago • 1 comments

When trying to use Decodable with classes, I get a odd Xcode error no calls to throwing functions occur within 'try' expression. It's odd because while I can see the line that causes it, Xcode itself doesn't associate the warning with the specific line. And, I don't actually get the error as the init function does throw.

class Model: NSObject, Decodable {
    var name: String

    required init(json: AnyObject) throws {
        try self.name = json => "name"
    }

    static func decode(j: AnyObject) throws -> Self {
        // Xcode does not like this line
        return try self.init(json: j)
    }
}
image 2016-06-25 at 8 38 53 am

So my questions are:

  1. Is there a better setup I should be doing when using Decodable with classes?
  2. Any thoughts on how to resolve that warning itself?

Thanks so much!

chrismanderson avatar Jun 25 '16 12:06 chrismanderson

Hi!

  1. I don't think so. I have been doing the same thing, but mostly with final classes.
  2. Sorry, have no idea why that is happening. Only way I got the error to go away was by doing the following. It might not be the most elegant setup, but I have seriously considered it before for complex init-methods because of the separation of concerns between decoding json values and initializing the object.
class Model: NSObject, Decodable {
    var name: String

    required init(name: String){
        self.name = name
    }

    static func decode(j: AnyObject) throws -> Self {
        return try self.init(name: j => "name")
    }
}

And, I don't actually get the error as the init function does throw.

Are you saying it actually doesn't throw? 😮 Then what does it do? Would have been very strange and scary if it acted like try? j => , but when I try to replicate it throws like expected.

Also the warning seems to be gone in Swift 3, but I wonder why I didn't see it before.

Anviking avatar Jun 26 '16 13:06 Anviking