Created
January 18, 2020 09:51
-
-
Save DanielAsher/c3225e455208249ecfed323f6d61b591 to your computer and use it in GitHub Desktop.
Saying hi in swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func sayHi() -> String { | |
| return "Hello Dubi" | |
| } |
Managed to solve the problem of the custom string convertible (in a very non efficient way I am sure).
So now the code is working and doing what it should. Still not sure how to implement the get and set not inside subscript.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
import Foundation
import Foundation
import Glibc
struct Array2D: CustomStringConvertible {
var numberRows: Int
var numberCols: Int
var matrix: [Int]
var description: String = ""
init(numberRows: Int, numberCols: Int) {
self.numberRows = numberRows
self.numberCols = numberCols
matrix = Array(repeating: 0, count: numberRows * numberCols)
for d in 1...numberRows {
description += "(matrix[(d - 1) * numberCols...(d * numberCols) - 1]) \n"
}
}
func index(row: Int, col: Int) -> Int {
return (numberCols * (row - 1) + (col - 1))
}
func indexIsValid(row: Int, col: Int) -> Bool {
if col > numberCols || row > numberRows {
return false
} else {
return true
}
}
/* set(row: Int, col: Int, newValue) {
{
if indexIsValid(row: row, col: col) {
matrix[(row * numberCols) + col] = newValue
} else {
print("index is out of bounds")
exit(0)
}
}
}
}
get(row: Int, col: Int) -> Int {
return data[(index(row: row, col: col))]
}
*/
subscript(row: Int, col: Int) -> Int {
get {
if indexIsValid(row: row, col: col) {
return matrix[index(row: row, col:col)]
} else {
print("inxex is out of bounds")
exit(0)
}
}
set(newValue) {
if indexIsValid(row: row, col: col) {
matrix[index(row: row, col: col)] = newValue
description = ""
for d in 1...numberRows {
description += "(matrix[(d - 1) * numberCols...(d * numberCols) - 1]) \n"
}
} else {
print("index is out of bounds")
exit(0)
}
}
}
}