Value of type 'String' has no member 'stringByTrimmingCharactersInSet'

前端 未结 4 675
無奈伤痛
無奈伤痛 2020-12-11 00:12

After converting my project to swift 3, I get the following Value of type \'String\' has no member \'stringByTrimmingCharactersInSet\' error on the first line w

相关标签:
4条回答
  • 2020-12-11 00:51

    The new syntax is like this:

    var cString = hex.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased()
    

    As a suggestion, you don't need to specify that cString is a String, as this is assumed with the value you are assigning to it.

    0 讨论(0)
  • 2020-12-11 00:52

    Read the Swift 3 documentation; the new Swift 3 API is:

    func trimmingCharacters(in set: CharacterSet) -> String
    

    https://developer.apple.com/reference/swift/string/1643030-trimmingcharacters

    0 讨论(0)
  • 2020-12-11 01:07

    You can try this as well.

    let trimmedString = hex.trimmingCharacters(in: CharacterSet.whitespaces)
    
    0 讨论(0)
  • 2020-12-11 01:08

    And for an Int and Float how can I code it ?

        private var currentTotal_passengers: Int = 0 {
        didSet {
            currentTotal_passengers = currentTotal_passengers.trimmingCharacters(in: CharacterSet .whitespacesAndNewlines)
        }
    }
    
    0 讨论(0)
提交回复
热议问题