macos - 什么格式存储大量混合数据

原文 标签 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

最佳答案

不幸的是,除了写入/复制数据(可能借助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
}

关于macos - 什么格式存储大量混合数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31641220/

相关文章:

objective-c - NSTableView 对齐到窗口的右侧?

macos - 在 iCloud Drive 中进行 latex 化

objective-c - 按下选项卡时,NSTextView中的行为异常(即使设置了tabStops)

ios - 如何在本地运行 TestFlight 构建

ios - 调用异步方法的方法的单元测试

objective-c - 键入 cocoa 中的修正警告

c - 如何使用 ptrace() 观察进程直到它退出?

ios - 有没有一种方法可以制作可以与NFC交互的IOS应用?

objective-c - 事件触发的 cocoa URL书签

objective-c - AVPlayerLooper 不会循环播放多个视频