从 UIDatePicker 快速访问年份 [英] Swift Accessing year from UIDatePicker

查看:26
本文介绍了从 UIDatePicker 快速访问年份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚开始使用 swift 进行 iOS 开发,有时试图查看文档并理解它会有点不知所措.我目前还在 Lynda.com 上进行了 Simon Allardice 的 iOS 应用程序开发和快速基本培训,并对我们在 WhatDay 示例中实例化的对象之一提出了疑问.

Just barely getting into iOS development using swift and getting a little overwhelmed sometimes trying to look at documentation and understand it. I am also currently running through Simon Allardice's iOS app development with swift essential training on Lynda.com and had a question regarding one of the objects we have instantiated in the WhatDay example.

我们基本上是在设置一个 UIDatePicker 对象,从中提取我们使用 NSDateFormatter 对象查看的星期几,此时我想知道,

We basically are setting up a UIDatePicker object from which we are extracting what day of the week we are looking at with the NSDateFormatter object at which point I was wondering,

我们需要访问哪个属性才能获得用户滚动滚轮到的当前年份?

which property would we need to access to get ahold of the current year that the user scrolled the wheel to?

到目前为止,我们有这个代码来访问星期几,

So far we have this code to access the day of the week it was,

@IBACTION func displayDay(sender: AnyObject) {

// grab the selected data from the date picker
var chosenDate = self.datePicker.date

// create an NSDateFormatter
var formatter = NSDateFormatter()
formatter.dateFormat = "EEEE"

// grab the day and create a message
let day = formatter.stringFromDate(chosenDate)
let result = "That was a \(day)"

}

另外,他说我们可以使用日期格式化程序EEEE"来格式化星期几,但我在网上找不到任何关于字符串代码是什么的文档,关于在哪里可以找到这些信息的任何建议?

Also, he says we can use the date formatter "EEEE" to format for day of the week but I couldn't find any documentation on that online what the string codes are, any advice on where to find this information?

推荐答案

您可以使用 NSCalendar 用于日期的组件.
如果你想像你一样格式化你的日期,那么请查看 这个文档以了解不同的日期格式化字符串

// grab the selected data from the date picker
var chosenDate = self.datePicker.date
//use NSCalenda
let myCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)
let myComponents = myCalendar?.components(.WeekdayCalendarUnit | .YearCalendarUnit, fromDate: chosenDate)
let weekDay = myComponents?.weekday
let year = myComponents?.year

println("year:\(year) , weekday:\(weekDay)")

这篇关于从 UIDatePicker 快速访问年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆