我有18个静态数组,每个数组有18个值。
我尝试将它们填充到一个巨大的二维数组中,但每次都会使编译器崩溃。
我已经意识到它的数据太多了,但我需要这些值中的每一个都用于一系列复杂的方程式。
我需要这样做更有效率,因为我必须多次这样做。
如果我可以编写动态选择数组的代码,这很容易。
双组分选择器用于选择2个值(18个选项)。 18个选项对应于18个阵列中的18个值。 我需要动态获取的数组由第二个组件(1)aka choice2确定。
choice1 = picker.selectedRowInComponent(0)
choice2 = picker.selectedRowInComponent(1)
用户选择要转换的值
numberToConvert = 345.0
以下是数组的示例:
let factorArray0 = [0.0595, 0.1682, 0.2668, 0.1075, 0.2711, 0.3479, 0.4099, 0.4285, 0.6767, 0.6486, 0.5290, 0.9579, 1.0000, 1.3147, 1.2999, 1.6438, 1.6542, 2.0764]
let factorArray1 = [0.3544, 0.2053, 0.5854, 0.7452, 0.2379, 0.3042, 0.3595, 0.4101, 0.4438, 0.5098, 0.6448, 0.5165, 0.8691, 1.0000, 1.1354, 1.3688, 1.6125, 1.7446]
let factorArray2 = [0.6544, 0.1033, 0.7854, 0.2452, 0.2375, 0.3022, 0.3525, 0.4701, 0.5038, 0.5011, 0.6488, 0.9165, 0.8291, 1.0000, 1.0354, 1.3388, 1.3125, 1.7166]
等......总共18个不同的阵列 这就是我目前用来完成工作的目的:
var conversionFactor:Double = 0.0000
if choice1 == 0 {
conversionFactor = factorArray0[choice2]
}else if choice1 == 1 {
conversionFactor = factorArray1[choice2]
}else if choice1 == 2 {
conversionFactor = factorArray2[choice2]
}else if choice1 == 3 {
conversionFactor = factorArray3[choice2]
}else if choice1 == 4 {
conversionFactor = factorArray4[choice2]
}else if choice1 == 5 {
conversionFactor = factorArray5[choice2]
}
//and so on.... if statement for every index 0-17 (18 total values)
if语句太长了。我有太多有效的编程知道任何长期可能是错误的。如果我可以在数组中引用数组名称,我可以这样做:
let arrayOfArrays = [[factorArray1],[factorArray2],[factorArray3],[factorArray4],....[factorArray17]]
//**please imagine the above array is filled with contents of those arrays so I don't paste a gigantic array or arrays**
conversionFactor = arrayOfArrays[choice1][choice2]
这适用于填充整个小数组,但是有这么多的值,它甚至无法编译。
最终产品是:
answer = conversionFactor * numberToConvert
问题:有没有人有更优雅的解决方案?我认为枚举的东西可能有用但却无法想到如何。最后我需要使用CoreData,但我正在寻找一个临时解决方案,因为我所有关于如何获取和检索数据数组的搜索都没有成功。我接受的课程将教我CoreData,但我有一个截止日期,所以我只需要一个临时解决方案,或者有人告诉我什么教程或部分CoreData可以帮助我。
答案 0 :(得分:1)
你应该只能使用二维数组,18x18不足以引起问题。我检查了自己,这编译好了:
import Foundation
let factorTable = [[0.0595, 0.1682, 0.2668, 0.1075, 0.2711, 0.3479, 0.4099, 0.4285, 0.6767, 0.6486, 0.5290, 0.9579, 1.0000, 1.3147, 1.2999, 1.6438, 1.6542, 2.0764],
[0.0595, 0.1682, 0.2668, 0.1075, 0.2711, 0.3479, 0.4099, 0.4285, 0.6767, 0.6486, 0.5290, 0.9579, 1.0000, 1.3147, 1.2999, 1.6438, 1.6542, 2.0764],
[0.0595, 0.1682, 0.2668, 0.1075, 0.2711, 0.3479, 0.4099, 0.4285, 0.6767, 0.6486, 0.5290, 0.9579, 1.0000, 1.3147, 1.2999, 1.6438, 1.6542, 2.0764],
[0.0595, 0.1682, 0.2668, 0.1075, 0.2711, 0.3479, 0.4099, 0.4285, 0.6767, 0.6486, 0.5290, 0.9579, 1.0000, 1.3147, 1.2999, 1.6438, 1.6542, 2.0764],
[0.0595, 0.1682, 0.2668, 0.1075, 0.2711, 0.3479, 0.4099, 0.4285, 0.6767, 0.6486, 0.5290, 0.9579, 1.0000, 1.3147, 1.2999, 1.6438, 1.6542, 2.0764],
[0.0595, 0.1682, 0.2668, 0.1075, 0.2711, 0.3479, 0.4099, 0.4285, 0.6767, 0.6486, 0.5290, 0.9579, 1.0000, 1.3147, 1.2999, 1.6438, 1.6542, 2.0764],
[0.0595, 0.1682, 0.2668, 0.1075, 0.2711, 0.3479, 0.4099, 0.4285, 0.6767, 0.6486, 0.5290, 0.9579, 1.0000, 1.3147, 1.2999, 1.6438, 1.6542, 2.0764],
[0.0595, 0.1682, 0.2668, 0.1075, 0.2711, 0.3479, 0.4099, 0.4285, 0.6767, 0.6486, 0.5290, 0.9579, 1.0000, 1.3147, 1.2999, 1.6438, 1.6542, 2.0764],
[0.0595, 0.1682, 0.2668, 0.1075, 0.2711, 0.3479, 0.4099, 0.4285, 0.6767, 0.6486, 0.5290, 0.9579, 1.0000, 1.3147, 1.2999, 1.6438, 1.6542, 2.0764],
[0.0595, 0.1682, 0.2668, 0.1075, 0.2711, 0.3479, 0.4099, 0.4285, 0.6767, 0.6486, 0.5290, 0.9579, 1.0000, 1.3147, 1.2999, 1.6438, 1.6542, 2.0764],
[0.0595, 0.1682, 0.2668, 0.1075, 0.2711, 0.3479, 0.4099, 0.4285, 0.6767, 0.6486, 0.5290, 0.9579, 1.0000, 1.3147, 1.2999, 1.6438, 1.6542, 2.0764],
[0.0595, 0.1682, 0.2668, 0.1075, 0.2711, 0.3479, 0.4099, 0.4285, 0.6767, 0.6486, 0.5290, 0.9579, 1.0000, 1.3147, 1.2999, 1.6438, 1.6542, 2.0764],
[0.0595, 0.1682, 0.2668, 0.1075, 0.2711, 0.3479, 0.4099, 0.4285, 0.6767, 0.6486, 0.5290, 0.9579, 1.0000, 1.3147, 1.2999, 1.6438, 1.6542, 2.0764],
[0.0595, 0.1682, 0.2668, 0.1075, 0.2711, 0.3479, 0.4099, 0.4285, 0.6767, 0.6486, 0.5290, 0.9579, 1.0000, 1.3147, 1.2999, 1.6438, 1.6542, 2.0764],
[0.0595, 0.1682, 0.2668, 0.1075, 0.2711, 0.3479, 0.4099, 0.4285, 0.6767, 0.6486, 0.5290, 0.9579, 1.0000, 1.3147, 1.2999, 1.6438, 1.6542, 2.0764],
[0.0595, 0.1682, 0.2668, 0.1075, 0.2711, 0.3479, 0.4099, 0.4285, 0.6767, 0.6486, 0.5290, 0.9579, 1.0000, 1.3147, 1.2999, 1.6438, 1.6542, 2.0764],
[0.0595, 0.1682, 0.2668, 0.1075, 0.2711, 0.3479, 0.4099, 0.4285, 0.6767, 0.6486, 0.5290, 0.9579, 1.0000, 1.3147, 1.2999, 1.6438, 1.6542, 2.0764],
[0.0595, 0.1682, 0.2668, 0.1075, 0.2711, 0.3479, 0.4099, 0.4285, 0.6767, 0.6486, 0.5290, 0.9579, 1.0000, 1.3147, 1.2999, 1.6438, 1.6542, 2.0764]
]
struct BigArrays {
init() {
let choice1 = 9
let choice2 = 13
print(factorTable[choice1][choice2])
}
}