【问题标题】:Displaying only time from a Date object in Swift/SwiftUI在 Swift/SwiftUI 中仅显示 Date 对象的时间
【发布时间】:2021-02-02 06:24:52
【问题描述】:

我一直在使用结束时间显示音频持续时间。一位优秀的 Stack Overflow 开发人员提出了这个解决方案。

因此,我在我的视图中添加了这个:

struct ContentView: View {
    @ObservedObject var viewModel = MyViewModel() //<- here
    var body: some View {
        VStack {
            
            Text(viewModel.endTime) //<- here
            
        }
    }
}

这是我viewModel中的代码:

class MyViewModel: ObservableObject {
    @Published var endDate: Date? 
    var endTime: String{
        endDate == nil ? "":endDate!.description 
    }

    func play(){
        let path = Bundle.main.path(forResource: "song", ofType:"mp3")!
        let url = URL(fileURLWithPath: path)
        do {
            player = try AVAudioPlayer(contentsOf: url)
            
            endDate = Date() + player.duration
            if player.isPlaying{
                
                player.pause()
                
            }
            else{
                player.play()
                
            }
            isPlaying = player.isPlaying
        }catch{print("error")}

但我得到的时间错误(不是系统时间),但日期正确。我只想显示不带日期的正确时间。

【问题讨论】:

    标签: ios swift swiftui avaudioplayer


    【解决方案1】:

    从 iOS 14+ 开始,Text 有一个名为 style 的属性,用于显示日期。

    文本.日期样式

    用于显示日期的预定义样式。

    init(Date, style: Text.DateStyle)

    创建一个使用特定样式显示本地化日期和时间的实例。 https://developer.apple.com

    您不需要使用DateFormatter。可能使用Text()time 作为样式就足够了。

    Text(Date(), style: .time) 
    

    在你的情况下:

    Text(viewModel.endTime, style: .time)
    

    【讨论】:

      【解决方案2】:

      要显示格式化的日期,您可能需要使用DateFormatter

      let dateFormatter = DateFormatter()
      dateFormatter.dateStyle = .medium
      dateFormatter.timeStyle = .none
       
      let date = Date(timeIntervalSinceReferenceDate: 118800)
       
      // US English Locale (en_US)
      dateFormatter.locale = Locale(identifier: "en_US")
      print(dateFormatter.string(from: date)) // Jan 2, 2001
      

      来源:https://developer.apple.com/documentation/foundation/dateformatter

      (请注意,众所周知,创建 DateFormatter 是一项昂贵的操作,因此请小心不要过于频繁地创建它们)

      您的视图模型中也存在一些逻辑错误。以下是如何处理此问题的示例:

      struct ContentView : View {
          @ObservedObject private var vm = MyViewModel()
          
          var body: some View {
              Button(action:{
                  vm.play()
              } ) {
                  Text(vm.isPlaying ? "Stop" : "Play")
              }
              Text(vm.endTime)
          }
      }
      
      class MyViewModel: ObservableObject {
          @Published var endDate: Date?
          @Published var isPlaying = false
          
          var player = AVAudioPlayer()
          var dateFormatter = DateFormatter()
          
          var endTime: String{
              guard let endDate = endDate else {
                  return ""
              }
              dateFormatter.dateStyle = .none
              dateFormatter.timeStyle = .medium
              return dateFormatter.string(from: endDate)
          }
      
          func play(){
              do{
                  if player.isPlaying {
                      player.pause()
                  } else {
                      let path = Bundle.main.path(forResource: "All Of Me Final Mix", ofType:"mp3")!
                      let url = URL(fileURLWithPath: path)
                      player = try AVAudioPlayer(contentsOf: url)
                      endDate = Date() + player.duration - player.currentTime
                      player.play()
                  }
                  isPlaying = player.isPlaying
              } catch {
                  print("error: \(error)")
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多