XML 解析中 URL 没有响应 - Alamofire Swift 5

No response from URL in XML parsing - Alamofire Swift 5

我正在使用 XMLMapper with AlamoFire 请求来获得响应。 URL 在浏览器上运行良好,但当我尝试在 swift 中使用时,它会跳过该功能。即使我无法调试响应。它在解析之前,没有从 URL 获得任何数据或响应。有什么想法吗??

func xmlParser() {
        let urlXml = "https://images.apple.com/main/rss/hotnews/hotnews.rss"

        Alamofire.request(urlXml, method: .get).responseXMLObject { (response: DataResponse<RSSFeed>) in
            let rssFeed = response.result.value
            print(rssFeed?.channel?.items?.first?.title ?? "nil")
        }
    }

看来你的模型有问题。我设法映射了这个 link.

的响应

尝试使用以下结构并将其与您的进行比较以了解差异:

class RSSFeed: XMLMappable {
    var nodeName: String!

    var channel: Channel?

    required init?(map: XMLMap) {}

    func mapping(map: XMLMap) {
        channel <- map["channel"]
    }
}

class Channel: XMLMappable {
    var nodeName: String!

    var title: String?
    var link: URL?
    var description: String?
    var language: String?
    var copyright: String?
    var pubDate: String?
    var lastBuildDate: String?
    var category: String?
    var generator: String?
    var docs: URL?
    var items: [Item]?

    required init?(map: XMLMap) {}

    func mapping(map: XMLMap) {
        title <- map["title"]
        link <- (map["link"], XMLURLTransform())
        description <- map["description"]
        language <- map["language"]
        copyright <- map["copyright"]
        pubDate <- map["pubDate"]
        lastBuildDate <- map["lastBuildDate"]
        category <- map["category"]
        generator <- map["generator"]
        docs <- (map["docs"], XMLURLTransform())
        items <- map["item"]
    }
}

class Item: XMLMappable {
    var nodeName: String!

    var title: String?
    var link: URL?
    var description: String?
    var pubDate: String?

    required init?(map: XMLMap) {}

    func mapping(map: XMLMap) {
        title <- map["title"]
        link <- (map["link"], XMLURLTransform())
        description <- map["description"]
        pubDate <- map["pubDate"]
    }
}

希望对您有所帮助。