ios - 从解析中获取日期并将其显示为字符串 Swift

标签 ios swift parse-platform nsdate

就像标题所说,我正在从解析中获取信息并显示它,但我不知道如何使用日期来做到这一点。我需要将其覆盖为字符串以显示在标签中。我做了一些研究并了解我需要使用日期格式化程序,但我不明白我把它放在哪里以及格式化后我如何显示它。任何帮助将非常感激!

var names = [String]()
var locations = [String]()
var dates = [NSDate]()
var imageFiles = [PFFile]()
var abouts = [String]()

override func viewDidLoad() {
    super.viewDidLoad()

    refresher = UIRefreshControl()
    refresher.attributedTitle = NSAttributedString(string: "Pull to refresh")
    refresher.addTarget(self, action: "refresh", forControlEvents: UIControlEvents.ValueChanged)
    self.tableView.addSubview(refresher)
    refresh()

    PFGeoPoint.geoPointForCurrentLocationInBackground { (geoPoint, error) -> Void in
        if let geoPoint = geoPoint {
            PFUser.currentUser()?["location"] = geoPoint
            PFUser.currentUser()?.saveInBackground()

    var getLocalPostsQuery = PFQuery(className: "publicPosts")
    if let latitude = PFUser.currentUser()!["location"].latitude {
        if let longitude = PFUser.currentUser()!["location"].longitude {
    getLocalPostsQuery.whereKey("searchLocation", withinGeoBoxFromSouthwest: PFGeoPoint(latitude: latitude - 0.5, longitude: longitude - 0.5), toNortheast:  PFGeoPoint(latitude: latitude + 0.5, longitude: longitude + 0.5))
    getLocalPostsQuery.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
        if let objects = objects {

                self.names.removeAll(keepCapacity: true)
                self.locations.removeAll(keepCapacity: true)
                self.abouts.removeAll(keepCapacity: true)
                self.dates.removeAll(keepCapacity: true)
                self.imageFiles.removeAll(keepCapacity: true)

                for object in objects {

                    self.names.append(object["name"] as! String)
                    self.locations.append(object["location"] as! String)
                    self.dates.append(object["date"] as! String)
                    self.abouts.append(object["about"] as! String)
                    self.imageFiles.append(object["imageFile"] as! PFFile)
                    //self.attendings.append(object["attending"].count)
                    self.tableView.reloadData()
                            }
                        }
                    }
                }
            }
        }
    }
}

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

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return names.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let LECell = tableView.dequeueReusableCellWithIdentifier("LocalPostsCell", forIndexPath: indexPath) as! LocalPostsTableViewCell

    imageFiles[indexPath.row].getDataInBackgroundWithBlock { (data, error) -> Void in
        if let downloadedImage = UIImage(data: data!) {

            LECell.postImage.image = downloadedImage
        }
    }


    LECell.postName.text = names[indexPath.row]
    LECell.postLocation.text = locations[indexPath.row]
    LECell.postDate.text = dates[indexPath.row]

    return LECell
}

let localPostsDetailSegue = "showLocalPostsDetailView"

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == localPostsDetailSegue {
        let detailScene = segue.destinationViewController as! LocalPostsDetailViewController

        if let indexPath = self.tableView.indexPathForSelectedRow {
            let row = Int(indexPath.row)
            detailScene.name = names[row]
            detailScene.location = locations[row]
            detailScene.date = dates[row]
            detailScene.about = abouts[row]
            detailScene.photo = imageFiles[row]
        }
    }
}
}

最佳答案

您可以轻松地从 NSDate 获取字符串并显示它。

func getStringFromDate(date:NSDate)->String{

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM/dd/yy, H:mm"
return dateFormatter.stringFromDate(yourDate) // yourDate is your parse date
}

你也可以做一个分类。

关于ios - 从解析中获取日期并将其显示为字符串 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34194409/

相关文章:

parse-platform - 成为服务器环境中的用户来测试云代码

ios - 使用 SpriteKit 触摸屏幕时不会返回正确的信息

ios - 此导航器 3 缺少导航 Prop

ios - viewContext 未保存在数据库中 (app.sqlite)

swift - 以编程方式控制 macOS 鼠标 - Swift 4

ios - 解析 PFQuery 返回 nil (swift)

ios - 在 nil 中的 uiview 之间传输数据

ios - 从 UITableViewCell 中的 UITextField 访问文本

swift - 有没有办法查看程序集文件?

javascript - PFUser 上的 AfterSave 检查是否在创建或更新