Reputation: 13
Code:
extension Array {
// Total Together An Array
func FindTotal(_ arrayName: [Int]) -> Int {
var currentValue: Int = 0
for i in 0...Int(arrayName.count - 1) {
currentValue = currentValue + Int(arrayName[i])
}
return currentValue
}
// Number Grabber for Calculating the values
func calcItemsD(_ TargetArray: [String]) {
var placeholder: String? = nil
for i in 0...Int(TargetArray.count - 1) {
placeholder = String((TargetArray[i]).character(at: 0)!)
if (placeholder == "1") {
dealerNums.append("")
}
}
}
}
class DeckSetup : ObservableObject {
@Published public var deckOCards: [String] = []
@Published public var yourhand: [String] = []
@Published public var dealerHand: [String] = []
@Published public var dealerNums: [Int] = [7, 2]
@Published public var playerNums: [Int] = []
}
The dealerNums.append("") is throwing the error of out of scope and I am not sure why Heres the all the code that should be relevant.
Upvotes: 0
Views: 111
Reputation: 30341
dealerNums
is an array encapsulated in your DeckSetup
class. You can't access that from an Array
extension.
What you can do, is pass in dealerNums
into the function, like so:
func calcItemsD(_ targetArray: [String], dealerNums: inout [Int]) {
var placeholder: String? = nil
for i in 0 ..< targetArray.count {
placeholder = String(targetArray[i].first!)
if placeholder == "1" {
dealerNums.append("")
}
}
}
And called like so from inside your DeckSetup
class:
calcItemsD(["1", "K", "A"], dealerNums: &dealerNums)
dealerNums
is marked inout
, since you are mutating it within the function.
I cleaned up the function a tiny bit, but I don't know if you have more to it. For example, these are more things you could change to improve it:
for target in targetArray
instead of using the index.placeholder
is not needed to be stored outside the loop as it isn't used. You can have a local let placeholder = ...
if needed.!
) the first character. Provide a suitable alternative or fallback.Upvotes: 1