我正在尝试用iOS版的Swift编写一个摄影应用程序,为指定的“停止”和快门速度建立等效的曝光设置。有几个下游部分来编写这个应用程序,我还没有达到(因此你不会看到明显的组件,如用户输入或快门速度的代码)。我需要帮助的是如何根据(最终)用户定义的输入值更改数组的顺序。
我在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
不是用每个ƒstopout
的func changeRange
值填充,而是引用var ƒstopout = [Double]()
。请协助。
最佳答案
如果我可以提个建议:
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 )
这将消灭你的极端危险和容易出错的条件,如果然后图卡图卡。
然后在你看来,你会做如下事情:
var arrayƒstop = ƒstopout.map { "\($0)" }
一般来说,您不应该硬编码数组索引。
关于ios - iOS的Swift:更改数组顺序并填充Picker,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29998766/