I am writing a data gathering and manipulation program in Swift (my first time trying to program in Swift).
I have a function (determineHeighestColumnValue) to which I pass two parameters; an array containing dictionary objects loaded from a core data persistent store and a string representing the key for property that I want to access from a dictionary object at a particular index in that array.
Each dictionary object looks similar to this:
{
debtorDays = "-";
debtors = "-";
eacFees = 284680;
eacToDateContribution = 117159;
eacToDateContributionPerFees = "41%";
eacToDateOtherCosts = 6985;
eacToDateReimburseable = 10640;
eacToDateSalary = 171176;
lastInvoice = NA;
lastRevisionDate = "11-Jun-15";
etc etc
I am trying to work out how I can pass the key as a string and then use that to access the associated array value through this piece of code - "pprFiles[index].item.toDouble()" in the if statement below. ".toDouble()" is a string extension that returns the double value of the string. The function is determining column heights so the values passed and being accessed in the dictionary object are all number values as Strings - hence the conversion to a Double.
When I run the code with the "item" parameter specifically typed as a key then the code works fine. However, as a passed parameter I get a compiler error on the if statement.
Any thoughts on how I can pass a string value and get the if statement to work would be gratefully received.
The function code is:
func determineHeighestColumnValue(pprFiles: Array<PPRRowData>, item: String) ->Array<Double>{
var tempArray: [Double] = []
let count = pprFiles.count
for var index = 0; index < count; index++ {
// ******* need to work out how to get the reference right in the next line for item to be index.item
if pprFiles[index].item.toDouble() != nil {
tempArray.append(item.toDouble()!)
} else {
tempArray.append(0)
}
}
let highestColumn = maxElement(tempArray.map({abs($0)}))
if highestColumn != 0 {
tempArray = tempArray.map({$0 / highestColumn})
} else {
tempArray = tempArray.map({$0 * 0})
}
return tempArray
}
Aucun commentaire:
Enregistrer un commentaire