ios语言基础

Swift

1. 变量和常量
  • 变量 :使用 var 关键字定义,可以修改其值。
  • 常量 :使用 let 关键字定义,值一旦设定就不能修改。
swift 复制代码
var variableName = 42
variableName = 50

let constantName = 42
// constantName = 50 // 这是错误的,因为常量不能修改
2. 数据类型

Swift 是类型安全的语言,每个变量和常量都有一个确定的类型。常见数据类型包括:

  • Int:整数
  • FloatDouble:浮点数
  • Bool:布尔值
  • String:字符串
  • Array:数组
  • Dictionary:字典
swift 复制代码
let anInteger: Int = 10
let aFloat: Float = 4.5
let aDouble: Double = 3.14159
let aBool: Bool = true
let aString: String = "Hello, Swift"
let anArray: [Int] = [1, 2, 3]
let aDictionary: [String: Int] = ["one": 1, "two": 2]
3. 操作符

Swift 提供了常见的算术操作符、比较操作符、逻辑操作符和赋值操作符。

swift 复制代码
let sum = 2 + 3
let difference = 5 - 2
let product = 2 * 3
let quotient = 6 / 2

let isEqual = (2 == 2) // true
let isNotEqual = (2 != 3) // true
let isGreater = (3 > 2) // true

let isTrue = true && false // false
let isFalse = true || false // true
4. 条件语句

Swift 使用 ifelse 来构造条件语句。

swift 复制代码
let temperature = 30

if temperature > 25 {
    print("It's hot outside.")
} else {
    print("It's not that hot outside.")
}
5. 循环语句

Swift 支持 for 循环和 while 循环。

swift 复制代码
let numbers = [1, 2, 3, 4, 5]

for number in numbers {
    print(number)
}

var counter = 0
while counter < 5 {
    print(counter)
    counter += 1
}
6. 函数

使用 func 关键字定义函数,支持参数和返回值。

swift 复制代码
func greet(name: String) -> String {
    return "Hello, \(name)!"
}

let greeting = greet(name: "Alice")
print(greeting) // 输出 "Hello, Alice!"
7. 类和对象

Swift 是面向对象编程语言,使用 class 关键字定义类。

swift 复制代码
class Person {
    var name: String
    var age: Int

    init(name: String, age: Int) {
        self.name = name
        self.age = age
    }

    func introduce() -> String {
        return "Hi, I'm \(name) and I'm \(age) years old."
    }
}

let person = Person(name: "Bob", age: 25)
print(person.introduce()) // 输出 "Hi, I'm Bob and I'm 25 years old."
8. 结构体

结构体使用 struct 关键字定义,类似于类,但它是值类型。

swift 复制代码
struct Point {
    var x: Int
    var y: Int

    func description() -> String {
        return "Point at (\(x), \(y))"
    }
}

let point = Point(x: 10, y: 20)
print(point.description()) // 输出 "Point at (10, 20)"
9. 枚举

使用 enum 关键字定义枚举。

swift 复制代码
enum CompassPoint {
    case north
    case south
    case east
    case west
}

let direction = CompassPoint.north

switch direction {
case .north:
    print("Heading north")
case .south:
    print("Heading south")
case .east:
    print("Heading east")
case .west:
    print("Heading west")
}

iOS 开发

1. Xcode

Xcode 是苹果公司提供的集成开发环境(IDE),用于开发 iOS 和 macOS 应用。它包括代码编辑器、调试器和 Interface Builder 等工具。

2. UIKit

UIKit 是 iOS 应用的基础框架,提供了构建和管理应用用户界面的基本工具和 API。以下是一些核心概念:

  • UIView:所有视图的基类,代表屏幕上的矩形区域。
  • UIViewController:控制视图的控制器,管理视图的生命周期和用户交互。
  • UITableView:显示可滚动的单列列表的视图。
  • UICollectionView:显示多列或自定义布局的可滚动列表的视图。
3. Hello World 示例

以下是一个简单的 iOS 应用示例,展示如何在屏幕上显示 "Hello World"。

  1. 创建一个新的 Xcode 项目

    • 打开 Xcode,选择 "Create a new Xcode project"。
    • 选择 "App" 模板,点击 "Next"。
    • 输入项目名称和组织标识符,选择语言为 Swift,点击 "Next"。
    • 选择保存位置,点击 "Create"。
  2. 修改 ViewController

    • 打开 ViewController.swift 文件。
    • viewDidLoad 方法中添加以下代码:
    swift 复制代码
    import UIKit
    
    class ViewController: UIViewController {
        override func viewDidLoad() {
            super.viewDidLoad()
            
            // 创建一个标签
            let label = UILabel()
            label.text = "Hello World"
            label.textAlignment = .center
            label.frame = view.bounds
            
            // 将标签添加到视图中
            view.addSubview(label)
        }
    }
  3. 运行应用

    • 选择一个模拟器或连接的设备。
    • 点击 Xcode 顶部的运行按钮,运行应用。
相关推荐
消失的旧时光-194310 分钟前
Android回退按钮处理方法总结
android·开发语言·kotlin
千里马-horse31 分钟前
Async++ 源码分析7--parallel_reduce.h
开发语言·c++·async++·parallel_reduce
量化交易曾小健(金融号)42 分钟前
Python美股量化交易填坑记录——3.盈透(Interactive Brokers)证券API接口
开发语言·python
2501_916013741 小时前
iOS 上架 App 全流程实战,应用打包、ipa 上传、App Store 审核与工具组合最佳实践
android·ios·小程序·https·uni-app·iphone·webview
2501_915106321 小时前
iOS 26 能耗监测全景,Adaptive Power、新电池视图
android·macos·ios·小程序·uni-app·cocoa·iphone
Madison-No71 小时前
【C++】探秘string的底层实现
开发语言·c++
lly2024062 小时前
AJAX JSON 实例
开发语言
QiZhang | UESTC2 小时前
JAVA算法练习题day27
java·开发语言·c++·算法·leetcode·hot100
坚持就完事了2 小时前
2-C语言中的数据类型
c语言·开发语言
ss2733 小时前
手写MyBatis第96弹:异常断点精准捕获MyBatis深层BUG
java·开发语言·bug·mybatis