‘mutating’ in Swift

This post was originally posted at https://agrawalsuneet.github.io/blogs/mutating-in-swift/ and reposted on Medium on 29th Mar 2019.
class Employee {
var name : String
var teamName : String
init(name: String, teamName: String) {
self.name = name
self.teamName = teamName
}
func changeTeam(newTeamName : String){
self.teamName = newTeamName
}
}
var emp1 = Employee(name : "Suneet", teamName:"Engineering")print(emp1.teamName) //Engineering
emp1.changeTeam(newTeamName : "Product")
print(emp1.teamName) //Product
struct Employee {
var name : String
var teamName : String
init(name: String, teamName: String) {
self.name = name
self.teamName = teamName
}
func changeTeam(newTeamName : String){
self.teamName = newTeamName
//cannot assign to property: 'self' is immutable
}
}
cannot assign to property: 'self' is immutable

Please continue reading at https://agrawalsuneet.github.io/blogs/mutating-in-swift/

--

--

https://agrawalsuneet.github.io/

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store