Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使UIFont符合可解码_Ios_Swift_Uikit_Decodable - Fatal编程技术网

Ios 如何使UIFont符合可解码

Ios 如何使UIFont符合可解码,ios,swift,uikit,decodable,Ios,Swift,Uikit,Decodable,我正试图使UIFont符合Decodable,但我遇到了困难 我目前有一个解决方案,我把UIFont包装成这样的字体结构 public struct Font: Decodable{ let font: UIFont private enum CodingKeys: String, CodingKey { case size case font } public init(from decoder: Decoder){

我正试图使
UIFont
符合
Decodable
,但我遇到了困难

我目前有一个解决方案,我把UIFont包装成这样的字体结构

public struct Font: Decodable{
    let font: UIFont

    private enum CodingKeys: String, CodingKey {
        case size
        case font
    }

    public init(from decoder: Decoder){
        do{
            let values = try decoder.container(keyedBy: CodingKeys.self)
            font = UIFont(name: try values.decode(String.self, forKey: .font), size: try values.decode(CGFloat.self, forKey: .size))!
        } catch{
            fatalError("Font configuration error:\(error)")
        }
    }
}
这是可行的,但我看起来很笨拙,所以我试着这样做:

final class Font: UIFont, Decodable{
    private enum CodingKeys: String, CodingKey {
        case size
        case font
    }

    convenience init(from decoder: Decoder)  {
        do{
            let values = try decoder.container(keyedBy: CodingKeys.self)
            super.init(name: try values.decode(String.self, forKey: .font), size: try values.decode(CGFloat.self, forKey: .size))
        } catch{
            fatalError("Font configuration error:\(error)")
        }
    }
}
但是,这不起作用,因为
init(来自解码器:解码器)
不能是一个失败的初始化器,
UIFont.init(名称:String,大小:CGFloat)
是一个失败的初始化器,从一个不失败的初始化器调用一个失败的初始化器是不可能的


对于如何使
UIFont
符合
Decodable
,而无需包装的任何建议,我们将不胜感激。

我正在用手机写作,所以我无法尝试这段代码,但我认为这可以奏效。如果有帮助,请告诉我

final class Font: Codable {
let size: CGFloat
let name: String

var font: UIFont = UIFont.init()

init(s: CGFloat, n: String) {
    size = s
    name = n

    font = UIFont(name: name, size: size) ?? UIFont.systemFont(ofSize: size)
}

enum CodingKeys: String, CodingKey {
    case size
    case name
}

required init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)

    size = try container.decode(CGFloat.self, forKey: .size)
    name = try container.decode(String.self, forKey: .name)

    font = UIFont(name: name, size: size) ?? UIFont.systemFont(ofSize: size)
}

func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)

    try container.encode(size, forKey: .size)
    try container.encode(name, forKey: .name)
}
}

这确实是一个谜。这里有一个通用的答案,但这要求我们能够在通用的
init()
之后设置属性,这对
UIFont
没有好处,因为我们不能设置大小&c。直接的。也许
UIFontDescriptor
作为
Decodable
可能是答案,而不是
UIFont
?“
init(来自解码器:解码器)
不能是一个失败的初始化器”-相反,它应该是失败的(或者准确地说:它应该能够抛出错误)。这看起来像是问题的解决方案#1,这是可行的,但并不可取。如上所述,这将是可行的,但它仍然只是UIFont的包装,这是我想要避免的。主要是因为我想避免在使用Configuration.Fonts.Main.Font时编写类似的内容。。。。