swift 中无法调用 'stringFromDate' 错误

cannot invoke 'stringFromDate' error in swift

我正在尝试在 Swift 中使用简单的日期格式,但出现以下错误:

"Cannot invoke 'stringFromDate' with an argument list of type '(NSDate?!)'"

我的代码如下:

override func tableView(tableView: UITableView, cellForRowAtIndexPath 

    indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", 

    forIndexPath: indexPath) as! UITableViewCell

    // Configure the cell...

    let dateFormatter = NSDateFormatter()

    dateFormatter.dateFormat = ("dd-MMM-yyyy")

    var myUsers: AnyObject = users[indexPath.row]

    let strDate = dateFormatter.stringFromDate(myUsers.createdAt)


    return cell
}

}

有谁知道为什么我不能在 Swift 中执行此操作?我以前用过这种方法,效果很好。我搜索了其他答案,但没有找到合适的答案。

感谢任何可用的帮助。非常感谢

检查以下代码:

    var dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"
    let d = NSDate()
    let s = dateFormatter.stringFromDate(d)
    println(s)

这是你的第一个 post:

这行代码没有正确设置格式,因为调用它是为了从 String 转换为 Date:

dateFormatter.dateFromString("dd-MMM-yyyy")

所以,你必须先这样设置格式,这只是一个小错误,下次小心:

dateFormatter.dateFormat = "yyyy-MM-dd"

这是您编辑后 post 的:

这里有一个明显的错误:

var myUsers: AnyObject = users[indexPath.row]

AnyObject没有属性createdAt。这就是为什么你不能得到它的权利。并确保 users 数组是 'PFUser' 的数组。否则还是会报错