Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Playlist Browser with Swift Building the Music Library and Playlist Models Struct Initialization

Binary operator += cannot be applied to types '[String]' and 'String'???

I entered the code for the artists array just as it was demonstrated in the video, but I get an error saying that I cannot use the operator += for strings. Has anyone else seen this and figured out a solution?

5 Answers

Atif Naqvi
Atif Naqvi
4,144 Points

I was struggling with the same error. for the line

swift <code> artists += playlistDictionary["artists"] as! String! </code>

I was using the bang operator ! as they did for the earlier lines of code instead of using brackets [] around String like so

swift <code> artists += playlistDictionary["artists"] as! [String] </code>

Using xcode 7.1 and swift 2 through this tutorial

Gabe Nadel
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Gabe Nadel
Treehouse Guest Teacher

So, the operator += is generally meant for numerical values, not strings. Could you please paste your complete code in so we can take a look? Also, at what time in which exact video are you trying to reproduce?

By the way, your question was ok, but in the future, you'll probably get a quicker response if you post more complete code in the initial question. Cheers!

Thanks. I also associate += with numerical values, but in the video Pasan uses += to add

Thanks. I also associate += with numerical values, but in the video Pasan uses it to add one array to another, but at least in the latest version of xCode it is saying that it can't be done.

import Foundation
import UIKit

struct Playlist {
    let title: String?
    let description: String?
    let icon: UIImage?
    let largeIcon: UIImage?
    let artists: [String] = []
    let backgroundColor = UIColor.clearColor()

    init(index: Int){
        let musicLibrary = MusicLibrary().library
        let playlistDictionary = musicLibrary[index]

        title = playlistDictionary["title"] as! String!
        description = playlistDictionary["dictionary"] as! String!
        let iconName = playlistDictionary["icon"] as! String!
        icon = UIImage(named: iconName)
        let largeIconName = playlistDictionary["largeIcon"] as! String!
        largeIcon = UIImage(named: largeIconName)

        artists += playlistDictionary["artists"] as! [String]
        }

}

I found the problem. I can't say I understand it, but I found what I did wrong from Pasan's example. I had both "artists" and "backgroundColor" as 'let' constants. Changing them to var fixed it.

Karan Keelor
Karan Keelor
3,005 Points

Hi, having the same problem. I have both artists and backgroundColor as variables. Still getting the error.

I dont understand why we're using the += in the first place.

can you help?