Как извлечь конкретное значение столбца из файла CSV, преобразованного в файл Plist? - PullRequest
0 голосов
/ 11 мая 2019

Я преобразовал файл 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]]

    }

}

Однако каждый раз, когда я запускаю приложение Iпродолжайте получать следующее сообщение об ошибке:

*** Завершение работы приложения из-за необработанного исключения «NSInvalidArgumentException», причина: «Невозможно проанализировать строку формата» Обозначение раздела =% @ "'

Любая помощь очень ценится.

С уважением, Шади.

...