小编Ton*_*čić的帖子

在Swift 4中实现自定义解码器

我想使用DecodableSwift 4中引入的新协议解码XML文档,但是,似乎没有符合Decoder协议的XML解码器的现有实现.

我的计划是使用SWXMLHash库来解析XML,然后可能使该XMLIndexer库中的类扩展Decoder协议,以便可以使用XMLIndexer(XMLIndexer返回SWXMLHash.parse(xmlString))实例初始化我的模型.

XMLIndexer + Decoder.swift

我的问题是我不知道如何实现Decoder协议,我似乎无法在网上找到任何解释它是如何完成的资源.我发现的每一个资源都严格地提到了JSONDecoderSwift标准库中包含的类,没有找到的资源解决了创建自己的自定义解码器的问题.

xml decoder swift swxmlhash codable

30
推荐指数
1
解决办法
7993
查看次数

用于Swift 4中Codable协议的自定义字典编码器和解码器

如果我有一个符合Codable协议的结构,如下所示:

enum AnimalType: String, Codable {
    case dog
    case cat
    case bird
    case hamster
}

struct Pet: Codable {
    var name: String
    var animalType: AnimalType
    var age: Int
    var ownerName: String
    var pastOwnerName: String?
}
Run Code Online (Sandbox Code Playgroud)

如何创建一个编码器和解码器,将其编码/解码为Dictionary<String, Any?>类似实例?

let petDictionary: [String : Any?] = [
    "name": "Fido",
    "animalType": "dog",
    "age": 5,
    "ownerName": "Bob",
    "pastOwnerName": nil
]
let decoder = DictionaryDecoder()
let pet = try! decoder.decode(Pet.self, for: petDictionary)
Run Code Online (Sandbox Code Playgroud)

注意:我知道在将结果转换为字典对象之前可以使用JSONEncoderJSONDecoder类,但出于效率原因我不希望这样做.

Swift标准库带有JSONEncoderJSONDecoder …

dictionary encoder decoder swift codable

7
推荐指数
1
解决办法
1865
查看次数

使用collections.Counter计算不同颜色的表情符号

我想使用collections.Counter类来计算字符串中的表情符号.它通常工作正常,但是,当我引入彩色表情符号时,表情符号的颜色成分与表情符号分开,如下所示:

>>> import collections
>>> emoji_string = ""
>>> emoji_counter = collections.Counter(emoji_string)
>>> emoji_counter.most_common()
[('', 5), ('', 1), ('', 1), ('', 1), ('', 1), ('', 1)]
Run Code Online (Sandbox Code Playgroud)

我怎样才能使most_common()函数返回这样的内容:

[('', 1), ('', 1), ('', 1), ('', 1), ('', 1)]
Run Code Online (Sandbox Code Playgroud)

我正在使用Python 3.6

python unicode counter emoji

4
推荐指数
1
解决办法
635
查看次数

设置图像/背景图像时,UIButton 标题不会突出显示

我有一个带有右对齐箭头图标的自定义按钮。 默认登录按钮

当它通过点击突出显示时,只有箭头图标突出显示。文本保持白色,而不是像图标那样变成灰色。

登录按钮突出显示

这是代码(作用域在 UIButton 的子类中):

let rightIcon = #imageLiteral(resourceName: "disclosureIndicator")

setTitleColor(.white, for: .normal)
setBackgroundImage(rightIcon, for: .normal)

guard let image = backgroundImage(for: .normal) else { return }

titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: image.size.width)
Run Code Online (Sandbox Code Playgroud)

我还覆盖了 backgroundRect 属性。

override func backgroundRect(forBounds bounds: CGRect) -> CGRect {
    guard let image = backgroundImage(for: .normal) else {
        return super.backgroundRect(forBounds: bounds)
    }
    return CGRect(
        x: frame.width - (image.size.width + 20),
        y: (frame.height / 4) + (image.size.height / 4),
        width: image.size.width,
        height: image.size.height)
} …
Run Code Online (Sandbox Code Playgroud)

icons image title uibutton ios

2
推荐指数
1
解决办法
682
查看次数

标签 统计

codable ×2

decoder ×2

swift ×2

counter ×1

dictionary ×1

emoji ×1

encoder ×1

icons ×1

image ×1

ios ×1

python ×1

swxmlhash ×1

title ×1

uibutton ×1

unicode ×1

xml ×1