melisa
melisa

Reputation: 27

Cannot use mutating member on immutable value: 'n' is a 'let' constant

I am getting data from a website using rss. I want to set those datas to the variables in struct but I am getting an error in for loop.

struct News {
    var title: String
    var link: String
}

class HaberTableViewController: UITableViewController, XMLParserDelegate {
   var NewsArray:[News] = []

    override func viewDidLoad() {
        let url = "http://www.ensonhaber.com/rss/ensonhaber.xml"

        Alamofire.request(url).responseRSS() { (response) -> Void in
            if let feed: RSSFeed = response.result.value {
                for item in feed.items {
                    print(item.title!)
                    for n in self.NewsArray
                    {
                        n.title.append(item.title)
                        n.link.append(item.link)
                    }                    
                }             
            }
        }        
    }

.
.
.

}

Upvotes: 1

Views: 723

Answers (1)

rmaddy
rmaddy

Reputation: 318774

I think you are trying to populate the NewArray array with new News instances using values from feed.items. If that's the case then your code should be:

if let feed: RSSFeed = response.result.value {
    for item in feed.items {
        if let title = item.title, let link = item.link {
            let news = News(title: title, link: link)
            NewsArray.append(news)
        }
    }             
}

Note that this also deals with item.title (and presumably item.link) being optional.

Please note that variable names should start with lowercase letters so NewsArray should be newsArray.

Upvotes: 1

Related Questions