如何在 Swift (Xcode11.1) 中获取当前时间
How can I get current time in Swift (Xcode11.1)
我做了一个简单的闹钟应用。
在 Swift4 或 swift 获得主情节提要时,我可以使用 dateformatter 获取当前时间。
但是现在我得到了 Xcode(v.11.1),我不能使用 dateformatter。
我的代码非常简单,我是初学者。
如果我这样写代码,我会遇到 6 个问题。
Consecutive declarations on a line must be separated by ';'
Expected '(' in argument list of function declaration
Expected '{' in body of function declaration
Expected 'func' keyword in instance method declaration
Expected declaration
Invalid redeclaration of 'dateFormatter()'
我知道这对你来说将是一个非常简单的问题
我搜索 google,文档甚至堆栈,但我找不到任何正确的方法来解决这个问题。
import SwiftUI
import Foundation
struct Alarm_view: View {
let time = Date()
let timeFormatter = DateFormatter()
timeFormatter.dateFormat = "HH:ss"
let stringDate = dateFormatter.string(from: time)
@State var selecttimes = Date()
@State var sound = 1
@State var currenttimes = NSDate()
@State var currenttimetext = Date()
@State var timetext = Date()
var body: some View {
VStack {
NavigationView{
Form {
Section {
DatePicker(selection: $selecttimes,displayedComponents: .hourAndMinute, label: { Text("tamest") })
Picker(selection: $sound, label: Text("alarm")) {
/*@START_MENU_TOKEN@*/Text("1").tag(1)/*@END_MENU_TOKEN@*/
/*@START_MENU_TOKEN@*/Text("2").tag(2)/*@END_MENU_TOKEN@*/
Text("3").tag(3)
}
}
}.navigationBarTitle(Text("alarm set"))
}
VStack {
Button("start") {
self.timetext = self.selecttimes
}.foregroundColor(Color.black).offset(y:-5)
Button(action: /*@START_MENU_TOKEN@*/{}/*@END_MENU_TOKEN@*/) {
Text("stop")
.foregroundColor(Color.red)
}
}.offset(y: -10)
Text("\(timetext)")
Text("\(currenttimetext)")
}
}
}
struct Alarm_view_Previews: PreviewProvider {
static var previews: some View {
Alarm_view()
}
}
你应该在函数中定义如下代码
let time = Date()
let timeFormatter = DateFormatter()
timeFormatter.dateFormat = "HH:ss"
let stringDate = timeFormatter.string(from: time)
func getDate()->String{
let time = Date()
let timeFormatter = DateFormatter()
timeFormatter.dateFormat = "HH:ss"
let stringDate = timeFormatter.string(from: time)
return stringDate
}
希望这能帮助您解决问题
从 iOS 14 开始,Text 有一个 属性 命名样式用于显示日期。
Text.DateStyle
A predefined style used to display a Date. https://developer.apple.com
您不需要使用 DateFormatter
。使用 Text()
和 time
作为样式就足够了,可能。
Text(Date(), style: .time)
你的情况:
Text(timetext, style: .time)
Text(currenttimetext, style: .time)
我做了一个简单的闹钟应用。 在 Swift4 或 swift 获得主情节提要时,我可以使用 dateformatter 获取当前时间。 但是现在我得到了 Xcode(v.11.1),我不能使用 dateformatter。 我的代码非常简单,我是初学者。 如果我这样写代码,我会遇到 6 个问题。
Consecutive declarations on a line must be separated by ';'
Expected '(' in argument list of function declaration
Expected '{' in body of function declaration
Expected 'func' keyword in instance method declaration
Expected declaration
Invalid redeclaration of 'dateFormatter()'
我知道这对你来说将是一个非常简单的问题 我搜索 google,文档甚至堆栈,但我找不到任何正确的方法来解决这个问题。
import SwiftUI
import Foundation
struct Alarm_view: View {
let time = Date()
let timeFormatter = DateFormatter()
timeFormatter.dateFormat = "HH:ss"
let stringDate = dateFormatter.string(from: time)
@State var selecttimes = Date()
@State var sound = 1
@State var currenttimes = NSDate()
@State var currenttimetext = Date()
@State var timetext = Date()
var body: some View {
VStack {
NavigationView{
Form {
Section {
DatePicker(selection: $selecttimes,displayedComponents: .hourAndMinute, label: { Text("tamest") })
Picker(selection: $sound, label: Text("alarm")) {
/*@START_MENU_TOKEN@*/Text("1").tag(1)/*@END_MENU_TOKEN@*/
/*@START_MENU_TOKEN@*/Text("2").tag(2)/*@END_MENU_TOKEN@*/
Text("3").tag(3)
}
}
}.navigationBarTitle(Text("alarm set"))
}
VStack {
Button("start") {
self.timetext = self.selecttimes
}.foregroundColor(Color.black).offset(y:-5)
Button(action: /*@START_MENU_TOKEN@*/{}/*@END_MENU_TOKEN@*/) {
Text("stop")
.foregroundColor(Color.red)
}
}.offset(y: -10)
Text("\(timetext)")
Text("\(currenttimetext)")
}
}
}
struct Alarm_view_Previews: PreviewProvider {
static var previews: some View {
Alarm_view()
}
}
你应该在函数中定义如下代码
let time = Date()
let timeFormatter = DateFormatter()
timeFormatter.dateFormat = "HH:ss"
let stringDate = timeFormatter.string(from: time)
func getDate()->String{
let time = Date()
let timeFormatter = DateFormatter()
timeFormatter.dateFormat = "HH:ss"
let stringDate = timeFormatter.string(from: time)
return stringDate
}
希望这能帮助您解决问题
从 iOS 14 开始,Text 有一个 属性 命名样式用于显示日期。
Text.DateStyle
A predefined style used to display a Date. https://developer.apple.com
您不需要使用 DateFormatter
。使用 Text()
和 time
作为样式就足够了,可能。
Text(Date(), style: .time)
你的情况:
Text(timetext, style: .time)
Text(currenttimetext, style: .time)