Ios Can';在XMLparser之后从字符串转换日期

Ios Can';在XMLparser之后从字符串转换日期,ios,swift,date,nsdateformatter,nsxmlparser,Ios,Swift,Date,Nsdateformatter,Nsxmlparser,我做了一个RSSParser,我不想将日期从yyyy-MM-dd't'HH:MM:ssZ转换为dd/MM/yyyy: func setDateForCell(cell:ActuTblCell, indexPath:NSIndexPath) { let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ" let dateString = posts.obj

我做了一个RSSParser,我不想将日期从
yyyy-MM-dd't'HH:MM:ssZ
转换为
dd/MM/yyyy

func setDateForCell(cell:ActuTblCell, indexPath:NSIndexPath) {
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
    let dateString = posts.objectAtIndex(indexPath.row).valueForKey("date") as! String
    if let dateAdded = dateFormatter.dateFromString(dateString)
    {
        dateFormatter.dateFormat = "dd/MM/yyyy"
        cell.dateActuCell?.text = "\(dateFormatter.stringFromDate(dateAdded))"
    }
    //cell.dateActuCell?.text = posts.objectAtIndex(indexPath.row).valueForKey("date") as? String
}
但是当我放置断点时,我的函数不会进入if条件

import UIKit

@objc
protocol ActualitesViewControllerDelegate {
    optional func toggleLeftPanel()
    optional func collapseSidePanels()
}

class ActualitesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, NSXMLParserDelegate {
    @IBOutlet var tableView: UITableView!

    var parser = NSXMLParser()
    var posts = NSMutableArray()
    var elements = NSMutableDictionary()
    var element = NSString()
    var title1 = NSMutableString()
    var date = NSMutableString()
    var dscrptn = NSMutableString()

    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.navigationController?.navigationBar.barTintColor = UIColor(red: 38.0/255.0, green: 51.0/255.0, blue: 85.0/255.0, alpha: 1.0)
        self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Gotham", size: 13)!, NSForegroundColorAttributeName : UIColor.whiteColor()]
        self.title = "ACTUALITÉS"

        self.beginParsing()
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func beginParsing()
    {
        posts = []
        parser = NSXMLParser(contentsOfURL:(NSURL(string:"http://www.solutis.fr/actualites-rachat-credit,rss.html"))!)!
        parser.delegate = self
        parser.parse()

        tableView!.reloadData()
    }

    //XMLParser Methods

    func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String])
    {
        element = elementName
        if (elementName as NSString).isEqualToString("item")
        {
            elements = NSMutableDictionary()
            elements = [:]
            title1 = NSMutableString()
            title1 = ""
            date = NSMutableString()
            date = ""
            dscrptn = NSMutableString()
            dscrptn = ""
        }
    }

    func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?)
    {
        if (elementName as NSString).isEqualToString("item") {
            if !title1.isEqual(nil) {
                elements.setObject(title1, forKey: "title")
            }
            if !date.isEqual(nil) {
                elements.setObject(date, forKey: "date")
            }
            if !dscrptn.isEqual(nil) {
                elements.setObject(dscrptn, forKey: "dscrptn")
            }

            posts.addObject(elements)
        }
    }

    func parser(parser: NSXMLParser, foundCharacters string: String)
    {
        if element.isEqualToString("title") {
            title1.appendString(string)
        } else if element.isEqualToString("pubDate") {
            date.appendString(string)
        } else if element.isEqualToString("description") {
            dscrptn.appendString(string)
        }
    }

    //Tableview Methods

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return posts.count
    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }

    func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 10
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        return basicCellAtIndexPath(indexPath)
    }


    func basicCellAtIndexPath(indexPath:NSIndexPath) -> ActuTblCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! ActuTblCell

        setTitleForCell(cell, indexPath: indexPath)
        setDateForCell(cell, indexPath: indexPath)
        setDescriptionForCell(cell, indexPath: indexPath)
        return cell
    }

    func setTitleForCell(cell:ActuTblCell, indexPath:NSIndexPath) {
        cell.titleActuCell?.text = posts.objectAtIndex(indexPath.row).valueForKey("title") as! NSString as String
    }

    func setDateForCell(cell:ActuTblCell, indexPath:NSIndexPath) {
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
        let dateString = posts.objectAtIndex(indexPath.row).valueForKey("date") as! String
        if let dateAdded = dateFormatter.dateFromString(dateString)
        {
            dateFormatter.dateFormat = "dd/MM/yyyy"
            cell.dateActuCell?.text = "\(dateFormatter.stringFromDate(dateAdded))"
        }
        //cell.dateActuCell?.text = posts.objectAtIndex(indexPath.row).valueForKey("date") as? String
    }

    func setDescriptionForCell(cell:ActuTblCell, indexPath:NSIndexPath) {
        cell.descriptionActuCell?.text = posts.objectAtIndex(indexPath.row).valueForKey("dscrptn") as! NSString as String
    }
}
如果我这样做:

print(posts.objectAtIndex(indexPath.row).valueForKey("date") as? String)
我得到:


可选(“2015-10-19T10:59:53+00:00\n\t\t\t”)

我在这里回答问题是新手,但我想帮忙

我猜您的dateFormatter没有解析您传入的日期字符串

我会打印出日期(即,在从对象中获取日期字符串后插入debugPrint),并检查其格式是否与此行中所需的格式相同:
dateFormatter.dateFormat=“yyyy-MM-dd'T'HH:MM:ssZ”


如果字符串匹配正确,则应显示为“2015-11-02'T'01:22:18-08:00”。

获取的字符串包含空格和换行符。
在将字符串传递给日期格式化程序之前修剪字符串

let dateString = posts.objectAtIndex(indexPath.row).objectForKey("date") as! String
if let dateAdded = dateFormatter.dateFromString(dateString.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) { ...

请添加接收日期字符串的示例。旁注:除非您真的需要KVC方法,否则请使用
objectForKey:
而不是
valueForKey:
。@vadian看我的编辑请完善,我没有想过打印我的日期,因为我正在学习单元格中显示的日期,谢谢。我还没有打印出来,另一个答案帮助了我,它们包含空格和换行符。太好了!现在您对如何调试代码有了更多的了解:)