2017-02-15 27 views
2

我已經通過Alamofire.download下載了xml文件(現在我有一份XML當我的用戶離線時)。接下來的事情是將下載的XML解析爲字典。無法使用Swift3 Alamofire(4.3.0)解析XML,EVReflection(4.2.0) - 不是符合密鑰編碼的密鑰

我使用EVReflection/XML時收到以下錯誤:

WARNING: The class '_TtCC13WINSystemInfo15RepeaterUpdates9WinSystem' is not key value coding-compliant for the key '__name' There is no support for optional type, array of optionals or enum properties. As a workaround you can implement the function 'setValue forUndefinedKey' for this. See the unit tests for more information

WARNING: The class '_TtCC13WINSystemInfo15RepeaterUpdates17WinSystemRepeater' is not key value coding-compliant for the key 'winSystemRepeater' There is no support for optional type, array of optionals or enum properties. As a workaround you can implement the function 'setValue forUndefinedKey' for this. See the unit tests for more information

這裏是XML的一個片段:

<?xml version="1.0" encoding="UTF-8"?> 
<winSystem> 
    <timeStamp> AS OF:02/15/2017 20:17:01</timeStamp> 
    <winSystemRepeaters> 
     <winSystemRepeater> 
      <node>1000</node> 
      <repeaterId>1000</repeaterId> 
      <grouping>irlp</grouping> 
      <callSign>VE7RHS</callSign> 
      <serviceArea>Vancouver</serviceArea> 
      <serviceState>BC</serviceState> 
      <country>Canada</country> 
      <locationElevation>Vancouver, BC Canada</locationElevation> 
      <latitudeDefault>49.26973</latitudeDefault> 
      <longitudeDefault>-123.24992</longitudeDefault> 
      <freqOffsetPl>145.2700 -600.0000 100.0</freqOffsetPl> 
      <url>http://www.ars.ams.ubc.ca</url> 
      <notes>IRLP Status AS OF:02/15/2017 20:17:01</notes> 
     </winSystemRepeater> 
     <winSystemRepeater> 
      <node>1003</node> 
      <repeaterId>1003</repeaterId> 
      <grouping>irlp</grouping> 
      <callSign>VE7ISC</callSign> 
      <serviceArea>Nanaimo</serviceArea> 
      <serviceState>BC</serviceState> 
      <country>Canada</country> 
      <locationElevation>Nanaimo, BC Canada</locationElevation> 
      <latitudeDefault>49.22750</latitudeDefault> 
      <longitudeDefault>-123.97417</longitudeDefault> 
      <freqOffsetPl>146.6400 -600.0000 0.00</freqOffsetPl> 
      <url>http://ve7na.ca</url> 
      <notes>IRLP Status AS OF:02/15/2017 20:17:01</notes> 
     </winSystemRepeater> 
    </winSystemRepeaters> 
</winSystem> 

我EVObject類定義:

class WinSystem: EVObject { 
    var timeStamp: String? 
    var winSystemRepeaters: [WinSystemRepeater] = [WinSystemRepeater]() 
} 

class WinSystemRepeater: EVObject { 
    var node: String? 
    var repeaterId: NSNumber? 
    var grouping: String? 
    var callSign: String? 
    var serviceArea: String? 
    var serviceState: String? 
    var country: String? 
    var locationElevation: String? 
    var latitudeDefault: NSNumber? 
    var longitudeDefault: NSNumber? 
    var freqOffsetPl: String? 
    var url: String? 
    var notes: String? 
} 

我的功能在通知成功下載XML fi後被調用le:

func buildRepeaterDictionary(notification:Notification) -> Void { 

    SpeedLog.print("Starting buildRepeaterDictionary") 
    SpeedLog.print(notification) 
    guard let userInfo = notification.userInfo, 
     let urlString = userInfo["fileUrlString"] as? String else { 
      SpeedLog.print("No userInfo found in successful notification") 
      return 
    } 

    // Parse XML 
    let xmlUrl = URL(string: urlString) 
    do { 
     let xmlString = try String(contentsOf: xmlUrl!, encoding: String.Encoding.utf8) 
     let repeaters = WinSystem(xmlString: xmlString) 
     SpeedLog.print("Opened file: \(xmlUrl)") 
     SpeedLog.print("Repeaters: \(repeaters)") 
    } 
    catch { 
     SpeedLog.print("Unable to open: \(xmlUrl)") 
    } 
} 

當我在let repeaters = WinSystem(xmlString: xmlString)語句之前打印xmlString時。我得到:

Printing description of xmlString: 
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<winSystem>\n <timeStamp> AS OF:02/16/2017 06:00:01</timeStamp>\n <winSystemRepeaters>\n <winSystemRepeater>\n <node>1000</node>\n <repeaterId>1000</repeaterId>\n <grouping>irlp</grouping>\n <callSign>VE7RHS</callSign>\n <serviceArea>Vancouver</serviceArea>\n <serviceState>BC</serviceState>\n <country>Canada</country>\n <locationElevation>Vancouver, BC Canada</locationElevation>\n <latitudeDefault>49.26973</latitudeDefault>\n <longitudeDefault>-123.24992</longitudeDefault>\n <freqOffsetPl>145.2700 -600.0000 100.0</freqOffsetPl>\n <url>http://www.ars.ams.ubc.ca</url>\n <notes>IRLP Status AS OF:02/16/2017 06:00:01</notes>\n </winSystemRepeater>\n 

我在想什麼?

回答

0

正如您在EVReflection測試AlamofireXmlTests.swift中看到的,您需要在對象中使用名稱__name作爲可變參數。 XMLDictionary庫在某些情況下會將該屬性添加爲節點名稱。

如果你喜歡,你可以通過添加以下代碼段將它寫入到一個不同的屬性對象:

override internal func propertyMapping() -> [(keyInObject: String?, keyInResource: String?)] { 
    return [(keyInObject: "name", keyInResource: "__name")] 
} 

您的其他問題,您winSystemRepeaters是在它的winSystemRepeater數組的對象。所以你需要一個額外的對象來保存列表。所以,你的對象定義爲:

class WinSystem: EVObject { 
    var timeStamp: String? 
    var winSystemRepeaters: WinSystemRepeaters? 
} 

class WinSystemRepeaters: EVObject { 
    var winSystemRepeater: [WinSystemRepeater]? 
} 

class WinSystemRepeater: EVObject { 
    var node: String? 
    var repeaterId: NSNumber? 
    var grouping: String? 
    var callSign: String? 
    var serviceArea: String? 
    var serviceState: String? 
    var country: String? 
    var locationElevation: String? 
    var latitudeDefault: NSNumber? 
    var longitudeDefault: NSNumber? 
    var freqOffsetPl: String? 
    var url: String? 
    var notes: String? 
} 

但是......我不記得有這個解決方法......那麼,爲什麼不是你的情況......你能嘗試命名您WinSystem物業工作winSystemRepeater而不是winSystemRepeaters?

+0

我現在可以在輸入時更改EVObjects後解析XML。我仍然收到錯誤:'警告:類'_TtCC13WINSystemInfo15RepeaterUpdates9WinSystem'不是鍵值編碼兼容的密鑰'__name' 不支持可選類型,可選項或枚舉屬性的數組。 作爲一種解決方法,您可以爲此實現函數'setValue forUndefinedKey'。請參閱單元測試以獲取更多信息 val:'可選(winSystem)'我沒有將val添加到錯誤字符串中。看起來這個值是根元素名稱。 – Kent

相關問題