How to reassign a variable's value inside a struct? - swift

I have a struct "Person" which I want to reassign each individual object's amount from the structArray which I declared. When I do a for each loop to reassign the amount, an error says
Left side of mutating operator isn't mutable: 'person' is a 'let' constant
struct Person {
let name : String
var amount : Double
}
var structArray:[Person] = []
func calculateBill(pax: [Person]) -> [Person] {
for person in pax {
person.amount += taxByPerson //error
}
return pax
}
What is causing the issue and how can I fix this to be able to reassign the value?
EDIT: Thanks guys for pointing out where my error was, although the downvoting is pretty depressing to watch lol.
func calculateBill(pax: [Person]) -> [Person] {
var finalBill:[Person] = pax
for i in 0..<finalBill.count {
finalBill[i].amount += taxByPerson
}
return finalBill
}

Value types could be a pain and passed parameters are constants (let) by definition.
You could pass the array as an inout parameter, in the function add the value directly in the array:
func calculateBill(pax: inout [Person]) {
for index in 0..<pax.count {
pax[index].amount += taxByPerson
}
}
and call it
calculateBill(pax: &structArray)

Well the error is exactly what it says on the tin... person is a let context. You'll need to use var. But this is more easily accomplished with map
struct Person {
var name: String
var amount: Double
}
var people = [Person]()
func calculateBill(_ people: [Person]) -> [Person] {
return people.map{
var p = $0
p.amount += taxByPerson
return p
}
}

Related

Swift - How to Change Multi-Level JSON Struct Single Element Value on Condition

I have multi-level Struct that converts complex JSON data to a Struct. What I am struggling is to change the dataStruct.group.point.presentValue element value on condition.
var dataStruct : DataStruct = load("jsonfile.json")
struct DataStruct : Codable {
let name: String
var group: [groupData]
}
struct groupData: Codable, Hashable {
let id, name : String
var point : [pointInfo]
}
struct pointInfo : Codable, Hashable {
let id : String
let address : address
let name : String
var presentValue : String
}
struct address: Codable, Hashable {
let index: String
let type: String
}
I have tried the following map function, but the compiler complains that the Group in ForEach is 'let' constant.
Basically the function is supposed to compare address.index field in the Struct to the passed pointNo variable, and once it has been found (unique), point.presentValue is changed to the new value.
What is the correct way to achieve this?
func updatePresentValue(pointNo : String) {
dataStruct.group.forEach { Group in
Group.point = Group.point.map { point -> pointInfo in
var p = point
if point.address.index == pointNo {
p.presentValue = "New value"
return p
}
else { return p }
}
}
}
Basically there are two ways.
Extract the objects by assigning them to variables, modify them and reassign them to their position in dataStruct.
Enumerate the arrays and modify the objects in place.
This is an example of the second way
func updatePresentValue(pointNo : String) {
for (groupIndex, group) in dataStruct.group.enumerated() {
for (pointIndex, point) in group.point.enumerated() {
if point.address.index == pointNo {
dataStruct.group[groupIndex].point[pointIndex].presentValue = "New value"
}
}
}
}
It gets more complicated when dealing with multilevel structures but here is one way to do it where we first enumerate over group so we get both the object and the index of the object for each iteration so we can use this index when updating the group array. The inner struct is updated using a mutable copy of point
for (index, group) in dataStruct.group.enumerated() {
if group.point.contains(where: { $0.address.index == pointNo }) {
var copy = group
copy.point = group.point.reduce(into: []) {
if $1.address.index == pointNo {
var pointCopy = $1
pointCopy.presentValue = "new value"
$0.append(pointCopy)
} else {
$0.append($1)
}
}
dataStruct.group[index] = copy
}
}

Modifying an array passed as an argument to a function in Swift

Sorry for the newbie question; I'm still learning. I'm running into some odd behavior and couldn't find any documentation on this. I was wondering if you can help point out what I'm doing wrong here.
Error:
Cannot use mutating member on immutable value: 'arr' is a 'let' constant
class mySingleton {
static let sharedInstance = mySingleton()
private init() {}
var men = ["bob", "doug"]
var women = ["alice", "lisa"]
func removeFirst() {
self.arr.removeFirst()
}
func removeFirstByGender(gender: String) {
if gender == "men" {
self.modify(arr: self.men) // <-- error occurs here.
} else {
self.modify(arr: self.women) // <-- error occurs here.
}
}
func modify(arr: [String]) {
arr.removeFirst()
}
}
You need to change the definition of modify to accept an inout parameter. By default, function arguments are immutable, but by using the inout keyword, you can make them mutable. You also need to pass the argument by reference.
func modify( arr: inout [String]) {
arr.removeFirst()
}
var stringArr = ["a","b"]
modify(arr: &stringArr) //stringArr = ["b"] after the function call

Error - Cannot use mutating member on immutable value: function call returns immutable value

So I have this custom struct
public struct Feature {
var featureID: String = ""
var featureName: String = ""
var matchingFieldValue: String = ""
var polygonCollection = [MyPolygon]()
mutating func setFeatureID(featureID: String) {
self.featureID = featureID
}
func getMatchingFieldValue() -> String {
return matchingFieldValue
}
mutating func setMatchingFieldvalue(matchingFieldValue: String) {
self.matchingFieldValue = matchingFieldValue
}
public func getPolygonCollection() -> [MyPolygon] {
return polygonCollection
}
}
and I am trying to append a polygon to my polygonCollection by calling this function
feature.getPolygonCollection().append(polygon)
but I am getting the error
cannot use mutating member on immutable value: function call returns immutable value
by the way, I am defining the polygon in another class, it is a long class so just put the relevant calling code that gives the error.
All the previously asked ques
I appreciate all the help.
Due to value semantics getPolygonCollection() returns an immutable copy of polygonCollection. You cannot change it. That's what the error message says.
Add this function in the struct
mutating func add(polygon: MyPolygon) {
self.polygonCollection.append(polygon)
}
and call it
feature.add(polygon)

Cannot use mutating member on immutable value of type

I have following struct:
public protocol SuperModel {
// empty protocol
}
struct ModelOne: SuperModel {
struct SubModelOne {
var someVar: Double
var othervar: Double?
}
var sub: SubModelOne?
mutating func setSub(sub: SubModelOne) {
self.sub = sub
}
}
In my class, I want to use this struct like that:
final class SomeClass: SuperClass {
var data: SuperModel
init() {
self.data = ModelOne()
}
func someFunc() {
(self.data as! ModelOne).setSub(ModelOne.SubModelOne(someVar: 2, otherVar: 1))
}
}
I get following error: Cannot use mutating member on immutable value of type 'ModelOne'. Why is that so and how can I fix this?
When you apply type casting to value types (such structs), if succeed, you receive immutable copy of requested value:
(self.data as! ModelOne) // this is copy of data
The only way (as known to me) how you can mutate values that need to be casted - reassign value (as #Sahil Beri pointed you need declare variable):
func someFunc() {
if var data = data as? ModelOne {
data.setSub(ModelOne.SubModelOne(someVar: 2, otherVar: 1))
self.data = data // you can do this since ModelOne conforms to SuperModel
}
}
Use like this,
struct UserAttributes {
var name:String?
var organizationID:String?
var email:String?
mutating func parseUserAttributes(attribues:[AWSCognitoIdentityProviderAttributeType])->UserAttributes{
for type in attribues{
if type.name == "name"{
name = type.value
}else if(type.name == "family_name"){
organizationID = type.value
}else if(type.name == "custom:role_id"){
role = type.value
}else if(type.name == "email"){
email = type.value
}
}
}
}
In some other file call like this,
var userAttributes = UserAttributes()
userAttributes = userAttributes.parseUserAttributes(attribues:attributes)
Problem is that you have declared data as SuperModel but allocate it as ModelOne. Declare data as ModelOne. Then the problem goes away.
final class SomeClass: SuperClass {
var data: ModelOne
init() {
self.data = ModelOne()
}
func someFunc() {
(self.data).setSub(ModelOne.SubModelOne(someVar: 2, otherVar: 1))
}
}
First downcast the self.data to ModelOne then call setSub function
if var data = self.data as? ModelOne {
data.setSub(ModelOne.SubModelOne(someVar: 2, othervar: 1))
}
#Shadow of is right. You try to mutate a temporary structure which is impossible and most of the time useless as it will be released once the mutation done. It's in fact a similar issue to trying to modify the return struct of a function. (see answer here : Cannot assign to property: function call returns immutable value)
In Swift 3, in my case, I was able to resolve the error just by changing struct to a class object.

Change the value that is being set in variable's willSet block

I'm trying to sort the array that is being set before setting it but the argument of willSet is immutable and sort mutates the value. How can I overcome this limit?
var files:[File]! = [File]() {
willSet(newFiles) {
newFiles.sort { (a:File, b:File) -> Bool in
return a.created_at > b.created_at
}
}
}
To put this question out of my own project context, I made this gist:
class Person {
var name:String!
var age:Int!
init(name:String, age:Int) {
self.name = name
self.age = age
}
}
let scott = Person(name: "Scott", age: 28)
let will = Person(name: "Will", age: 27)
let john = Person(name: "John", age: 32)
let noah = Person(name: "Noah", age: 15)
var sample = [scott,will,john,noah]
var people:[Person] = [Person]() {
willSet(newPeople) {
newPeople.sort({ (a:Person, b:Person) -> Bool in
return a.age > b.age
})
}
}
people = sample
people[0]
I get the error stating that newPeople is not mutable and sort is trying to mutate it.
It's not possible to mutate the value inside willSet. If you implement a willSet observer, it is passed the new property value as a constant parameter.
What about modifying it to use didSet?
var people:[Person] = [Person]()
{
didSet
{
people.sort({ (a:Person, b:Person) -> Bool in
return a.age > b.age
})
}
}
willSet is called just before the value is stored.
didSet is called immediately after the new value is stored.
You can read more about property observers here
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Properties.html
You can also write a custom getter and setter like below. But didSet seems more convenient.
var _people = [Person]()
var people: [Person] {
get {
return _people
}
set(newPeople) {
_people = newPeople.sorted({ (a:Person, b:Person) -> Bool in
return a.age > b.age
})
}
}
It is not possible to change value types (including arrays) before they are set inside of willSet. You will need to instead use a computed property and backing storage like so:
var _people = [Person]()
var people: [Person] {
get {
return _people
}
set(newPeople) {
_people = newPeople.sorted { $0.age > $1.age }
}
}
Another solution for people who like abstracting away behavior like this (especially those who are used to features like C#'s custom attributes) is to use a Property Wrapper, available since Swift 5.1 (Xcode 11.0).
First, create a new property wrapper struct that can sort Comparable elements:
#propertyWrapper
public struct Sorting<V : MutableCollection & RandomAccessCollection>
where V.Element : Comparable
{
var value: V
public init(wrappedValue: V) {
value = wrappedValue
value.sort()
}
public var wrappedValue: V {
get { value }
set {
value = newValue
value.sort()
}
}
}
and then assuming you implement Comparable-conformance for Person:
extension Person : Comparable {
static func < (lhs: Person, rhs: Person) -> Bool {
lhs.age < lhs.age
}
static func == (lhs: Person, rhs: Person) -> Bool {
lhs.age == lhs.age
}
}
you can declare your property like this and it will be auto-sorted on init or set:
struct SomeStructOrClass
{
#Sorting var people: [Person]
}
// … (given `someStructOrClass` is an instance of `SomeStructOrClass`)
someStructOrClass.people = sample
let oldestPerson = someStructOrClass.people.last
Caveat: Property wrappers are not allowed (as of time of writing, Swift 5.7.1) in top-level code— they need to be applied to a property var in a struct, class, or enum.
To more literally follow your sample code, you could easily also create a ReverseSorting property wrapper:
#propertyWrapper
public struct ReverseSorting<V : MutableCollection & RandomAccessCollection & BidirectionalCollection>
where V.Element : Comparable
{
// Implementation is almost the same, except you'll want to also call `value.reverse()`:
// value = …
// value.sort()
// value.reverse()
}
and then the oldest person will be at the first element:
// …
#Sorting var people: [Person]
// …
someStructOrClass.people = sample
let oldestPerson = someStructOrClass.people[0]
And even more directly, if your use-case demands using a comparison closure via sort(by:…) instead of implementing Comparable conformance, you can do that to:
#propertyWrapper
public struct SortingBy<V : MutableCollection & RandomAccessCollection>
{
var value: V
private var _areInIncreasingOrder: (V.Element, V.Element) -> Bool
public init(wrappedValue: V, by areInIncreasingOrder: #escaping (V.Element, V.Element) -> Bool) {
_areInIncreasingOrder = areInIncreasingOrder
value = wrappedValue
value.sort(by: _areInIncreasingOrder)
}
public var wrappedValue: V {
get { value }
set {
value = newValue
value.sort(by: _areInIncreasingOrder)
}
}
}
// …
#SortingBy(by: { a, b in a.age > b.age }) var people: [Person] = []
// …
someStructOrClass.people = sample
let oldestPerson = someStructOrClass.people[0]
Caveat: The way SortingBy's init currently works, you'll need to specify an initial value ([]). You can remove this requirement with an additional init (see Swift docs), but that approach is much less complicated when your property wrapper works on a concrete type (e.g. if you wrote a non-generic PersonArraySortingBy property wrapper), as opposed to a generic-on-protocols property wrapper.