├── examples
├── demo1.swift
├── demo16.swift
├── demo8.swift
├── demo10.swift
├── demo13.swift
├── demo2.swift
├── demo11.swift
├── demo3.swift
├── demo9.swift
├── demo14.swift
├── demo4.swift
├── demo18.swift
├── demo12.swift
├── demo15.swift
├── demo6.swift
├── demo17.swift
├── demo7.swift
├── demo5.swift
├── demo20.swift
└── demo19.swift
├── .gitattributes
├── img
└── collection_array_dictionary_comparison.png
├── .github
├── FUNDING.yml
└── workflows
│ └── ci.yml
├── renovate.json
├── package.json
├── .gitignore
├── idoc.yml
├── swift.svg
└── README.md
/examples/demo1.swift:
--------------------------------------------------------------------------------
1 | print("Hello world!")
2 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | *.swift linguist-language=swift
2 | *.md linguist-detectable=true
3 |
--------------------------------------------------------------------------------
/examples/demo16.swift:
--------------------------------------------------------------------------------
1 | import Cocoa
2 | var A = 42
3 | A = "This is hello"
4 | print(A)
--------------------------------------------------------------------------------
/examples/demo8.swift:
--------------------------------------------------------------------------------
1 | var a = 86
2 | var b = a != 86 ? "歪果仁" : "中国人"
3 | print( b )
4 |
--------------------------------------------------------------------------------
/examples/demo10.swift:
--------------------------------------------------------------------------------
1 | var a = true
2 | var b = false
3 | print(!a)
4 | print(a && b)
5 | print(a || b)
--------------------------------------------------------------------------------
/examples/demo13.swift:
--------------------------------------------------------------------------------
1 | var version :Int32 = 3
2 | var version2 :Int64 = 30
3 |
4 | print(version)
5 | print(version2)
--------------------------------------------------------------------------------
/examples/demo2.swift:
--------------------------------------------------------------------------------
1 | let year = 1987
2 | var month = 11
3 | var day = 3
4 |
5 | print(year,month,day,separator:"-")
--------------------------------------------------------------------------------
/examples/demo11.swift:
--------------------------------------------------------------------------------
1 | let constNumber: Int = 1987
2 | var name : String = "我是调调!"
3 |
4 | print(constNumber)
5 | print(name)
--------------------------------------------------------------------------------
/examples/demo3.swift:
--------------------------------------------------------------------------------
1 | let constNumber = 1987
2 | var variableNumber = 13
3 |
4 | print(constNumber)
5 | print(variableNumber)
--------------------------------------------------------------------------------
/examples/demo9.swift:
--------------------------------------------------------------------------------
1 | print("将输出 1 到 5")
2 | for i in 1...5 {
3 | print(i)
4 | }
5 | print("只输出 1 到 4")
6 | for i in 1..<5 {
7 | print(i)
8 | }
--------------------------------------------------------------------------------
/img/collection_array_dictionary_comparison.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jaywcjlove/swift-tutorial/HEAD/img/collection_array_dictionary_comparison.png
--------------------------------------------------------------------------------
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | ko_fi: jaywcjlove
2 | buy_me_a_coffee: jaywcjlove
3 | custom: ["https://www.paypal.me/kennyiseeyou", "https://jaywcjlove.github.io/#/sponsor"]
4 |
--------------------------------------------------------------------------------
/examples/demo14.swift:
--------------------------------------------------------------------------------
1 |
2 | var stringArray: [String] = ["This", "is", "github"]
3 |
4 | // 并通过下面的方式可以访问数组和字典中的值
5 | print(stringArray[0]) // 访问第0个值, 数组的元素是从0开始记的
6 |
--------------------------------------------------------------------------------
/examples/demo4.swift:
--------------------------------------------------------------------------------
1 | /**
2 | * 这是一个错误的演示
3 | */
4 |
5 | let constNumber = 1987
6 | var variableNumber = 13
7 | constNumber = variableNumber + 1
8 | print(constNumber)
--------------------------------------------------------------------------------
/examples/demo18.swift:
--------------------------------------------------------------------------------
1 | let a: Int! = 5;
2 | // 表示可以承载一个 nil
3 | // 正常时不可以承载 nil
4 |
5 | let b = a // b 变成了显示可选性 `Int?`
6 | let e: Int = a
7 | let c: Int = a
8 | let d = a + 0
--------------------------------------------------------------------------------
/examples/demo12.swift:
--------------------------------------------------------------------------------
1 | var version = 3
2 | let tutorial = "Swift \(version) tutorial"
3 | let message = "Hello \(tutorial)!"
4 | var name = "Hello !" + "sdsd"
5 | print(tutorial)
6 | print(message)
--------------------------------------------------------------------------------
/renovate.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": [
3 | "config:base"
4 | ],
5 | "packageRules": [
6 | {
7 | "matchPackagePatterns": ["*"],
8 | "rangeStrategy": "replace"
9 | }
10 | ]
11 | }
12 |
--------------------------------------------------------------------------------
/examples/demo15.swift:
--------------------------------------------------------------------------------
1 | let awesome = ("Swift", 3, "awesome.")
2 | let (a, b, c) = awesome
3 | print(a) // 输出 Swift
4 | print(b) // 输出 3
5 | print(c) // 输出 awesome.
6 |
7 |
8 | // 和数组、字典不同的是,你不能通过[] 的方式来访问元组中的内容,但你可以像这样
9 | print(awesome.0)
--------------------------------------------------------------------------------
/examples/demo6.swift:
--------------------------------------------------------------------------------
1 | print(1 + 2 + 3) // 输出 6
2 | print(15 - 3) // 输出 12
3 | print(23 * 3) // 输出 69
4 | print(100.0 / 2.5) // 输出 40.0
5 | print(90 % 4) // 输出 2
6 | print("Hello " + "Swift!") // 输出 Hello Swift!
--------------------------------------------------------------------------------
/examples/demo17.swift:
--------------------------------------------------------------------------------
1 | // a 会被推测为 Int 类型
2 | let a = 42
3 |
4 | // pi 会被推测为 Double 类型
5 | let pi = 3.14159
6 |
7 | // anotherPi 会被推测为 Double 类型
8 | // 原始值3没有显式声明类型,而表达式中出现了一个浮点字面量,
9 | // 所以表达式会被推断为Double类型。
10 | let anotherPi = 3 + 0.14159
11 |
12 | print(anotherPi)
13 |
--------------------------------------------------------------------------------
/examples/demo7.swift:
--------------------------------------------------------------------------------
1 | var example = 13
2 |
3 | print("输出结果:",example)
4 | example += 1
5 | print("输出结果:",example)
6 | example -= 1
7 | print("输出结果:",example)
8 | example *= 2
9 | print("输出结果:",example)
10 | example /= 1
11 | print("输出结果:",example)
12 | example %= 1
13 | print("输出结果:",example)
--------------------------------------------------------------------------------
/examples/demo5.swift:
--------------------------------------------------------------------------------
1 | let constNumber = 1987 // 声明常量 constNumber 值为 1987
2 | var variableNumber = 13 // 声明变量 variableNumber 值为 13
3 | variableNumber = constNumber - 1 // 这里赋值,常量 constNumber 减 1 赋值给变量 variableNumber
4 | print(variableNumber) // 将 variableNumber 最终的值输出
--------------------------------------------------------------------------------
/examples/demo20.swift:
--------------------------------------------------------------------------------
1 | var optionalInteger: Int?
2 | optionalInteger = 404
3 |
4 | if optionalInteger != nil { // 确定可选类型包含值后可以在变量名后加!强制解析
5 | print(optionalInteger!) // 404
6 | }
7 |
8 |
9 | var errorCode: Int? = 404 // 在类型后面添加一个?
10 | print(errorCode) // Optional(404)
11 | errorCode = nil // 对非可选类型赋值为nil会报错
12 | print(errorCode) // nil
13 |
14 | // 确定可选类型包含值后可以在变量名后加!强制解析
15 | if errorCode != nil {
16 | print(errorCode!)// 404
17 | }
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "https://json.schemastore.org/package.json",
3 | "private": true,
4 | "name": "swift-tutorial",
5 | "title": "Swift 入门教程、读书笔记",
6 | "description": "通过简单的例子,来快速入门 Swift 语言基础编程、语法等各种语言特性,主要面向新手级别的学习者。",
7 | "version": "1.0.0",
8 | "scripts": {
9 | "start": "idoc --watch",
10 | "build": "idoc"
11 | },
12 | "repository": {
13 | "type": "git",
14 | "url": "https://github.com/jaywcjlove/swift-tutorial"
15 | },
16 | "keywords": [
17 | "swift",
18 | "tutorial",
19 | "swift-tutorial"
20 | ],
21 | "dependencies": {
22 | "idoc": "^1.29.0"
23 | }
24 | }
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Xcode
2 | #
3 | build/
4 | .build/
5 | *.pbxuser
6 | !default.pbxuser
7 | *.mode1v3
8 | !default.mode1v3
9 | *.mode2v3
10 | !default.mode2v3
11 | *.perspectivev3
12 | !default.perspectivev3
13 | xcuserdata
14 | *.xccheckout
15 | *.moved-aside
16 | DerivedData
17 | *.hmap
18 | *.ipa
19 | *.xcuserstate
20 | *.xcscmblueprint
21 | .DS_Store
22 |
23 |
24 | dist
25 | node_modules
26 | test/out
27 |
28 | npm-debug.log*
29 | lerna-debug.log
30 | yarn-error.log
31 | package-lock.json
32 |
33 | .DS_Store
34 | .cache
35 | .vscode
36 | .idea
37 |
38 | *.bak
39 | *.tem
40 | *.temp
41 | #.swp
42 | *.*~
43 | ~*.*
44 |
45 | # IDEA
46 | *.iml
47 | *.ipr
48 | *.iws
49 | .idea/
--------------------------------------------------------------------------------
/examples/demo19.swift:
--------------------------------------------------------------------------------
1 | var ch: Character = "a"
2 | var emptyString = "" // 空字符串字面量
3 | var anotherEmptyString = String() // 初始化 String 实例
4 | // 两个字符串均为空并等价。
5 | // 通过检查其Boolean类型的isEmpty属性来判断该字符串是否为空
6 | if emptyString.isEmpty {
7 | print("什么都没有")
8 | }
9 | // 打印输出:"什么都没有"
10 | let unusualMenagerie = "Koala 🐨, Snail 🐌, Penguin 🐧, Dromedary 🐪"
11 | print("unusualMenagerie has \(unusualMenagerie.characters.count) characters")
12 | // 打印输出:"unusualMenagerie has 40 characters"
13 |
14 | // +相加在一起(或称“串联”)
15 | var str1 = "hello";
16 | var str2 = " there";
17 | var str = str1 + str2;
18 | print(str);
19 | // welcome 现在等于 "hello there"
20 |
21 | let normal = "Could you help me, please?"
22 | // 转化大写
23 | let shouty = normal.uppercased()
24 | // shouty 值为 "COULD YOU HELP ME, PLEASE?"
25 | // 转化小写
26 | let whispered = normal.lowercased()
27 | // whispered 值为 "could you help me, please?"
--------------------------------------------------------------------------------
/idoc.yml:
--------------------------------------------------------------------------------
1 | site: Swift 入门教程、读书笔记
2 | favicon: ./swift.svg
3 | logo: ./swift.svg
4 |
5 | cacheFileStat: true
6 |
7 | homepage: https://wangchujiang.com/swift-tutorial/
8 |
9 | menus:
10 | 首页: index.html
11 | 捐赠:
12 | url: https://wangchujiang.com/#/sponsor
13 | target: __blank
14 | 应用:
15 | url: https://wangchujiang.com/#/app
16 | target: __blank
17 |
18 | editButton:
19 | label: Edit this page on GitHub
20 | url: https://github.com/jaywcjlove/swift-tutorial/tree/master/
21 |
22 | footer: |
23 | App •
24 | Projects •
25 | Sponsor •
26 | More Apps
27 | Released under the MIT License. Copyright © 2024 Kenny Wong
28 | Generated by idoc v{{idocVersion}}
--------------------------------------------------------------------------------
/swift.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/.github/workflows/ci.yml:
--------------------------------------------------------------------------------
1 | name: CI
2 | on:
3 | push:
4 | branches:
5 | - master
6 |
7 | jobs:
8 | build:
9 | runs-on: ubuntu-latest
10 | steps:
11 | - uses: actions/checkout@v4
12 | - uses: actions/setup-node@v4
13 | with:
14 | node-version: 20
15 | registry-url: 'https://registry.npmjs.org'
16 |
17 | - run: npm install
18 | - run: npm run build
19 |
20 | - name: Generate Contributors Images
21 | uses: jaywcjlove/github-action-contributors@main
22 | with:
23 | filter-author: (renovate\[bot\]|renovate-bot|dependabot\[bot\])
24 | output: dist/CONTRIBUTORS.svg
25 | avatarSize: 42
26 |
27 | - name: Create Tag
28 | id: create_tag
29 | uses: jaywcjlove/create-tag-action@main
30 | with:
31 | package-path: ./package.json
32 |
33 | - name: Deploy Website
34 | uses: peaceiris/actions-gh-pages@v4
35 | with:
36 | user_name: 'github-actions[bot]'
37 | user_email: 'github-actions[bot]@users.noreply.github.com'
38 | commit_message: ${{ github.event.head_commit.message }}
39 | github_token: ${{ secrets.GITHUB_TOKEN }}
40 | publish_dir: ./dist
41 |
42 | - name: Generate Changelog
43 | id: changelog
44 | uses: jaywcjlove/changelog-generator@main
45 | with:
46 | token: ${{ secrets.GITHUB_TOKEN }}
47 | filter-author: (jaywcjlove|小弟调调™|dependabot\[bot\]|Renovate Bot)
48 | filter: (^[\s]+?[R|r]elease)|(^[R|r]elease)
49 |
50 | - name: Create Release
51 | uses: ncipollo/release-action@v1
52 | if: steps.create_tag.outputs.successful
53 | with:
54 | allowUpdates: true
55 | token: ${{ secrets.GITHUB_TOKEN }}
56 | name: ${{ steps.create_tag.outputs.version }}
57 | tag: ${{ steps.create_tag.outputs.version }}
58 | body: |
59 | [](https://jaywcjlove.github.io/#/sponsor)
60 |
61 | Documentation ${{ steps.changelog.outputs.tag }}: https://raw.githack.com/jaywcjlove/vim-web/${{ steps.changelog.outputs.gh-pages-short-hash }}/index.html
62 | Comparing Changes: ${{ steps.changelog.outputs.compareurl }}
63 |
64 | ${{ steps.changelog.outputs.changelog }}
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | Swift入门教程、读书笔记
3 | ===
4 |
5 |
6 | [](https://jaywcjlove.github.io/#/sponsor)
7 | [](https://github.com/jaywcjlove/swift-tutorial/actions/workflows/ci.yml)
8 |
9 | 如果你已经掌握了一些 Swift 语法基础了,可以通过 [SwiftUI Example](https://github.com/jaywcjlove/swiftui-example) 来练习一些示例进行技术的提升。
10 |
11 | ✦ 欢迎下载我的 [macOS/iOS](https://wangchujiang.com/#app) 应用程序来支持我,谢谢 ✦
12 |
13 |
46 |
47 |
48 |