swift - 如何漂亮地打印符合 CustomDebugStringConvertible 的嵌套复合对象结构

标签 swift foundation protocol-oriented

我正在尝试漂亮地打印(对子对象进行缩进)一个复合对象结构,其中父对象和每个子对象均确认 CustomDebugStringConvertible 协议(protocol)。

到目前为止我的代码是: 导入基础

class StringUtils {
    static func appendIfValuePresent(key: String, value: AnyHashable?, toArray array: inout [String], separator: String = ": ") {
        if let value = value {
            array.append("\(key)\(separator)\(value)")
        }
    }
}

class Address: CustomDebugStringConvertible {
    var city: String? = "B'lore"
    var country: String? = "India"
    var pincode: String? = nil

    var debugDescription: String {
        var components = [String]()
        StringUtils.appendIfValuePresent(key: "city", value: city, toArray: &components)
        StringUtils.appendIfValuePresent(key: "country", value: country, toArray: &components)
        StringUtils.appendIfValuePresent(key: "pincode", value: pincode, toArray: &components)
        let debugStr = components.joined(separator: "\n")
        return debugStr
    }
}

class Contact: CustomDebugStringConvertible {
    var phNum: String? = "111-222-33"
    var email: String? = "ron@example.com"
    var address: Address? = Address()

    var debugDescription: String {
        var components = [String]()
        StringUtils.appendIfValuePresent(key: "phNum", value: phNum, toArray: &components)
        StringUtils.appendIfValuePresent(key: "email", value: email, toArray: &components)
        StringUtils.appendIfValuePresent(key: "address", value: address?.debugDescription, toArray: &components, separator: ":\n")
        let debugStr = components.joined(separator: "\n")
        return debugStr
    }
}

class Student: CustomDebugStringConvertible {
    var name = "ron"
    var contact: Contact? = Contact()
    var debugDescription: String {
        var components = [String]()
        StringUtils.appendIfValuePresent(key: "name", value: name, toArray: &components)
        StringUtils.appendIfValuePresent(key: "contact", value: contact?.debugDescription, toArray: &components, separator: ":\n")
        let debugStr = components.joined(separator: "\n")
        return debugStr
    }
}

let student = Student()

print(student)

上述代码片段的输出是:

// Actual output
/*
 name: ron
 contact:
 phNum: 111-222-33
 email: ron@example.com
 address:
 city: B'lore
 country: India
*/

但是,我想这样打印:

// Expected output
/*
 name: ron
 contact:
    phNum: 111-222-33
    email: ron@example.com
    address:
        city: B'lore
        country: India
*/

保留代码的通用结构来实现此目的的最佳方法是什么?

最佳答案

我可以设计一个解决方案来解决我的问题,并在这里发布了一个解决方案:https://gist.github.com/raunakp/16155d19d005939f1297b7c6cb6f890c

导入基础

class StringUtils {
    static func appendIfValuePresent(key: String, value: AnyHashable?, toArray array: inout [String], separator: String = ": ") {
        if let value = value {
            array.append("\(key)\(separator)\(value)")
        }
    }
}

struct PrettyPrintableComponent {
    var indentationLevel = 0
    var indentationSeparator = "\t"
    var lineSeparator = "\n"
}

protocol HasPrettyPrintableComponent {
    var prettyPrintableComponent: PrettyPrintableComponent { get set }
}

protocol PrettyDebugPrintable: HasPrettyPrintableComponent, CustomDebugStringConvertible { }

extension PrettyDebugPrintable {
    var indentationLevel: Int {
        get { return prettyPrintableComponent.indentationLevel }
        set { prettyPrintableComponent.indentationLevel = newValue }
    }
    var indentationSeparator: String {
        get { return prettyPrintableComponent.indentationSeparator }
        set { prettyPrintableComponent.indentationSeparator = newValue }
    }
    var lineSeparator: String {
        get { return prettyPrintableComponent.lineSeparator }
        set { prettyPrintableComponent.lineSeparator = newValue }
    }
}

class Address: PrettyDebugPrintable {
    var city: String? = "B'lore"
    var country: String? = "India"
    var pincode: String? = nil

    var prettyPrintableComponent = PrettyPrintableComponent()

    var debugDescription: String {
        var components = [String]()
        StringUtils.appendIfValuePresent(key: "city", value: city, toArray: &components)
        StringUtils.appendIfValuePresent(key: "country", value: country, toArray: &components)
        StringUtils.appendIfValuePresent(key: "pincode", value: pincode, toArray: &components)
        let indentationSeparator = String.init(repeating: prettyPrintableComponent.indentationSeparator, count: prettyPrintableComponent.indentationLevel)
        let debugStr = indentationSeparator + components.joined(separator: prettyPrintableComponent.lineSeparator + indentationSeparator)
        return debugStr
    }
}

class Contact: PrettyDebugPrintable {
    var phNum: String? = "111-222-33"
    var email: String? = "ron@example.com"
    var address: Address? = Address()

    var prettyPrintableComponent = PrettyPrintableComponent()

    var debugDescription: String {
        var components = [String]()
        StringUtils.appendIfValuePresent(key: "phNum", value: phNum, toArray: &components)
        StringUtils.appendIfValuePresent(key: "email", value: email, toArray: &components)
        address?.prettyPrintableComponent.indentationLevel = self.prettyPrintableComponent.indentationLevel + 1
        StringUtils.appendIfValuePresent(key: "address", value: address?.debugDescription, toArray: &components, separator: ":\n")
        let indentationSeparator = String.init(repeating: prettyPrintableComponent.indentationSeparator, count: prettyPrintableComponent.indentationLevel)
        let debugStr = indentationSeparator + components.joined(separator: prettyPrintableComponent.lineSeparator + indentationSeparator)
        return debugStr
    }
}

class Student: PrettyDebugPrintable {
    var name = "ron"
    var contact: Contact? = Contact()

    var prettyPrintableComponent = PrettyPrintableComponent()

    var debugDescription: String {
        var components = [String]()
        StringUtils.appendIfValuePresent(key: "name", value: name, toArray: &components)
        contact?.prettyPrintableComponent.indentationLevel = self.prettyPrintableComponent.indentationLevel + 1
        StringUtils.appendIfValuePresent(key: "contact", value: contact?.debugDescription, toArray: &components, separator: ":\n")
        let indentationSeparator = String.init(repeating: prettyPrintableComponent.indentationSeparator, count: prettyPrintableComponent.indentationLevel)
        let debugStr = indentationSeparator + components.joined(separator: prettyPrintableComponent.lineSeparator + indentationSeparator)
        return debugStr
    }
}

let student = Student()

print(student)

// output
/*
name: ron
contact:
    phNum: 111-222-33
    email: ron@example.com
    address:
        city: B'lore
        country: India
*/

关于swift - 如何漂亮地打印符合 CustomDebugStringConvertible 的嵌套复合对象结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57130270/

相关文章:

ios - 左侧的 Swift 文本字段添加图标

ios - 混合 Storyboard中的单元和以编程方式创建的单元

multithreading - NSIndexPath 线程安全吗?

objective-c - NSMutableArray 背后的数据结构是什么?

ios swift类符合协议(protocol)

ios - 通过扩展向协议(protocol)添加功能的原因是什么,为什么不把它放在协议(protocol)本身的定义中呢?

ios - 如何将协议(protocol)类型添加为 subview

swift - 是第一个(其中 :) Method always O(n) or it can be O(1) with usage of Set or Dictionary?

ios - 使用 ObjectMapper 解析字典中的字典 Swift

ios - 如何将平移手势从 UICollectionViewCell 传递给 UICollectionVIew?