GarySabo
GarySabo

Reputation: 6680

Is it possible to iterate through a value in a struct like you can with an array/dictionary in Swift?

I have a UIPickerView, and want to iterate through mdCounty.name for my rows, but cannot get anything to work. If not, what would be a more appropriate element to store the data?

This is my model data:

struct mdCounty {
    var name: String?
    var countyRecordation: Double = 0
    var countyTransfer: Double = 0
    var countyDeduction: Double = 0
}


let garrett = mdCounty(name: "Garrett", countyRecordation: 7.0, countyTransfer: 0.01, countyDeduction: 50000)
let allegany = mdCounty(name: "Allegany", countyRecordation: 6.50, countyTransfer: 0.005, countyDeduction: 30000)
let anneArundel = mdCounty(name: "Anne Arundel", countyRecordation: 7.0, countyTransfer: 0.01, countyDeduction: 0.0)
let baltimoreCity = mdCounty(name: "Baltimore City", countyRecordation: 10.0, countyTransfer: 0.015, countyDeduction: 22000)
let baltimoreCounty = mdCounty(name: "Baltimore County", countyRecordation: 5.0, countyTransfer: 0.015, countyDeduction: 22000)
let calvert = mdCounty(name: "Calvert", countyRecordation: 10.0, countyTransfer: 0.0, countyDeduction: 0.0)
let caroline = mdCounty(name: "Caroline", countyRecordation: 10.0, countyTransfer: 0.005, countyDeduction: 25000)
let cecil = mdCounty(name: "Cecil", countyRecordation: 8.20, countyTransfer: 0.0, countyDeduction: 0.0)
let charles = mdCounty(name: "Charles", countyRecordation: 10.0, countyTransfer: 0.01, countyDeduction: 0.0)
let dorchester = mdCounty(name: "Dorchester", countyRecordation: 10.0, countyTransfer: 0.0075, countyDeduction: 0.0)

Controller:

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {


var userPrice = String?()
var userLoan = String?()
var userCredit = String?()
var split = TRUE
let titleData = TitleData()
var selectedCounty = String?()


@IBOutlet weak var closingCostLabel: UILabel!
@IBOutlet weak var salesPriceField: UITextField!
@IBOutlet weak var loanAmountField: UITextField!
@IBOutlet weak var sellerCreditField: UITextField!
@IBOutlet weak var costsLabel: UILabel!
@IBOutlet weak var countyPicker: UIPickerView!


override func viewDidLoad() {
    super.viewDidLoad()
    countyPicker.delegate = self
    countyPicker.dataSource = self
    view.backgroundColor = UIColor(red: 0xfd/255, green: 0xe8/255, blue: 0xd7/255, alpha: 1.0)
}


func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return mdCounty(name).count
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent componefnt: Int) {
    selectedCounty = mdCounty.name[row]
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    return "\(titleData.mdCounty.name[row])"
}

Upvotes: 3

Views: 720

Answers (1)

zisoft
zisoft

Reputation: 23078

What you really want to use here is an Array of mdCounty objects:

struct mdCounty {
    var name: String = ""
    var countyRecordation: Double = 0
    var countyTransfer: Double = 0
    var countyDeduction: Double = 0
}

let mdCounties: [mdCounty] = [
    mdCounty(name: "Garrett", countyRecordation: 7.0, countyTransfer: 0.01, countyDeduction: 50000),
    mdCounty(name: "Allegany", countyRecordation: 6.50, countyTransfer: 0.005, countyDeduction: 30000),
    mdCounty(name: "Anne Arundel", countyRecordation: 7.0, countyTransfer: 0.01, countyDeduction: 0.0),
    ...
]


func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return mdCounties.count
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    selectedCounty = mdCounties[row]
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    return mdCounties[row].name
}

Upvotes: 4

Related Questions