Saintz
Saintz

Reputation: 69

Implement a String in a separator Swift3

I have a question. I work on a app with currencies and i want to put the correct currency symbol automatically in the let applcation to trim the complete string. First i have this code snipped :

let firstPriceTrimmed = firstPrice.components(separatedBy: "€").flatMap { String($0.trimmingCharacters(in: .whitespaces)) }.last

Now my code ist look like :

switch sideIDS {
    case 6, 8, 9, 10, 12:
        var current = "€"
        let firstPriceTrimmedNew = firstPriceTrimmed?.components(separatedBy: String(current)) { String($0.trimmingCharacters(in: .whitespaces)) }.first
    case 7:
        var current = "£"
        let firstPriceTrimmedNew = firstPriceTrimmed?.components(separatedBy: String(current)) { String($0.trimmingCharacters(in: .whitespaces)) }.last
    case 11:
        var current = "₹"
        let firstPriceTrimmedNew = firstPriceTrimmed?.components(separatedBy: String(current)) { String($0.trimmingCharacters(in: .whitespaces)) }.last
    default:
        XCTFail()
    }

But it didn't work. XCode put the message "Cannot invoke 'components' with an argument list of type '(separatedBy: String?, (_) -> _)'. Whats wrong? I don't understand what XCode is wanted...

Upvotes: 1

Views: 46

Answers (1)

Nirav D
Nirav D

Reputation: 72420

You haven't added .flapMap or any other closure with result of components(separatedBy:) inside all cases of switch.

let firstPriceTrimmedNew: String

switch sideIDS {
case 6, 8, 9, 10, 12:
    let current = "€"
    firstPriceTrimmedNew = firstPriceTrimmed?.components(separatedBy: current).flatMap { String($0.trimmingCharacters(in: .whitespaces)) }.first
case 7:
    let current = "£"
    firstPriceTrimmedNew = firstPriceTrimmed?.components(separatedBy: current).flatMap { String($0.trimmingCharacters(in: .whitespaces)) }.last
case 11:
    let current = "₹"
    firstPriceTrimmedNew = firstPriceTrimmed?.components(separatedBy: current).flatMap { String($0.trimmingCharacters(in: .whitespaces)) }.last
default:
    XCTFail()
}

Upvotes: 1

Related Questions