I want to set the timer at a certain time.I'd like to use calendar type as a means of doing so, but I can't do it well because I get an error indication.Specifically, there are three parts of the error:
Error 1
calendar.timeZone=.current
// Error
Consecutive declarations on a line must be separated by ';'
Expected'('in argument list of function declaration')
Expected '{' in body of function decimation
Expected 'func' keyword in instance method decoding
Expected declaration
Invalid redeclaration of 'calendar()'
Error 2
let condition 1 = calendar.date (from: DateComponents (year: 2021, month: 2, day:11, hour:9, minute:00, second:00))!
// Error
Cannot use instance member 'calendar' with in property initializer; property initializers run before 'self' is available
Error 3
//
if now > code 1
// Error
Expected declaration
//
// Source Code
import UIKit
import AVFoundation
classViewController:UIViewController {
varaudioPlayer —AVAudioPlayer!
override func viewDidLoad(){
super.viewDidLoad()
// Do any additional setup after loading the view.
}
// get the current date
let now = Date()
// set the timer start time
var calendar = Calendar (identifier: .gregorian)
calendar.timeZone=.current
calendar.locale=.current
let condition 1 = calendar.date (from: DateComponents (year: 2021, month: 2, day:11, hour:9, minute:00, second:00))!
if now > code 1 {
playSound(name: "onsei")
}
}
extension ViewController:AVAudioPlayerDelegate{
funcplaySound(name:String){
guard let path = Bundle.main.path(forResource:name, ofType:"m4a") else {
print("Sound source file not found")
return
}
do{
// AVAudioPlayer instantiation
audioPlayer=try AVAudioPlayer(contentsOf:URL(fileURLWithPath:path))
// Set the AVAudioPlayer Deligate
audioPlayer.delegate=self
// Audio Playback
audioPlayer.play()
} catch{
}
}
}
"I would like to use the calendar type as a means of doing so, but I can't do it well because I get an error indication."
"I want to set the timer at a specific time" is an annotation to clarify the intent of the code, but if you want to ask about itself, you'd better look at to see if iOS can do that in the first place and have a new thread created separately.
In your code, let now=Date()
and var calendar=Calendar(identifier:.gregorian)
are variable declarations, but expressions such as calendar.timeZone=.current
are not declarations but execution statements.
This is common in many class-based languages, not just Swift, but
The execution statement you wrote is not in the method and that is the reason for the error.
(If you think you should send an error message that makes you understand that a little more, send a bug report to bugs.swift.org.)
For example, if you create a method and move all the statements containing the execution statements into the method, you will not get any errors as follows:
class ViewController:UIViewController {
//...
checkTimeToPlaySound(){
// get the current date
let now = Date()
// set the timer start time
var calendar = Calendar (identifier: .gregorian)
calendar.timeZone=.current
calendar.locale=.current
let condition 1 = calendar.date (from: DateComponents (year: 2021, month: 2, day:11, hour:9, minute:00, second:00))!
if now>condition1 {//<- This is `condition1`
playSound(name: "onsei")
}
}
}
However, this method checkTimeToPlaySound()
will never run unless someone calls it or "set it up for someone to call it."
(For example, viewDidLoad()
is "iOS will call you at a specific time")
When you write programs for environments that are managed by complex GUI frameworks such as iOS, "write code like that in the right place and the system will run it well."
It is necessary to say thatRegarding the second point, "Set to run under those conditions", please check how you can set it up (or if you can't do anything about it) and ask a separate question.
© 2024 OneMinuteCode. All rights reserved.