[英]'Int' is not identical to 'String' - Swift
在此函数中,我尝试从UIDatePicker获取数据并从中减去5。 例如,如果在UIDatePicker中说7:15 AM,它将从小时数中减去并为2:15 AM。 修改后的代码无法在我给它贴上标签的行中工作,错误提示'Int'与'String'不相同 。 工作代码是原始版本,无论我在“日期选择器”中输入什么内容,它都可以工作,它显示用户选择的日期,因此,如果上午7:15,它将显示为上午7:15,而不会减去任何内容。
与情节提要挂钩的DatePicker
@IBOutlet weak var theDatePicker: UIDatePicker!
不带.toInt()的原始代码
func handler(sender: UIDatePicker) {
var timeFormatter = NSDateFormatter()
timeFormatter.timeStyle = NSDateFormatterStyle.ShortStyle
var strDate = timeFormatter.stringFromDate(theDatePicker.date)
hoursLeftLabel.text = strDate
}
修改后的代码
func handler(sender: UIDatePicker) {
var timeFormatter = NSDateFormatter()
timeFormatter.timeStyle = NSDateFormatterStyle.ShortStyle
var intDate = timeFormatter.stringFromDate(theDatePicker.date).toInt()
hoursLeftLabel.text = intDate
}
如果您有任何疑问或需要任何澄清,请在下方评论
尝试使用:
hoursLeftLabel.text = "\(intDate)"
这应该起作用,因为您创建了一个包含int日期的字符串。
错误Int与字符串不同意味着您在需要字符串的地方传递了Int。 错误的确可以更精确,例如说出错误的类型或类似的内容
编辑:我建议您做这样的日期计算:
let newDate = theDatePicker.date.dateByAddingTimeInterval(-5*60*60) //-5 Hours
//now you can display your date like this:
hoursLeftLabel.text = timeFormatter.stringFromDate(newDate)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.