ios - swift 4 可编码 : Cannot exclude a property

标签 ios swift4 codable

我正在开发一个简单的音乐音序器应用程序。这类应用往往具有必须保存/加载的复杂数据结构,因此在 Swift4 中引入 Codable 协议(protocol)对我来说完全是个好消息。

我的问题是: 我必须拥有不可编码的属性(property)。它不必编码,因为它是一个临时变量,仅在应用程序处于事件状态时保持事件状态。 所以我只是尝试通过实现 CodingKey 来排除,但编译器仍然给我错误“Type 'Song' does not conform to protocol 'Decodable'”。

具体来说,我想在下面的代码中排除“musicSequence”。

class Song : Codable { //Type 'Song' does not conform to protocol 'Decodable'
    var songName : String = "";    
    var tempo : Double = 120;

    // Musical structure
    var tracks : [Track] = [] // "Track" is my custom class, which conforms Codable as well    
    // Tones
    var tones = [Int : ToneSettings] (); // ToneSettings is also my custom Codable class

    var musicSequence : MusicSequence? = nil; // I get the error because of this line

    private enum CodingKeys: String, CodingKey {
        case songName
        case tempo
        case tracks
        case tones
    }

    func createMIDISequence () {
        // Create MIDI sequence based on "tracks" above
        // and keep it as instance variable "musicSequence"
    }
}

有没有人有什么想法?

最佳答案

(请参阅下面的奇怪事件。)

您对 CodingKeys 的使用已经在处理您的编码问题。您仍然可以免费获得它。但是您需要告诉系统如何手动处理解码:

required init(from decoder: Decoder) throws {
    let values = try decoder.container(keyedBy: CodingKeys.self)
    songName = try values.decode(String.self, forKey: .songName)
    tempo = try values.decode(Double.self, forKey: .tempo)
    tracks = try values.decode([Track].self, forKey: .tracks)
    tones = try values.decode([Int: ToneSettings].self, forKey: .tones)
}

相当 还不够聪明,无法弄清楚 musicSequence 可以而且应该默认为 nil(也许这无论如何都太聪明了) .

可能值得在 bugs.swift.org 上打开一个缺陷来要求这个 Decodable 是自动的。在您提供 CodingKeys 并且有默认值的情况下,它应该能够弄清楚。


编辑:当我第一次回答这个问题时,我完全重复了你的错误。但是当我再次尝试重新复制您的代码时,错误并没有出现。以下代码在 playground 中编译和运行:

import Foundation

struct Track: Codable {}
struct ToneSettings: Codable {}
struct MusicSequence {}

class Song : Codable { //Type 'Song' does not conform to protocol 'Decodable'
    var songName : String = "";
    var tempo : Double = 120;

    // Musical structure
    var tracks : [Track] = [] // "Track" is my custom class, which conforms Codable as well
    // Tones
    var tones = [Int : ToneSettings] (); // ToneSettings is also my custom Codable class

    var musicSequence : MusicSequence? = nil; // I get the error because of this line

    private enum CodingKeys: String, CodingKey {
        case songName
        case tempo
        case tracks
        case tones
    }

    func createMIDISequence () {
        // Create MIDI sequence based on "tracks" above
        // and keep it as instance variable "musicSequence"
    }
}

let song = Song()
let data = try JSONEncoder().encode(song)
String(data: data, encoding: .utf8)

let song2 = try JSONDecoder().decode(Song.self, from: data)

我想知道这里是否存在编译器错误;确保使用新的测试版对此进行测试。

关于ios - swift 4 可编码 : Cannot exclude a property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45557262/

相关文章:

ios - 安装了相同应用程序的iOS联系人列表

ios - 如何在 UICollectionView Image Scrolling 中添加 PageControl

ios - 无论框架的 x 和 y 为何,UIView 始终位于屏幕底部 - Swift

c++ - 在 Cocos2dx 中绘制矩形

ios - 我应该为 Pod 使用 Swift 4 迁移工具吗?

video - Exoplayer 等效于 iOS - 类似于 TikTok 的视频流

json - 使用 codable 将字符串转换为 Date/Int/Double

ios - 如何从具有多个值的 Codable 结构中获取一个值?

swift - 使用 Moya 刷新身份验证 token

swift - 如何使用数据和可编码的Swift优化NSAttributedString的存储?