3

I made simple alarm application. At Swift4 or when swift got main storyboard, I can get current time by using dateformatter. But now I got Xcode(v.11.1) and I can't use dateformatter. my code is very simple and I'm a beginner. if I wrote code this way I got 6 problems.

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()'

I know that it's going to be very simple problem for you I search google, docs even stack but I can't get any correct way to solve this problems.

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()
}
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
확마팍씨
  • 57
  • 1
  • 4

2 Answers2

7

You should define the following code in function

    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
    }

Hope this helps you in fixing issue

Komal Gupta
  • 155
  • 5
  • Wow It works!!!! I just wonder that why I need to create function?? before it works without function right?? is it changed?? – 확마팍씨 Feb 05 '20 at 04:44
  • 1
    Nothing has been changed, it was not working earlier as well. Please see that in second last code line - there was an issue of "dateFormatter" which is now replaced with "timeFormatter". If you want to try without function, replace this variable with one you declared – Komal Gupta Feb 05 '20 at 04:57
  • 3
    Why would you format time with hour and seconds without minutes `"HH:ss"` ? – Leo Dabus Feb 05 '20 at 05:12
7

As of iOS 14, Text has a property named style used to display date.

Text.DateStyle

A predefined style used to display a Date. https://developer.apple.com

You do not need to use DateFormatter. Using Text() with time as style is enough, probably.

Text(Date(), style: .time) 

In your case:

Text(timetext, style: .time)
Text(currenttimetext, style: .time)
mahan
  • 12,366
  • 5
  • 48
  • 83