← 返回
内容创作 中文

swift-programming

Learn and write Swift code for variables, functions, classes, structs, enums, optionals, closures, protocols, async/await, and SwiftUI.
学习并编写Swift代码,涵盖变量、函数、类、结构体、枚举、可选值、闭包、协议、async/await及SwiftUI。
saitamawtf
内容创作 clawhub v1.0.0 1 版本 99900.6 Key: 无需
★ 0
Stars
📥 1,005
下载
💾 41
安装
1
版本
#latest

概述

Swift Programming Skill

Learn and program in Swift - Apple's programming language for iOS, macOS, watchOS, and tvOS.

Quick Reference

Variables & Constants

var nombre = "Juan"          // Variable
let edad = 25                 // Constante
var mensaje: String = "Hola"  // Con tipo explícito

Tipos de Datos

String       // Texto
Int           // Entero
Double        // Decimal
Bool          // true/false
Array         // Lista
Dictionary    // clave-valor
Optional      // puede ser nil

Funciones

func saludar(nombre: String) -> String {
    return "Hola, \(nombre)!"
}

// Parametro con valor por defecto
func greet(_ name: String = "Mundo") -> String {
    return "Hola, \(name)"
}

Clases

class Persona {
    var nombre: String
    var edad: Int
    
    init(nombre: String, edad: Int) {
        self.nombre = nombre
        self.edad = edad
    }
    
    func presentar() -> String {
        return "Soy \(nombre) y tengo \(edad) años"
    }
}

Estructuras

struct Punto {
    var x: Double
    var y: Double
    
    func distancia() -> Double {
        return sqrt(x*x + y*y)
    }
}

Enums

enum Direction {
    case north, south, east, west
}

enum Status {
    case success(String)
    case failure(Error)
}

Optionals

var nombre: String? = nil  // Puede ser nil

// unwrap seguro
if let nombre = nombre {
    print(nombre)
}

// nil coalescing
let nombreDefecto = nombre ?? "Anonimo"

// optional chaining
let longitud = nombre?.count ?? 0

Closures

let suma = { (a: Int, b: Int) -> Int in
    return a + b
}

// Syntaxis reducida
let multiplicar: (Int, Int) -> Int = { $0 * $1 }

Protocolos

protocol Naming {
    var name: String { get }
    func greet() -> String
}

struct User: Naming {
    var name: String
    
    func greet() -> String {
        return "Hola, \(name)!"
    }
}

Async/Await

func fetchData() async throws -> Data {
    let url = URL(string: "https://api.example.com")!
    let (data, _) = try await URLSession.shared.data(from: url)
    return data
}

// Llamar
Task {
    do {
        let data = try await fetchData()
    } catch {
        print("Error: \(error)")
    }
}

SwiftUI Basics

import SwiftUI

struct ContentView: View {
    @State private var count = 0
    
    var body: some View {
        VStack {
            Text("Contador: \(count)")
            Button("Incrementar") {
                count += 1
            }
        }
    }
}

Recursos

  • Apple Swift Docs: https://docs.swift.org/swift-book/
  • Hacking with Swift: https://www.hackingwithswift.com

版本历史

共 1 个版本

  • v1.0.0 当前
    2026-03-29 06:04 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

content-creation

Baidu Wenku AIPPT

ide-rea
使用百度文库 AI 智能生成 PPT,自动根据内容选择模板。
★ 66 📥 46,237
content-creation

AdMapix

fly0pants
广告情报与应用数据分析助手,支持搜索广告素材、分析应用排名、下载量、收入及市场洞察,用于广告素材和竞品分析。
★ 295 📥 136,537

pine-editor

saitamawtf
使用TradingView Pine Script v6内置函数和可视化工具创建、自定义和回测交易指标与策略。
★ 0 📥 164