ios - I would like to pass a 2d array of values to a second view in a Swift Single-View Application -
i trying create basic sudoku solver using xcode. currently, have 9x9 display of text fields gets converted 2d array , goes through solver i've made already. current dilemma want pass solved array second view , print sudoku board i'm not sure how 1.) correctly transition second view , 2.) how bring array print it.
i can't yet add images, storyboard contains 2 views. not connected currently. first has 9x9 display of text fields , button beneath no action associated. second has 9x9 display of labels , nothing else.
here entirety of code first view:
import uikit class viewcontroller: uiviewcontroller { @iboutlet var row1: [uitextfield]! @iboutlet var row2: [uitextfield]! @iboutlet var row3: [uitextfield]! @iboutlet var row4: [uitextfield]! @iboutlet var row5: [uitextfield]! @iboutlet var row6: [uitextfield]! @iboutlet var row7: [uitextfield]! @iboutlet var row8: [uitextfield]! @iboutlet var row9: [uitextfield]! @ibaction func executesolver() { var board = [[int]](count: 9, repeatedvalue: [int](count: 9, repeatedvalue: 0)) var board2 = [row1, row2, row3, row4, row5, row6, row7, row8, row9] var row = 0; row < 9; ++row{ var rowe = board2[row]; var col = 0; col < 9; ++col{ var val = rowe[col].text; if var intval = val.toint() { if intval > 0 && intval < 10 { board[row][col] = intval; } } } } var ss = solver(givenboard: board) board = ss.getboard() } override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } }
you can consider using custom 2d struct.
why use this? gives subscripting.
also, if labels share common characteristics, might wise use array of outlets , use tags identify each instead of creating of outlets 1 one.
struct array2d<t> { let columns: int let rows: int private var array: array<t?> init(columns: int, rows: int) { self.columns = columns self.rows = rows array = array<t?>(count: rows*columns, repeatedvalue: nil) } subscript(column: int, row: int) -> t? { { return array[row*columns + column] } set { array[row*columns + column] = newvalue } } }
Comments
Post a Comment