Azimjon Abdurasulov

Swift Syntax

Cheat Sheet'lar

Swift Syntax Cheat Sheet

Ushbu ma'lumotnoma Swift tilining asosiy sintaksisini tezda eslab olish uchun mo'ljallangan.

1. O'zgaruvchilar va Konstantalar

let name = "Ali"      // Konstant (o'zgarmas)
var age = 25           // O'zgaruvchi
age = 26               // Mumkin
// name = "Vali"       // XATO!

2. Ma'lumot turlari

let pi: Double = 3.14
let isDev: Bool = true
let message: String = "Salom"
let count: Int = 10

3. Optionals

var nickname: String? = nil
nickname = "azimjon"

// Safe unwrapping
if let name = nickname { print(name) }
guard let name = nickname else { return }
let displayName = nickname ?? "Mehmon" // Nil-coalescing

4. Control Flow (Shartlar va Tsikllar)

// if-else
if age >= 18 { print("Kattalar") }

// switch
switch age {
case 0...12: print("Bola")
case 13...19: print("O'smir")
default: print("Katta")
}

// for-in
for i in 1...5 { print(i) }

// while
while count > 0 { count -= 1 }

5. Funksiyalar

func greet(name: String) -> String {
    return "Salom, \(name)!"
}
print(greet(name: "Ali"))

6. Closures

let multiply = { (a: Int, b: Int) -> Int in
    return a * b
}
let result = multiply(2, 3) // 6

7. Enums

enum Direction {
    case north, south, east, west
}
let move = Direction.north

8. Structs vs Classes

// Struct (Value type)
struct Point { var x, y: Int }

// Class (Reference type)
class Person {
    var name: String
    init(name: String) { self.name = name }
}

9. Protocols

protocol Describable {
    func describe() -> String
}

struct Car: Describable {
    func describe() -> String { "Bu mashina" }
}

10. Error Handling

enum MyError: Error { case failed }

func doWork() throws { throw MyError.failed }

do {
    try doWork()
} catch {
    print(error)
}

11. Collections

var fruits = ["Apple", "Banana"]      // Array
var scores = ["Ali": 10, "Vali": 8]   // Dictionary
var uniqueNumbers = Set([1, 2, 2, 3]) // Set (1, 2, 3)

12. Generics

func swapValues<T>(_ a: inout T, _ b: inout T) {
    let temp = a
    a = b
    b = temp
}

13. Access Control

  • open / public: Hamma joyda
  • internal: Modul ichida (default)
  • fileprivate: Fayl ichida
  • private: Scope (qavs) ichida

Markdown manba

iOS Lessons