如何从转换为Plist文件的CSV文件中提取特定的列值?

时间:2019-05-10 21:44:39

标签: swift csv filter plist nspredicate

我将CSV文件转换为Plist文件,并将其导入到我的Xcode项目中,

Plist文件的内容可以在下面找到:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <array>
        <dict>
            <key>"Section designation","Mass per metre (kg/m)","Depth of section, h (mm)","Width of section, b (mm)","Web thickness, tw (mm)","Flange thickness, tf (mm)","Root radius, r (mm)","Depth between fillets, d (mm)"</key>
            <string>1016 x 305x 584,584.0,1056.0,314.0,36.0,64.0,30.0,868.1</string>
        </dict>
        <dict>
            <key>"Section designation","Mass per metre (kg/m)","Depth of section, h (mm)","Width of section, b (mm)","Web thickness, tw (mm)","Flange thickness, tf (mm)","Root radius, r (mm)","Depth between fillets, d (mm)"</key>
            <string>1016 x 305x 494,494.0,1036.0,309.0,31.0,54.0,30.0,868.1</string>
        </dict>
        <dict>
            <key>"Section designation","Mass per metre (kg/m)","Depth of section, h (mm)","Width of section, b (mm)","Web thickness, tw (mm)","Flange thickness, tf (mm)","Root radius, r (mm)","Depth between fillets, d (mm)"</key>
            <string>1016 x 305x 438,438.0,1026.0,305.0,26.9,49.0,30.0,868.1</string>
        </dict>
        <dict>
            <key>"Section designation","Mass per metre (kg/m)","Depth of section, h (mm)","Width of section, b (mm)","Web thickness, tw (mm)","Flange thickness, tf (mm)","Root radius, r (mm)","Depth between fillets, d (mm)"</key>
            <string>1016 x 305x 415,415.0,1020.0,304.0,26.0,46.0,30.0,868.1</string>
        </dict>
        <dict>
            <key>"Section designation","Mass per metre (kg/m)","Depth of section, h (mm)","Width of section, b (mm)","Web thickness, tw (mm)","Flange thickness, tf (mm)","Root radius, r (mm)","Depth between fillets, d (mm)"</key>
            <string>1016 x 305x 393,392.7,1015.9,303.0,24.4,43.9,30.0,868.1</string>
        </dict>
        <dict>
            <key>"Section designation","Mass per metre (kg/m)","Depth of section, h (mm)","Width of section, b (mm)","Web thickness, tw (mm)","Flange thickness, tf (mm)","Root radius, r (mm)","Depth between fillets, d (mm)"</key>
            <string>1016 x 305x 350,350.0,1008.0,302.0,21.1,40.0,30.0,868.1</string>
        </dict>
        <dict>
            <key>"Section designation","Mass per metre (kg/m)","Depth of section, h (mm)","Width of section, b (mm)","Web thickness, tw (mm)","Flange thickness, tf (mm)","Root radius, r (mm)","Depth between fillets, d (mm)"</key>
            <string>1016 x 305x 314,314.3,999.9,300.0,19.1,35.9,30.0,868.1</string>
        </dict>
        <dict>
            <key>"Section designation","Mass per metre (kg/m)","Depth of section, h (mm)","Width of section, b (mm)","Web thickness, tw (mm)","Flange thickness, tf (mm)","Root radius, r (mm)","Depth between fillets, d (mm)"</key>
            <string>1016 x 305x 272,272.3,990.1,300.0,16.5,31.0,30.0,868.1</string>
        </dict>
        <dict>
            <key>"Section designation","Mass per metre (kg/m)","Depth of section, h (mm)","Width of section, b (mm)","Web thickness, tw (mm)","Flange thickness, tf (mm)","Root radius, r (mm)","Depth between fillets, d (mm)"</key>
            <string>1016 x 305x 249,248.7,980.1,300.0,16.5,26.0,30.0,868.1</string>
        </dict>
        <dict>
            <key>"Section designation","Mass per metre (kg/m)","Depth of section, h (mm)","Width of section, b (mm)","Web thickness, tw (mm)","Flange thickness, tf (mm)","Root radius, r (mm)","Depth between fillets, d (mm)"</key>
            <string>1016 x 305x 222,222.0,970.3,300.0,16.0,21.1,30.0,868.1</string>
        </dict>
    </array>
</plist>

将Plist文件导入Xcode项目后,我想提取特定列值的所有相应值,如下代码所示:

import UIKit

class UniversalBeamsViewController: UIViewController {

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view.

        print(getDataForSectionDesignation(sectionDesignation: "1016 x 305x 584"))

    }

    func getSwiftArrayFromPlist(name: String) -> (Array<Dictionary<String, String>>) {

        let path = Bundle.main.path(forResource: name, ofType: "plist")

        let array = NSArray(contentsOfFile: path!)

        return ((array as? Array<Dictionary<String, String>>)!)

    }

    func getDataForSectionDesignation(sectionDesignation: String) -> (Array<[String: String]>) {

        let array = getSwiftArrayFromPlist(name: "UB 1016x305 series")

        let namePredicate = NSPredicate(format: "Section designation == %@", sectionDesignation)

        return [array.filter {namePredicate.evaluate(with: $0)}[0]]

    }

}

但是,每次我运行该应用程序时,我都会不断收到以下错误消息:

  

***由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'无法解析格式   字符串“部分名称=%@”'

非常感谢您的帮助。

关于, 沙迪。

0 个答案:

没有答案