Cannot convert value of type 'Set<String>' to expected argument type 'String?'

I have a list of items that contains groups, when I print them, it shows all groups.

MOVIE: Cartoon Multi Subtitles
MOVIE: Cartoon Multi Subtitles
MOVIE: Cartoon Multi Subtitles
MOVIE: Cartoon Multi Subtitles
MOVIE: Cartoon Multi Subtitles

MOVIE: English
MOVIE: English
MOVIE: English
MOVIE: English
MOVIE: English

I found that using Set will remove duplicates.

@IBAction func button(_ sender: Any) {
    
    let alertController = UIAlertController(title: "", message: "", preferredStyle: .alert)

    let uniqueGroups = Set((playlist?.objects.map({ $0.group }))!)

    
    alertController.addAction(UIAlertAction(title: uniqueGroups, style: .default) { <-- Cannot convert value of type 'Set<String>' to expected argument type 'String?'
        action in
    })

    
    alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel))
    
    self.present(alertController, animated: true, completion: nil)
}

Cannot convert value of type 'Set<String>' to expected argument type 'String?'

What do you want the alert's title to be?

Using a set will reduce:

MOVIE: Cartoon Multi Subtitles
MOVIE: Cartoon Multi Subtitles
MOVIE: Cartoon Multi Subtitles
MOVIE: Cartoon Multi Subtitles
MOVIE: Cartoon Multi Subtitles

MOVIE: English
MOVIE: English
MOVIE: English
MOVIE: English
MOVIE: English

to:

MOVIE: Cartoon Multi Subtitles
MOVIE: English

But you're still left with two strings that UIAlertAction doesn't know how to combine.

You can combine them yourself; but it really depends on what you want the alert to display. Here's one approach:

UIAlertAction(
    title: uniqueGroups.joined(separator: "😎"),
    style: .default
) { _ in }
    for i in uniqueGroups {
        
        alertController.addAction(UIAlertAction(title: i, style: .default) {
            action in
        })
    }

Solved :sunglasses:

Terms of Service

Privacy Policy

Cookie Policy