Hi, I'm pretty new to Swift and am a little confused on how the 'mutating' keyword functions.
I have this struct:
struct Road: Identifiable, Equatable, Hashable, Encodable, Decodable {
let id: UUID
// Customization
var roadname: String
var encodedImage: Data?
// Functionality
var wordlist: [WordPair]
var milestones: [Milestone]
var milestonesCompleted: Int {
var count = 0
for item in milestones {
if item.completed {
count += 1
}
}
return count
}
}
// Initialisers
extension Road {
init(id: UUID = UUID() ) {
self.id = id
self.roadname = "New Road"
self.wordlist = []
self.milestones = []
}
init(id: UUID = UUID(), wordlist: [WordPair], roadname: String, imagedata: UIImage? ) {
self.id = id
self.roadname = roadname
self.encodedImage = imagedata?.pngData()
self.wordlist = wordlist
self.milestones = Road.createMilestones(words: wordlist)
}
}
extension Road {
func getImage() -> UIImage? {
if let createImage = encodedImage{
return UIImage(data: createImage)
} else {
return nil
}
}
mutating func updateImage(image: Data){
self.encodedImage = image
}
mutating func cloneRoad(road: Road) {
self.roadname = road.roadname
self.encodedImage = road.encodedImage
self.wordlist = road.wordlist
}
mutating func getNewMilestones() {
self.milestones = Road.createMilestones(words: wordlist)
}
static func createMilestones( words: [WordPair ]) -> [Milestone] {
let MAX_PER_MILESTONE = 10;
var temporaryWordList: [WordPair] = []
var tmpmilestones: [Milestone] = []
for item in words {
temporaryWordList.append(item)
if (temporaryWordList.count % MAX_PER_MILESTONE == 0 &&
temporaryWordList.count != 0 ){
tmpmilestones.append(Milestone(id: (temporaryWordList.count / MAX_PER_MILESTONE), wordList: temporaryWordList))
}
}
tmpmilestones.append(Milestone(id: ( tmpmilestones.count + 1 ), wordList: temporaryWordList))
return tmpmilestones
}
}
I want to update 'milestones' down the line, an array of objects (also structs) computed from the updated wordlist.
My issue is that the wordlist is updated, but calling the function
mutating func getNewMilestones() {
self.milestones = Road.createMilestones(words: wordlist)
}
Doesn't change anything in the actual object. The changes exist when checking values within the function, but the actual change is lost outside.
Whats going on here?
Thank you very much in advance!