Skip to content

Instantly share code, notes, and snippets.

@hamzamuric
Created October 12, 2022 15:31
Show Gist options
  • Save hamzamuric/736dba6ce8340debc8dfda392c45613e to your computer and use it in GitHub Desktop.
Save hamzamuric/736dba6ce8340debc8dfda392c45613e to your computer and use it in GitHub Desktop.
pma
import Foundation
let ime: String = "Vahid"
let br: Int = 4
let brojevi: [Int] = [1, 2, 3]
let ocene: [String: Int] = ["Vahid": 8, "Amar": 9]
let broj = brojevi[1]
print(broj)
let ocena = ocene["Ermin", default: 5]
let ocena2 = ocene["Dzenis"] ?? 10
print(ocena2)
let koordinate = (1, 4)
//let x = koordinate.0
//let y = koordinate.1
let (x, y) = koordinate
print(x)
for i in 1...5 {
print(i)
}
for i in 1..<5 {
print(i)
}
func hello(_ name: String) {
print("Hello \(name)")
}
hello("Valje")
func greeting(from: String, to: String) {
print("\(from) said greetings to \(to)")
}
greeting(from: "Halje", to: "Valje")
func distance(from f: (Double, Double), to t: (Double, Double)) -> Double {
let (x1, y1) = f
let (x2, y2) = t
return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2))
}
let p1 = (4.0, 3.2)
let p2 = (2.2, 1.1)
let dist = distance(from: p1, to: p2)
print(dist)
//let plus1 = { (x: Int) -> Int in
// x + 1
//}
let plus1: (Int) -> Int = { $0 + 1 }
let dupli = brojevi
.filter { $0 > 1 }
.map { $0 * 2 }
print(dupli)
func counter() -> () -> Int {
var n = 0
return {
n += 1
return n
}
}
let cnt = counter()
print(cnt())
print(cnt())
print(cnt())
print(cnt())
class Point {
var x: Double
var y: Double
init(x: Double, y: Double) {
self.x = x
self.y = y
print("Point created \((x, y))")
}
deinit {
print("Point destroyed \((x, y))")
}
func mod() -> Double {
return 4.3
}
func translate(dx: Double, dy: Double) {
self.x += dx
self.y += dy
}
}
var points: [Point] = []
for i in 1...3 {
let p = Point(x: Double(i), y: Double(i))
points.append(p)
print("Using point \((p.x, p.y))")
}
print(points)
extension String {
func trimmed() -> String {
return self.trimmingCharacters(in: .whitespaces)
}
mutating func trim() {
self = self.trimmed()
}
}
var text = " bla bla "
print(text.trimmed())
text.trim()
print(text)
protocol Bla {
func bla()
}
let gradovi = ["Pazar": 12000, "Beograd": 1200000]
if let stanovnici = gradovi["Tutin"] {
print(stanovnici)
}
func foo(br: Int) {
guard br > 0 else {
return
}
print(br)
}
foo(br: 3)
foo(br: -3)
enum Direction {
case up, down, left, right
}
var d = Direction.up
d = .left
enum Currency : String {
case eur = "EUR"
case usd = "USD"
}
var c = Currency.eur
c = .usd
print(c.rawValue)
enum MyError : Error {
case networkError, storageError
}
func getData() throws -> String {
throw MyError.networkError
}
do {
let data = try getData()
} catch MyError.networkError {
} catch MyError.storageError {
} catch {
}
let data = try? getData()
let data2 = try! getData()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment