【问题标题】:C-Style for statement deprecated in swift 3 [duplicate]swift 3中不推荐使用C样式的语句[重复]
【发布时间】:2017-02-06 07:22:58
【问题描述】:

由于 swift 3 中不推荐使用 C 风格的 for 语句,我该如何更改代码?以下代码行出现错误:

for var frame = 0; frame <= frameCount; frame += 1

func keyframePathsWithDuration(_ duration: CGFloat, lastUpdatedAngle: CGFloat, newAngle: CGFloat, radius: CGFloat, type: RMIndicatorType) -> [CGPath] {
    let frameCount: Int = Int(ceil(duration * 60))
    var array: [CGPath] = []
    for var frame = 0; frame <= frameCount; frame += 1 {
        let startAngle = degreeToRadian(-90)

        let angleChange = ((newAngle - lastUpdatedAngle) * CGFloat(frame))
        let endAngle = lastUpdatedAngle + (angleChange / CGFloat(frameCount))
        array.append((self.pathWithStartAngle(startAngle, endAngle: endAngle, radius: radius, type: type).cgPath))
    }
    return array
}

【问题讨论】:

  • 这样试试for frame in 0...frameCount

标签: swift loops swift3


【解决方案1】:

试试 Swift 3/4 的新语法

func keyframePathsWithDuration(_ duration: CGFloat, lastUpdatedAngle: CGFloat, newAngle: CGFloat, radius: CGFloat, type: RMIndicatorType) -> [CGPath] {
            let frameCount: Int = Int(ceil(duration * 60))
            var array: [CGPath] = []
            for frame in 0...frameCount {
                let startAngle = degreeToRadian(-90)

                let angleChange = ((newAngle - lastUpdatedAngle) * CGFloat(index))
                let endAngle = lastUpdatedAngle + (angleChange / CGFloat(frameCount))
                array.append((self.pathWithStartAngle(startAngle, endAngle: endAngle, radius: radius, type: type).cgPath))
            }
            return array
}

有关新循环语法的更多信息是:

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ControlFlow.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-10
    • 1970-01-01
    • 2017-07-12
    • 2017-03-26
    • 1970-01-01
    • 2012-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多