iosswiftxmlalamofirexmlmapper

No response from URL in XML parsing - Alamofire Swift 5


I am using XMLMapper with AlamoFire request to get response. URL is working fine on browsers but when I try to use in swift it skips the function. Even I cannot debug the response. Its before parsing, not getting any data or response from the URL. Any idea ??

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")
        }
    }

Solution

  • It seems that something is wrong with your model. I managed to map the response from this link.

    Try using the following structure and compare it with yours to see the difference:

    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"]
        }
    }
    

    Hope this helps.