Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用Swift将整数转换为特定格式的字符串是否更简单?_Ios_Swift - Fatal编程技术网

Ios 使用Swift将整数转换为特定格式的字符串是否更简单?

Ios 使用Swift将整数转换为特定格式的字符串是否更简单?,ios,swift,Ios,Swift,在我的音频应用程序中,我使用进度滑块播放音频——在UI中,我想显示该集播放的时间。我是这样做的 @objc func updateSlider(){ Slider.value = Float(audioPlayer.currentTime) func secondsToHoursMinutesSeconds (seconds : Int) -> (Int, Int, Int) { return (seconds / 3600

在我的音频应用程序中,我使用进度滑块播放音频——在UI中,我想显示该集播放的时间。我是这样做的

   @objc func updateSlider(){

        Slider.value = Float(audioPlayer.currentTime)

        func secondsToHoursMinutesSeconds (seconds : Int) -> (Int, Int, Int) {
            return (seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60)
        }

        let example = (Float(audioPlayer.currentTime))
        let myIntValue = Int(example)
        self.goneTime.text = String(Float(describing: myIntValue)
此代码动态更新标签,但按照指定的格式(Int,Int,Int)进行更新。示例输出:(1,5,20)当我想要1:5:20时

我试图修改标记为错误的格式(Int/Int/Int)

我发现了一个解决方法——但很难看——使用.replacingOccurrencesOf。从中,它表示可以一次替换字符串的一部分

因此,我将代码更改为:

 func secondsToHoursMinutesSeconds (seconds : Int) -> (Int, Int, Int) {
            return (seconds / 3600, (seconds % 3600) / 60, (seconds % 3600) % 60)
        }

    let example = (Float(audioPlayer.currentTime))
    let myIntValue = Int(example)

    let updated = secondsToHoursMinutesSeconds(seconds: myIntValue)

    let updated2 = String(describing: updated)

    let str2 = updated2.replacingOccurrences(of: ",", with: ":", options:
        NSString.CompareOptions.literal, range: nil)

    let str3 = str2.replacingOccurrences(of: "(", with: "", options:
        NSString.CompareOptions.literal, range: nil)

    self.goneTime.text = str3

这是可行的,但是有没有最佳实践来简化这些类型的修改?斯威夫特和学习新手

在Swift中,您只需使用字符串插值,即可获得所需的任何数据结果,如下所示:

例如:

let val1 = 10
let val2 = 20
let val3 = 30

let result = "\(val1) : \(val2) : \(val3)"
print(result) // it will give output: 10:20:30
希望有帮助

您可以通过以下方式完成此操作:


AVAudioPlayer
currentTime
实例属性返回一个
TimeInterval
(双精度)。您应该使用
DateComponentsFormatter
并将
unitsStyle
设置为位置:

extension Formatter {
    static let positional: DateComponentsFormatter = {
        let formatter = DateComponentsFormatter()
        formatter.unitsStyle = .positional
        return formatter
    }()
}

操场测试:

let seconds: TimeInterval = 3920
let display = Formatter.positional.string(from: seconds)   // "1:05:20"

在您的情况下的用法:

goneTime.text = Formatter.positional.string(from: audioPlayer.currentTime)

为了好玩,您可以用功能性的方式:

let time = [1, 5, 20]
let result = time.reduce("", { $0 + ($0.isEmpty ? "" : ":") + "\($1)" })
print(result) // "1:5:20"
请注意,
“1:5:20”
显示时间间隔的格式不正确这是唯一(且更可取)正确处理组件零填充的解决方案。请注意,
字符串的误用(描述
——从字面上翻译元组的外观——导致两个额外的替换步骤
let time = [1, 5, 20]
let result = time.reduce("", { $0 + ($0.isEmpty ? "" : ":") + "\($1)" })
print(result) // "1:5:20"