C 与 Swift 对照
同一个程序分别用 C 和 Swift 编写,取自本课程的课时。
绑定与可变性
C 中的 const 用于限定对象或通过指针访问的类型;它通常不会冻结所有可访问的数据。const 指针和指向 const 的指针描述的是不同的限制。Swift 的 let 固定一个绑定,而 var 允许值改变。绑定到 let 的值类型实例不能被修改。
C1#include <stdio.h>2#include <stdbool.h>3#include <string.h>4 5int main(void) {6const int saved = 18;7 int current = saved;8 current = 21;9 printf("%d %d\n", saved, current);10 return 0;11}Swift1import Foundation2let saved = 183var current = saved4current = 215print("\(saved) \(current)")字符串与插值
C 的 printf 会解析格式字符串,并要求参数类型与格式相匹配;%% 会写入一个字面百分号。Swift 插值会使用反斜杠和括号,将带类型的表达式插入 String。无论文本如何构造,最终文本都可以单独返回或打印。
C1#include <stdio.h>2#include <stdbool.h>3#include <string.h>4 5int main(void) {6int percent = 40;7 printf("Downloaded %d%%\n", percent);8 return 0;9}Swift1import Foundation2let percent = 403print("Downloaded \(percent)%")Switch 语句与模式
C 的 switch 作用于整数表达式,包括 char 和 enum 值。除非控制流离开对应的 case,否则执行会继续贯穿后续 case。Swift 的 switch 可以匹配字符串和更丰富的模式,并且不会隐式贯穿后续 case。它必须涵盖所有可能情况,或者提供合适的 default。
C1#include <stdio.h>2#include <stdbool.h>3#include <string.h>4 5int main(void) {6char command = '?';7 switch (command) {8 case 'h':9 case '?': puts("show help"); break;10 case 'q': puts("close"); break;11 default: puts("unknown"); break;12 }13 return 0;14}Swift1import Foundation2let command = "?"3switch command {4case "help", "?": print("show help")5case "quit": print("close")6default: print("unknown")7}函数与标签
C 和 Swift 都支持自由函数,不过 C 的声明和定义通过翻译单元和头文件进行组织。Swift 中的同级源文件会编译到同一个模块中,这些练习将辅助函数放在 Functions.swift 中,由 main.swift 调用。返回类型和参数类型仍然是明确的约定。
C1#include <stdio.h>2static void line(char symbol, int count) { for (int i = 0; i < count; ++i) putchar(symbol); putchar('\n'); }3int main(void) { line('*', 3); return 0; }Swift1import Foundation2func line(_ symbol: String, count: Int = 3) -> String {3 String(repeating: symbol, count: count)4}字典与缺失的键
标准 C 没有内置的字典容器;这个简短的对比使用由条目组成的数组,以及一个带有显式回退值的查找函数。Swift 的 Dictionary 提供键查找,并返回一个 Optional。当 API 需要区分这两种情况时,有效的零值必须与缺少的键区分开来。
C1#include <stdio.h>2#include <string.h>3typedef struct { const char *key; int value; } Entry;4static int lookup(const Entry *entries, int count, const char *key, int fallback) {5 for (int i = 0; i < count; ++i) if (strcmp(entries[i].key, key) == 0) return entries[i].value;6 return fallback;7}8int main(void) {9 Entry settings[] = {{"zoom", 2}};10 settings[0].value = 3;11 printf("%d %d\n", lookup(settings, 1, "zoom", 1), lookup(settings, 1, "speed", 1));12 return 0;13}Swift1import Foundation2var settings = ["zoom": 2]3settings["zoom"] = 34print("\(settings["zoom"] ?? 1) \(settings["speed"] ?? 1)")结构体与独立副本
C 结构体赋值会复制成员值,这与将指针赋给同一个结构体不同。Swift 结构体也具有值语义,并且还支持直接在类型上定义方法。会更改存储属性的 Swift 实例方法需要声明为 mutating,并且需要可变的接收者。
C1#ifndef THEME_H2#define THEME_H3typedef struct { const char *name; } Theme;4static void theme_select(Theme *theme, const char *name) { theme->name = name; }5#endifSwift1import Foundation2struct Theme {3 var name: String4 mutating func select(_ value: String) { name = value }5}
课程大纲
10 章48 节课1 个项目48 个挑战99 道测验题常见问题
面向 C 开发者的 Swift 适合谁学习?
适合已经会写 C、想学 Swift 的人。每节课都从你熟悉的 C 代码出发,展示 Swift 如何实现同样的事情,所以你可以跳过入门基础。
需要先会 Swift 吗?
不需要。课程从你的第一个 Swift 程序开始。你需要掌握 C 基础:变量、条件、循环、函数和集合。
面向 C 开发者的 Swift 有多少节课?
面向 C 开发者的 Swift 包含 48 节互动课,你可以按自己的节奏完成。
面向 C 开发者的 Swift 包含哪些内容?
本课程分为以下几章:Swift 入门, 文本与决策, 控制流, 函数与数组, and more。
面向 C 开发者的 Swift 免费吗?
是的。Coddy 上的每门课程都可以免费开始。你可以直接在浏览器中完成课程,无需下载,也无需注册。