简体   繁体   English

json.swift 错误是 Self.init 未在委托初始化程序的所有路径上调用

[英]json.swift error is Self.init isn't called on all paths in delegating initializer

enter image description here在此处输入图片说明

errors such as the photo came out in json.swift json.swift 中出现诸如照片之类的错误

I don't know how to fix it.我不知道如何修复它。 please help me请帮我

    public convenience init(nsurl:NSURL) {
    var enc:NSStringEncoding = NSUTF8StringEncoding
    let err:NSError?

    do {
        let str: String? = try NSString(contentsOfURL: nsurl, encoding: NSUTF8StringEncoding) as String
        //print(str)

        self.init(nsurl:nsurl)

    } catch let error as NSError {
        //print(error.description)

    }
}

You have a few options here depend on your intention.根据您的意图,您有几个选择。

If you just wont your object to be crated and print addition info during init you can rewrite you code like this.如果您只是不想在初始化期间创建对象并打印附加信息,则可以像这样重写代码。

public convenience init(nsurl:NSURL) {       
 var enc:NSStringEncoding = NSUTF8StringEncoding
 let err:NSError?

 self.init(nsurl:nsurl)
 do {
       let str: String? = try NSString(contentsOfURL: nsurl, encoding: NSUTF8StringEncoding) as String
       print(str)

    } catch let error as NSError {
       print(error.description)
    }
}

Another option is to have failable initializer.另一种选择是具有可失败的初始化程序。 For this you need call self.init(nsurl:nsurl) if do block performed without exception otherwise return nil from catch block.为此,您需要调用 self.init(nsurl:nsurl) 如果 do 块无异常执行,否则从 catch 块返回 nil。 Also you need to mark you convenience init method as failable (see "?" after init word)您还需要将便利的 init 方法标记为可失败(请参阅 init 字后的“?”)

public convenience init?(nsurl:NSURL) {       
 var enc:NSStringEncoding = NSUTF8StringEncoding
 let err:NSError?

    do {
       self.init(nsurl:nsurl)
       let str: String? = try NSString(contentsOfURL: nsurl, encoding: NSUTF8StringEncoding) as String
       print(str)

    } catch let error as NSError {
       print(error.description)
       return nil
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在从初始化程序返回之前,不会在所有路径上调用“self.init” - 'self.init' isn't called on all paths before returning from initializer Swift:在从初始化程序返回之前没有在所有路径上调用“super.init”? - Swift: 'super.init' isn't called on all paths before returning from initializer? 在调用self.init之前自己使用的iOS Swift便利初始化程序 - iOS Swift convenience initializer self used before self.init called 在从初始化程序错误返回之前,不会在所有路径上调用Super.init - Super.init isn't called on all paths before returning from initializer Error 解决“从初始化快速返回之前未在所有路径上调用Super.init的问题”。 - Problems getting around 'Super.init isn't called on all paths before returning from initializer swift.' 从初始化程序返回之前,并非在所有路径上都调用“ super.init” - 'super.init' isn't called on all paths before returning from initializer 在从初始化程序返回之前,不会在所有路径上调用super init - super init isn't called on all paths before returning from initializer 便利初始化程序必须委托自定义 UIView 初始化程序的 self.init 错误 - Convenience initializer must delegate with self.init error for custom UIView initializer 为什么我在UIButton的子类的便捷初始化中不能使用“ self.init(type:.custom)” - Why can't I use “self.init(type: .custom)” in convenience initializer at my subclass of UIButton 当我添加了 self.init(url: url) 时,为什么要求调用 self.init? - Why is it asking for self.init to be called, when I have added self.init(url: url)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM