Cannot use mutating member on immutable value of type [String] - swift

I failed to append String by using following code:
(labGroups[labGroupIndex!].labs[labIndex!].tstDspGps[tstDspGpsIndex!].tests[testsIndex!]["specialReqValues"] as![String]).append("sdfsdf")
Error:
Cannot use mutating member on immutable value of type
'[String]'
And I can append value by using following code:
var arr = labGroups[labGroupIndex!].labs[labIndex!].tstDspGps[tstDspGpsIndex!].tests[testsIndex!]["specialReqValues"] as![String]
arr.append("testValue")
However, it only affect the variable arr.
That is not what I want, when I print:
((labGroups[labGroupIndex!].labs[labIndex!].tstDspGps[tstDspGpsIndex!].tests[testsIndex!]["specialReqValues"] as![String]))
it still has nil value.
Can anyone help to solve this problem?

Related

Find the sum of an array in swift

I have an array that holds integer values. And I have defined it like so:
#State private var numbers: Array = []
the array is updated as the user uses the app. At a certain point, I need to find the sum of all the values in the array. Here is what I tried to do:
let sumOfNum = numberz.reduce(0, +)
However, this is giving me the following error on the plus(+) symbol:
Cannot convert value of type '(Int) -> Int' to expected argument type '(Int, Any) throws -> Int'
Not sure what the problem is. Any help or guidance would be appreciated.
Your issue is probably your Array declaration. You should declare is as an array of Int instead of an array of Any.
So your array declaration should be
#State private var numbers: [Int] = []

Swift: Cannot convert value of type 'Int?' to specified type 'UInt32'

I'm trying to assign an array count to a UInt32. I get the error "Cannot convert value of type 'Int?' to specified type 'UInt32'". The array count is type Int, but the error says "Int?", which looks like an optional Int. I have no idea what else it could mean.
let randColorCount:UInt32 = slider?.randUIColors.count
I've also tried:
let randColorCount:UInt32 = UInt32(slider?.randUIColors.count)
But I get the error "Cannot invoke initializer for type 'UInt32' with an argument list of type '(Int?)'".
slider?.randUIColors.count results in Int? because of the use of the optional chaining.
One simple solution is to combine this with the nil coalescing operator ?? to provide a default value incase slider is nil.
let randColorCount = UInt32(slider?.randUIColors.count ?? 0)

How to assign a value to [any]

When I set c to a
var a: [Any]
var c: Array<PostCategory>
error shown:
cannot convert value of type 'Array' to expected argument type
[Any]
how to solve the problem?
The error message is a bit misleading but try initializing the array before assigning it:
var c: Array<PostCategory> = []
...or...
var c = Array<PostCategory>()
I bet your PostCategory is a struct. Apparently struct arrays aren't convertible to an Any array. This is weird because all types conforms to the Any protocol.
If you change the PostCategory to a class instead, it should work fine. You might need to create a new initializer for the class though, since classes doesn't give you the same default initializer as a struct does.

Using .sortInPlace within a .forEach closure Swift

I am attempting to sort the arrays within a dictionary, but am getting an error. Here is the code I have tried. What am I missing & why won't this compile?
var dict = [Int: [String]]()
dict[1] = ["Zack", "James", "Bill", "Quin", "Mike", "Adam"]
dict[1]?.sortInPlace()
dict.forEach{$0.1.sortInPlace()} // error: cannot use mutating member on immutable value of type '[String]'
Edit: I was able to get the following code to work after realizing that the for each loop assigns a constant by default:
db.forEach{db[$0.0] = $0.1.sort()}
Swift, by default assigns each value inside closure to be immutable. You can modify the default behavior by declaring the variable as mutable using var as this,
dict.forEach({ (key: Int, var value: [String]) in
value.sortInPlace()
print(value)
})

Swift Playground: Indexing a List within a class gives an error

I tried the following in Swift Playground:
class C {
init(test: Integer) {
let simpleList:String[] = ["A","B","C"]
simpleList[test]
println(simpleList[test])
}
}
I get an error:
Could not find an overload for subscript that accepts the supplied
arguments
This pops up in multiple places when I try to index a list.
There are usually two causes for this error:
In your case, you need to change test to the Int type like this: (test: Int).
Another case that throws a similar error is due to simpleList being of type Any or AnyObject.
In cases like those you need to cast it to type String[] or another Swift array type before indexing