存储大量混合数据的格式

时间:2015-07-26 19:45:56

标签: macos swift cocoa

我需要你们的观点如何存储这类数据,因为我还是初学者,如果这是一个愚蠢的问题,那就很抱歉。

我需要在我的应用中存储大量数据,如图所示,以便可以访问计算结果。

我尝试使用Struct来做,但我必须将每一行分配给一个变量而我不想拥有数百个。我宁愿使用数组但不知道混合类型是否会使它变得更复杂。

struct sectionPropertiesUKB {
   let name : String
   let mass : Double
   let depth : Double
   let width : Double
   let tw : Double
   let tf : Double
   let r : Double
   let d : Double
   let cfOverTf: Double
   let cwOverTw: Double
   let C : Double
   let N : Int
   let n : Int
   let SurfaceAperMeter : Double
   let SurfaceAperTonne : Double
   let Iyy : Int
   let Izz : Int
   let iyy : Double
   let izz : Double
   let Wely : Int
   let Welz : Int
   let Wply : Int
   let Wplz : Int
   let U : Double
   let X : Double
   let Iw : Double
   let It : Int
   let A : Int

init(_ name : String, _ mass : Double, _ depth : Double, _ width : Double, _ tw : Double, _ tf : Double, _ r : Double, _ d : Double, _ cfOverTf: Double, _ cwOverTw: Double, _ C : Double, _ N : Int, _ n : Int, _ SurfaceAperMeter : Double, _ SurfaceAperTonne : Double, _ Iyy : Int, _ Izz : Int, _ iyy : Double, _ izz : Double, _ Wely : Int, _ Welz : Int, _ Wply : Int, _ Wplz : Int, _ U : Double, _ X : Double, _ Iw : Double, _ It : Int, _ A : Int)
{
    self.name = name
    self.mass = mass
    self.depth = depth
    self.width = width
    self.tw = tw
    self.tf = tf
    self.r = r
    self.d = d
    self.cfOverTf = cfOverTf
    self.cwOverTw = cwOverTw
    self.C = C
    self.N = N
    self.n = n
    self.SurfaceAperMeter = SurfaceAperMeter
    self.SurfaceAperTonne = SurfaceAperTonne
    self.Iyy = Iyy
    self.Izz = Izz
    self.iyy = iyy
    self.izz = izz
    self.Wely = Wely
    self.Welz = Welz
    self.Wply = Wply
    self.Wplz = Wplz
    self.U = U
    self.X = X
    self.Iw = Iw
    self.It = It
    self.A = A

}

init()
{
    self.init("",0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0,0,0.0,0.0,0,0,0.0,0.0,0,0,0,0,0.0,0.0,0.0,0,0)

}

let b1 = sectionPropertiesUKB("1016x305x487",486.7,1036.3,308.5,30.0,54.1,30.0,868.1,20.2,28.9,17,150,86,3.2,6.58,1022000,26700,40.6,6.57,19700,1730,23200,2800,0.867,21.1,64.4,4300,620)

有没有更好更简单的方法存储?

我不想将b1定义为b333变量。核心数据会成为答案吗? 谢谢你的帮助。

Data Example

1 个答案:

答案 0 :(得分:1)

不幸的是,除了写入/复制数据(可能是借助OCR)之外,没有更简单的方法将数据导入程序。

如果您不想编写代码数据,您也可以将其设置为JSON文件并加载它。优点是您不必将其编写为代码,并在需要时将其转换为所需类型的数组。

// using the resources folder in a playground
// File: Data.json this represents your table (like row = sectionPropertiesUKB)
[
  {"name1" : [3.5,  7.4,  3.9]},
  {"name2" : [354,  7,    4.2]},
  {"name3" : [5.6,  1.3,  9.3]},
  {"name4" : [8.7,  2.2,  3.4]}
]

// a struct like you have
struct Sample {
    let name: String
    let length: Double
    let width: Double
    let count: Int
}

以下代码只能写一次。

Swift 1.2:

// get path and contents of the Data.json file
let path = NSBundle.mainBundle().pathForResource("Data", ofType: ".json")
if let string = String(contentsOfFile: path!) {

    /// data is of type [[String : [Double]]]
    let data = NSJSONSerialization.JSONObjectWithData(string.dataUsingEncoding(NSUTF16StringEncoding)!, options: NSJSONReadingOptions.AllowFragments, error: nil) as! [[String: [Double]]]

    /// mapped data
    let samples: [Sample] = data.map{ (dataPoint: [String : [Double]]) in

        /// row of your table
        let element: (String, [Double]) = Array(dataPoint)[0]
        let el: [Double] = element.1

        // type construction. Since el is of type [Double] some types have to be converted to Int
        return Sample(name: element.0, length: el[0], width: el[1], count: Int(el[2]))
    }
}

Swift 2(尝试捕捉但同样Sample):

let path = NSBundle.mainBundle().pathForResource("Data", ofType: ".json")

do {
    let string = try String(contentsOfFile: path!)
    let data = try NSJSONSerialization.JSONObjectWithData(string.dataUsingEncoding(NSUTF16StringEncoding)!, options: NSJSONReadingOptions.AllowFragments) as! [[String: [Double]]]

    let samples: [Sample] = data.map{ dataPoint in
        let element = Array(dataPoint)[0]
        let el = element.1
        return Sample(name: element.0, length: el[0], width: el[1], count: Int(el[2]))
    }
} catch {
    print(error)
}

如果您想通过名称访问它们,您应该使用可以像这样创建的字典(通过替换let samples的当前声明):

let samplesDict: [String : Sample] = data.reduce([:]) { (var dict, dataPoint) in
    let element = Array(dataPoint)[0]
    let el = element.1
    dict[element.0] = Sample(name: element.0, length: el[0], width: el[1], count: Int(el[2]))
    return dict
}