Swift for iOS:更改数组顺序并填充选择器

Swift for iOS: change array order and populate Picker

我正在尝试在 Swift 中为 iOS 编写一个摄影应用程序,为指定的 ƒ 光圈和快门速度建立等效的曝光设置。编写此应用程序的几个下游部分我尚未触及(因此您不会看到明显组件的代码,例如用户输入或快门速度)。我需要帮助的是如何根据(最终)用户定义的输入值更改数组的顺序。

我能够在 Playground 中成功实现这一点,但是当我尝试将其应用于 ViewController.swift 以填充选择器时,我 运行 遇到了问题。

这是我的游乐场代码:

var setƒstop = 1.4
var ƒstopout = [Double]()

func changeRange(inout setƒstop: (Double), inout ƒstopout: [Double]) {

if setƒstop == 1.0 {
    ƒstopout = [1,1.4,2,2.8,4,5.6,8,11,16,22,32,44,64,88,128]
} else if setƒstop == 1.4 {
    ƒstopout = [1.4,2,2.8,4,5.6,8,11,16,22,32,44,64,88,128,1]
} else if setƒstop == 2.0 {
    ƒstopout = [2,2.8,4,5.6,8,11,16,22,32,44,64,88,128,1,1.4]
} else if setƒstop == 2.8 {
    ƒstopout = [2.8,4,5.6,8,11,16,22,32,44,64,88,128,1,1.4,2]
} else if setƒstop == 4.0 {
    ƒstopout = [4,5.6,8,11,16,22,32,44,64,88,128,1,1.4,2,2.8]
} else if setƒstop == 5.6 {
    ƒstopout = [5.6,8,11,16,22,32,44,64,88,128,1,1.4,2,2.8,4]
} else if setƒstop == 8.0 {
    ƒstopout = [8,11,16,22,32,44,64,88,128,1,1.4,2,2.8,4,5.6]
} else if setƒstop == 11.0 {
    ƒstopout = [11,16,22,32,44,64,88,128,1,1.4,2,2.8,4,5.6,8]
} else if setƒstop == 16.0 {
    ƒstopout = [16,22,32,44,64,88,128,1,1.4,2,2.8,4,5.6,8,11]
} else if setƒstop == 22.0 {
    ƒstopout = [22,32,44,64,88,128,1,1.4,2,2.8,4,5.6,8,11,16]
} else if setƒstop == 32.0 {
    ƒstopout = [32,44,64,88,128,1,1.4,2,2.8,4,5.6,8,11,16,22]
} else if setƒstop == 44.0 {
    ƒstopout = [44,64,88,128,1,1.4,2,2.8,4,5.6,8,11,16,22,32]
} else if setƒstop == 64.0 {
    ƒstopout = [64,88,128,1,1.4,2,2.8,4,5.6,8,11,16,22,32,44]
} else if setƒstop == 88.0 {
    ƒstopout = [88,128,1,1.4,2,2.8,4,5.6,8,11,16,22,32,44,64]
} else if setƒstop == 128.0 {
    ƒstopout = [128,1,1.4,2,2.8,4,5.6,8,11,16,22,32,44,64,88]
}
}

changeOrder(&setƒstop, &ƒstopout)


println(ƒstopout)
println(ƒstopout[0])

这是我的 ViewController.swift 代码:

import UIKit

var setƒstop = 1.4
var ƒstopout = [Double]()

func changeRange(inout setƒstop: (Double), inout ƒstopout: [Double]) {

if setƒstop == 1.0 {
    ƒstopout = [1,1.4,2,2.8,4,5.6,8,11,16,22,32,44,64,88,128]
} else if setƒstop == 1.4 {
    ƒstopout = [1.4,2,2.8,4,5.6,8,11,16,22,32,44,64,88,128,1]
} else if setƒstop == 2.0 {
    ƒstopout = [2,2.8,4,5.6,8,11,16,22,32,44,64,88,128,1,1.4]
} else if setƒstop == 2.8 {
    ƒstopout = [2.8,4,5.6,8,11,16,22,32,44,64,88,128,1,1.4,2]
} else if setƒstop == 4.0 {
    ƒstopout = [4,5.6,8,11,16,22,32,44,64,88,128,1,1.4,2,2.8]
} else if setƒstop == 5.6 {
    ƒstopout = [5.6,8,11,16,22,32,44,64,88,128,1,1.4,2,2.8,4]
} else if setƒstop == 8.0 {
    ƒstopout = [8,11,16,22,32,44,64,88,128,1,1.4,2,2.8,4,5.6]
} else if setƒstop == 11.0 {
    ƒstopout = [11,16,22,32,44,64,88,128,1,1.4,2,2.8,4,5.6,8]
} else if setƒstop == 16.0 {
    ƒstopout = [16,22,32,44,64,88,128,1,1.4,2,2.8,4,5.6,8,11]
} else if setƒstop == 22.0 {
    ƒstopout = [22,32,44,64,88,128,1,1.4,2,2.8,4,5.6,8,11,16]
} else if setƒstop == 32.0 {
    ƒstopout = [32,44,64,88,128,1,1.4,2,2.8,4,5.6,8,11,16,22]
} else if setƒstop == 44.0 {
    ƒstopout = [44,64,88,128,1,1.4,2,2.8,4,5.6,8,11,16,22,32]
} else if setƒstop == 64.0 {
    ƒstopout = [64,88,128,1,1.4,2,2.8,4,5.6,8,11,16,22,32,44]
} else if setƒstop == 88.0 {
    ƒstopout = [88,128,1,1.4,2,2.8,4,5.6,8,11,16,22,32,44,64]
} else if setƒstop == 128.0 {
    ƒstopout = [128,1,1.4,2,2.8,4,5.6,8,11,16,22,32,44,64,88]
}
}


class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

override func viewDidLoad() {
    super.viewDidLoad()
    changeRange(&setƒstop, &ƒstopout)
}

var arrayƒstop = ["\(ƒstopout[0])","\(ƒstopout[1])","\(ƒstopout[2])"] // etc. A truncated array is being used here for testing purposes.

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return arrayƒstop.count
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    return arrayƒstop[row]
}
}

我已经尝试了几种变体,但这是我唯一没有遇到编译错误的版本。相反,当我尝试 运行 模拟器时,它崩溃并出现错误:

fatal error: Array index out of range
(lldb)

意味着 var arrayƒstop 没有填充 func changeRangeƒstopout 值,而是引用 var ƒstopout = [Double]().

请协助。

不要用固定数字分配 arrayƒstop,而是尝试使用 for-loop 或:

let arrayƒstop = ƒstopout.map { String(stringInterpolationSegment: [=10=]) }

因为您要将 Double 转换为 String,所以您需要为其提供如下格式:String(format: "%.1f", [=15=]) 或使用 stringInterpolationSegment,如例如。

你的 ƒstopout 数组是空的,这就是它导致崩溃的原因。 您应该在调用 changeRange 方法后初始化 arrayƒstop,似乎您在那里初始化了它的数据。

之后只需重新加载您的 pickerView 的数据。

为了确定我在说什么,当您以这种方式声明它时初始化您的 ƒstopout 例如:

var ƒstopout = [1,1.4,2,2.8,4,5.6,8,11,16,22,32,44,64,88,128]

你不会遇到崩溃。

如果我可以提出建议:

extension Array {
    func rotated(steps : Int) -> [T] {
        return [T]( self[steps..<self.count] + self[0..<steps] )
    }
}

func indexOf<T: Equatable>(ary : [T], theItem : T) -> Int? {
    for (i, obj) in enumerate(ary) {
        if obj == theItem {
            return i
        }
    }
    return nil
}


let ƒstops = [1,1.4,2,2.8,4,5.6,8,11,16,22,32,44,64,88,128]

let ƒstopout = ƒstops.rotated( indexOf(ƒstops, setƒstop) ?? 0 )

这将消除你极其危险和容易出错的条件 if-then thunka-thunkas。

然后在你看来你会做这样的事情:

var arrayƒstop = ƒstopout.map { "\([=11=])" }

一般来说,您不应该对数组索引进行硬编码。