【问题标题】:Transfer custom type to String将自定义类型转换为字符串
【发布时间】:2020-03-12 16:56:51
【问题描述】:

我有自定义类型:

open class HashTag: Equatable {

    open var text: String
    open var isRemovable: Bool
    open var hasHashSymbol: Bool
    open var configuration: HashtagConfiguration?

    public init(word: String, withHashSymbol: Bool = true, isRemovable: Bool = true) {
        self.text = word
        self.isRemovable = isRemovable
        self.hasHashSymbol = withHashSymbol

        if hasHashSymbol {
            self.text = "#" + text
        }
    }

    public static func == (lhs: HashTag, rhs: HashTag) -> Bool {
        return lhs.text == rhs.text
    }
}

我想把它转成普通的String数组再转回来。例如,对于:

   var tags = [
                HashTag(word: "this"),
                HashTag(word: "is"),
                HashTag(word: "an"),
                HashTag(word: "example")
    ]

我想转移到

var tagsTransferred = ["this","is","an","example"]

然后回传:

var tagsTransferred = ["this","is","an","example"]

到:

   var tags = [
                HashTag(word: "this"),
                HashTag(word: "is"),
                HashTag(word: "an"),
                HashTag(word: "example")
    ]

【问题讨论】:

  • 听起来你在找Array.map(_:)
  • 您是在问如何将[String] 变成[HashTag]?或者您是在问如何让编译器自动将文字 ["this","is","an","example"] 转换为 [HashTag]
  • 任何这种变体都会很好。

标签: ios swift casting


【解决方案1】:

Array 中的 map 方法用于转换数组的每个元素,即,正是您正在做的事情。

要将[String] 转换为[HashTag]

let hashTagArray = stringArray.map { HashTag(word: $0) }

[HashTag] 转换为[String]

let stringArray = hashTagArray.map { $0.text }

此外,如果您希望这样的事情成为可能:

// the string array literal is automatically converted to [HashTag]
let hashTagArray: [HashTag] = ["this", "is", "an", "example"]

你可以这样做:

open class HashTag : Equatable, ExpressibleByStringLiteral {

    ...

    public typealias StringLiteralType = String
    public required convenience init(stringLiteral value: String) {
        self.init(word: value)
    }
}

【讨论】:

  • 你也可以使用stringArray.map(HashTag.init),从Swift 5.1开始,你可以使用hashTagArray.map(\.text) :)
  • @Alexander-ReinstateMonica No stringArray.map(HashTag.init) 不适用于默认值
  • @Alexander-ReinstateMonica 我认为.map(HashTag.init) 不起作用,因为其他参数是“仅”可选参数,并且编译器不够聪明,无法弄清楚,但显然它是......这总是一件事吗?我记得可选参数不能很好地直接引用方法......
  • @LeoDabus 我在操场上试过它,但它确实有效......哦等等......这是字符串文字初始化......
  • @Alexander-ReinstateMonica 不,实际上并非如此。我认为它正在工作,但它实际上只是解决了我添加的init(stringLiteral:)...
【解决方案2】:

这两种情况都可以使用compactMap 函数。

  1. 从 [HashTag] 到 [String]

    var tags = [
        HashTag(word: "this"), 
        HashTag(word: "is"), 
        HashTag(word: "an"), 
        HashTag(word: "example")
    ]
    
    let arrString = tags.compactMap { (tag) -> String in
        tag.text.removeFirst() // This line just to remove "#"
        return tag.text
    }
    
  2. 从 [String] 到 [HashTag]

    var strings = ["this", "is", "an", "example"]
    
    let arrHashtag = strings.compactMap {
        return HashTag(word: $0)
    }
    

【讨论】:

  • 在非可选元素数组上使用 compactMap 以及将其与不可出错的初始化程序一起使用是没有意义的。
猜你喜欢
  • 2013-11-27
  • 2019-11-22
  • 2017-06-22
  • 2020-03-17
  • 1970-01-01
  • 2018-02-04
  • 2011-12-21
  • 1970-01-01
  • 2015-05-15
相关资源
最近更新 更多