1

I have a filter that I am trying to use to compare one value to another. Here is the enum that I am using:

enum SomeEnum: String {

    case first  = "Hey"
    case second = "There"
    case third  = "Peace"

static let values = [first, second, third]

func pickOne() -> String {
        switch self {
        case .first:
            return "value 1"
        case .second:
            return "value 2"
        case .third:
            return "value 3"
    }
}

Here is where I am attempting to filter and find matching values:

array.append(SomeEnum.values.filter({$0.rawValue ==  anotherArray["id"] as! String}))

I end up getting an ambiguous error:

Cannot convert value of type '[SomeEnum]' to expected argument type 'String'

Any ideas?

3
  • What is the types of array? Commented May 25, 2016 at 6:10
  • @DejanSkledar Array is of type [String]. Commented May 25, 2016 at 6:11
  • What's your anotherArray? You're trying to subscript it like a dictionary Commented May 25, 2016 at 6:20

1 Answer 1

1

The problem is, that SomeEnum.values return type is [SomeEnum] and not String.

And the append function expects the parameter to be String, instead it is [SomeEnum].

This is, what you need to change:

  1. Change append to appendContentsOf, since filter function returns an array, and not a single value
  2. Change the [SomeEnum] to [String] since you are adding it to a [String] array, like this.

This is the fix:

array.appendContentsOf(SomeEnum.values.filter({ $0.rawValue == "SomeString" }).map({ $0.PickOne() }))
Sign up to request clarification or add additional context in comments.

2 Comments

I edited my code, but if I enter $0.rawValue == "Hey" then it returns "Hey". I'd like to return case .first value of "value 1". Does that make sense? The errors were fixed, tho! :D
Okay, I edited your answer. It works just fine now. Thanks!! :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.