Swift 开发者学习 Dart
本指南旨在利用您的 Swift 编程知识来学习 Dart。它展示了两种语言的主要相似点和不同点,并介绍了 Swift 中不存在的 Dart 概念。作为 Swift 开发者,Dart 可能会让您感到熟悉,因为这两种语言共享许多概念。
Swift 和 Dart 都支持健全空安全。这两种语言默认都不允许变量为空。
与 Swift 类似,Dart 对集合、泛型、并发(使用 async/await)和扩展都有类似的支持。
混入(Mixins)是 Dart 中另一个对 Swift 开发者来说可能比较新的概念。与 Swift 类似,Dart 提供 AOT(提前编译)编译。然而,Dart 也支持 JIT(即时编译)模式,以辅助各种开发方面,例如增量重新编译或调试。欲了解更多信息,请查看 Dart 概览。
约定与 Lint
#Swift 和 Dart 都具有 Lint 工具来强制执行标准约定。然而,Swift 有一个独立的工具 SwiftLint
,而 Dart 有官方的布局约定并包含一个 Linter,使合规性变得轻松。要为您的项目自定义 Lint 规则,请遵循自定义静态分析的说明。(请注意,Dart 和 Flutter 的 IDE 插件也提供此功能。)
Dart 还提供了一个代码格式化工具,在命令行或通过 IDE 运行 dart format
时,它可以自动格式化任何 Dart 项目。
有关 Dart 约定和 Lint 的更多信息,请查看高效 Dart 和Linter 规则。
变量
#与 Swift 相比,Dart 中变量的声明和初始化方式略有不同。变量声明总是以变量类型、var
关键字或 final
关键字开头。与 Swift 一样,Dart 支持类型推断,编译器会根据赋给变量的值推断类型
// String-typed variable.
String name = 'Bob';
// Immutable String-typed variable.
final String name = 'Bob';
// This is the same as `String name = 'Bob';`
// since Dart infers the type to be String.
var name = 'Bob';
// And this is the same as `final String name = 'Bob';`.
final name = 'Bob';
每个 Dart 语句都以分号结尾,表示语句的结束。您可以在 Dart 中用显式类型替换 var
。然而,根据约定,当分析器可以隐式推断类型时,建议使用 var
。
// Declare a variable first:
String name;
// Initialize the variable later:
name = 'bob';
// Declare and initialize a variable at once with inference:
var name = 'bob';
上述 Dart 代码的 Swift 等效写法如下
// Declare a variable first:
var name: String
// Initialize the variable later
name = "bob"
// Declare and initialize a variable at once with inference:
var name = "bob"
在 Dart 中,当一个没有显式类型的变量在其声明后被初始化时,其类型会被推断为包罗万象的 dynamic
类型。同样,当无法自动推断类型时,它会默认为 dynamic
类型,这会移除所有类型安全。因此,Dart Linter 会通过生成警告来阻止这种做法。如果您打算允许变量拥有任何类型,则首选将其赋值给 Object?
而不是 dynamic
。
有关更多信息,请查看 Dart 语言之旅中的变量部分。
Final
#Dart 中的 final
关键字表示变量只能设置一次。这与 Swift 中的 let
关键字类似。
在 Dart 和 Swift 中,您只能初始化 final
变量一次,无论是在声明语句中还是在初始化列表中。任何第二次赋值的尝试都会导致编译时错误。以下两个代码片段都是有效的,但随后设置 name
会导致编译错误。
final String name;
if (b1) {
name = 'John';
} else {
name = 'Jane';
}
let name: String
if (b1) {
name = "John"
} else {
name = "Jane"
}
Const
#除了 final
,Dart 还有 const
关键字。const
的一个好处是它在编译时完全求值,并且在应用程序的生命周期内不能被修改。
const bar = 1000000; // Unit of pressure (dynes/cm2)
const double atm = 1.01325 * bar; // Standard atmosphere
在类级别定义的 const
变量需要标记为 static const
。
class StandardAtmosphere {
static const bar = 1000000; // Unit of pressure (dynes/cm2)
static const double atm = 1.01325 * bar; // Standard atmosphere
}
const
关键字不仅仅用于声明常量变量;它还可以用于创建常量值
var foo = const ['one', 'two', 'three'];
foo.add('four'); // Error: foo contains a constant value.
foo = ['apple', 'pear']; // This is allowed as foo itself isn't constant.
foo.add('orange'); // Allowed as foo no longer contains a constant value.
在上面的例子中,您不能更改 const
值(添加、更新或删除给定列表中的元素),但您可以为 foo
分配一个新值。在 foo
被分配了一个新的(非常量)列表后,您可以添加、更新或删除列表的内容。
您还可以将常量值赋给 final
字段。您不能在常量上下文中使用 final
字段,但可以使用常量。例如
final foo1 = const [1, 2, 3];
const foo2 = [1, 2, 3]; // Equivalent to `const [1, 2, 3]`
const bar2 = foo2; // OK
const bar1 = foo1; // Compile-time error, `foo1` isn't constant
您还可以定义 const
构造函数,使这些类不可变(不变),并使创建这些类的实例作为编译时常量成为可能。有关更多信息,请查看const 构造函数。
内置类型
#Dart 在平台库中包含多种类型,例如
- 基本值类型,例如
- 数字 (
num
,int
,double
) - 字符串 (
String
) - 布尔值 (
bool
) - 空值 (
Null
)
- 数字 (
- 集合
- 列表/数组 (
List
) - 集合 (
Set
) - 映射/字典 (
Map
)
- 列表/数组 (
有关更多信息,请查看 Dart 语言之旅中的内置类型。
数字
#Dart 定义了三种用于存储数字的数值类型
num
- 一种通用的 64 位数字类型。
int
- 一个平台相关的整数。在原生代码中,它是一个 64 位补码整数。在 Web 端,它是一个非小数的 64 位浮点数。
double
- 一个 64 位浮点数。
与 Swift 不同,Dart 没有无符号整数的特定类型。
所有这些类型也是 Dart API 中的类。int
和 double
类型都共享 num
作为它们的父类
由于数字值在技术上是类的实例,它们方便地公开了自己的实用函数。因此,例如,一个 int
可以如下转换为 double
int intVariable = 3;
double doubleVariable = intVariable.toDouble();
在 Swift 中,使用专门的初始化器可以实现相同的效果
var intVariable: Int = 3
var doubleVariable: Double = Double(intVariable)
对于字面值,Dart 会自动将整数字面量转换为 double
值。以下代码完全没有问题
double doubleValue = 3;
与 Swift 不同,在 Dart 中您可以使用相等 (==
) 运算符将整数值与双精度浮点数进行比较,如下所示
int intVariable = 3;
double doubleVariable = 3.0;
print(intVariable == doubleVariable); // true
此代码打印 true
。然而,在 Dart 中,Web 和原生平台之间的底层数字实现是不同的。Dart 中的数字页面详细介绍了这些差异,并展示了如何编写代码以使这些差异不影响结果。
字符串
#与 Swift 一样,Dart 使用 String
类型表示一系列字符,尽管 Dart 不支持表示单个字符的 Character
类型。String
可以用单引号或双引号定义,但是,推荐使用单引号。
String c = 'a'; // There isn't a specialized "Character" type
String s1 = 'This is a String';
String s2 = "This is also a String";
let c: Character = "a"
let s1: String = "This is a String"
let s2: String = "This is also a String"
转义特殊字符
#Dart 中转义特殊字符与 Swift(以及大多数其他语言)类似。要包含特殊字符,请使用反斜杠字符对其进行转义。
以下代码显示了一些示例
final singleQuotes = 'I\'m learning Dart'; // I'm learning Dart
final doubleQuotes = "Escaping the \" character"; // Escaping the " character
final unicode = '\u{1F60E}'; // 😎, Unicode scalar U+1F60E
请注意,也可以直接使用 4 位十六进制值(例如,\u2665
),但是,花括号也适用。有关处理 Unicode 字符的更多信息,请查看 Dart 语言之旅中的Runes 和字形簇。
字符串连接和多行声明
#在 Dart 和 Swift 中,您都可以转义多行字符串中的换行符,这使您的源代码更易于阅读,但仍然将 String
输出为单行。Dart 有几种定义多行字符串的方法
使用隐式字符串连接:任何相邻的字符串字面量都会自动连接,即使它们跨越多行
dartfinal s1 = 'String ' 'concatenation' " even works over line breaks.";
使用多行字符串字面量:当字符串两边使用三个引号(单引号或双引号)时,该字面量允许跨越多行
dartfinal s2 = '''You can create multiline strings like this one.'''; final s3 = """This is also a multiline string.""";
Dart 还支持使用
+
运算符连接字符串。这适用于字符串字面量和字符串变量dartfinal name = 'John'; final greeting = 'Hello ' + name + '!';
字符串插值
#使用 ${
语法将表达式插入字符串字面量中。Dart 在此基础上进行了扩展,允许在表达式为单个标识符时省略花括号
var food = 'bread';
var str = 'I eat $food'; // I eat bread
var str = 'I eat ${bakery.bestSeller}'; // I eat bread
在 Swift 中,您可以通过将变量或表达式用括号括起来并加上反斜杠前缀来达到同样的效果
let s = "string interpolation"
let c = "Swift has \(s), which is very handy."
原始字符串
#与 Swift 一样,您可以在 Dart 中定义原始字符串。原始字符串会忽略转义字符,并包含字符串中存在的任何特殊字符。在 Dart 中,您可以通过在字符串字面量前加上字母 r
来实现这一点,如下例所示。
// Include the \n characters.
final s1 = r'Includes the \n characters.';
// Also includes the \n characters.
final s2 = r"Also includes the \n characters.";
final s3 = r'''
The \n characters are also included
when using raw multiline strings.
''';
final s4 = r"""
The \n characters are also included
when using raw multiline strings.
""";
let s1 = #"Includes the \n characters."#
let s2 = #"""
The \n characters are also included
when using raw multiline strings.
"""#
相等性
#与 Swift 一样,Dart 的相等性运算符 (==
) 比较两个字符串是否相等。如果两个字符串包含相同的代码单元序列,则它们相等。
final s1 = 'String '
'concatenation'
" works even over line breaks.";
assert(s1 ==
'String concatenation works even over '
'line breaks.');
常用 API
#Dart 为字符串提供了几种常用 API。例如,Dart 和 Swift 都允许您使用 isEmpty
检查字符串是否为空。还有其他便捷方法,例如 toUpperCase
和 toLowerCase
。有关更多信息,请查看 Dart 语言之旅中的字符串。
布尔值
#布尔值在 Dart (bool
) 和 Swift (Bool
) 中都表示一个二进制值。
空安全
#Dart 强制执行健全空安全。默认情况下,类型不允许空值,除非标记为可空。Dart 通过在类型末尾加上问号 (?
) 来表示这一点。这类似于 Swift 的可选类型。
空感知运算符
#Dart 支持多种处理可空性的运算符。空合并运算符 (??
) 和可选链运算符 (?.
) 在 Dart 中可用,并且与 Swift 中的操作方式相同
a = a ?? b;
let str: String? = nil
let count = str?.count ?? 0
此外,Dart 还提供了空安全的级联运算符 (?..
)。当目标表达式解析为 null
时,此运算符会忽略任何操作。Dart 还提供了空赋值运算符 (??=
),这是 Swift 没有的。如果具有可空类型的变量的当前值为 null
,此运算符会为该变量赋值。表示为 a ??= b;
,它是以下内容的简写
a = a ?? b;
// Assign b to a if a is null; otherwise, a stays the same
a ??= b;
a = a ?? b
! 运算符(也称为“强制解包”)
#在安全地假设可空变量或表达式实际上是非空的情况下,可以告诉编译器抑制任何编译时错误。这是通过使用后缀 !
运算符实现的,将其作为表达式的后缀放置。(不要将其与 Dart 的“非”运算符混淆,后者使用相同的符号)
int? a = 5;
int b = a; // Not allowed.
int b = a!; // Allowed.
在运行时,如果 a
结果为空,则会发生运行时错误。
与 ?.
运算符类似,当访问对象的属性或方法时,使用 !
运算符
myObject!.someProperty;
myObject!.someMethod();
如果在运行时 myObject
为 null
,则会发生运行时错误。
Late 字段
#late
关键字可以分配给类字段,以指示它们在稍后初始化,同时保持不可空。这类似于 Swift 的“隐式解包可选类型”。这对于在变量初始化之前从未被观察到的情况很有用,允许其稍后初始化。一个不可空的 late
字段不能在稍后被赋值为 null。此外,当一个不可空的 late
字段在初始化之前被观察到时,会抛出运行时错误,这是您在良好行为的应用程序中希望避免的情况。
// Using null safety:
class Coffee {
late String _temperature;
void heat() { _temperature = 'hot'; }
void chill() { _temperature = 'iced'; }
String serve() => _temperature + ' coffee';
}
在这种情况下,_temperature
仅在调用 heat()
或 chill()
之后才初始化。如果 serve()
在其他方法之前被调用,则会发生运行时异常。请注意,_temperature
永远不能为 null
。
您还可以将 late
关键字与初始化器结合使用,以实现延迟初始化
class Weather {
late int _temperature = _readThermometer();
}
在这种情况下,_readThermometer()
仅在首次访问该字段时运行,而不是在初始化时运行。
Dart 的另一个优点是使用 late
关键字来延迟 final
变量的初始化。虽然在将 final
变量标记为 late
时不必立即初始化它,但它仍然只能初始化一次。第二次赋值会导致运行时错误。
late final int a;
a = 1;
a = 2; // Throws a runtime exception because
// "a" is already initialized.
函数
#Swift 使用 main.swift
文件作为应用程序的入口点。Dart 使用 main
函数作为应用程序的入口点。每个程序都必须有一个 main
函数才能执行。例如
void main() {
// main function is the entry point
print("hello world");
}
// main.swift file is the entry point
print("hello world")
Dart 不支持 Tuples
(尽管 pub.dev 上有几个 tuple 包可用)。如果一个函数需要返回多个值,您可以将它们包装在一个集合中,例如列表、集合或映射,或者您可以编写一个包装类,返回一个包含这些值的实例。有关此内容的更多信息,请参阅集合和类部分。
异常与错误处理
#与 Swift 一样,Dart 的函数和方法支持处理异常和错误。Dart 错误通常表示程序员的错误或系统故障,如栈溢出。Dart 错误不应该被捕获。另一方面,Dart 异常表示可恢复的故障,旨在被捕获。例如,在运行时代码可能尝试访问流式数据源,但反而收到一个异常,如果未捕获,将导致应用程序终止。您可以通过将函数调用包装在 try-catch
块中来管理 Dart 中的异常。
try {
// Create audio player object
audioPlayer = AVAudioPlayer(soundUrl);
// Play the sound
audioPlayer.play();
}
catch {
// Couldn't create audio player object, log the exception
print("Couldn't create the audio player for file $soundFilename");
}
类似地,Swift 使用 do-try-catch
块。例如
do {
// Create audio player object
audioPlayer = try AVAudioPlayer(contentsOf: soundURL)
// Play the sound
audioPlayer?.play()
}
catch {
// Couldn't create audio player object, log the error
print("Couldn't create the audio player for file \(soundFilename)")
}
您可以在同步和异步 Dart 代码中使用 try-catch
块。有关更多信息,请参阅 Error
和 Exception
类的文档。
参数
#与 Swift 类似,Dart 支持在其函数中使用命名参数。然而,与 Swift 不同的是,这些在 Dart 中并非默认。Dart 中默认的参数类型是位置参数。
int multiply(int a, int b) {
return a * b;
}
Swift 中,等效的做法是在参数前加上下划线,以消除参数标签的需要。
func multiply(_ a: Int, _ b: Int) -> Int {
return a * b
}
在 Dart 中创建命名参数时,将它们定义在单独的花括号块中,位于位置参数之后
int multiply(int a, int b, {int c = 1, int d = 1}) {
return a * b * c * d;
}
// Calling a function with both required and named parameters
multiply(3, 5); // 15
multiply(3, 5, c: 2); // 30
multiply(3, 5, d: 3); // 45
multiply(3, 5, c: 2, d: 3); // 90
// The Swift equivalent
func multiply(_ a: Int, _ b: Int, c: Int = 1, d: Int = 1) -> Int {
return a * b * c * d
}
命名参数必须包含以下之一
- 默认值
- 在类型末尾添加
?
将类型设置为可空 - 变量类型前的关键字
required
要了解更多关于可空类型的信息,请查看空安全。
在 Dart 中将命名参数标记为必需,必须在其前面加上 required
关键字
int multiply(int a, int b, { required int c }) {
return a * b * c;
}
// When calling the function, c has to be provided
multiply(3, 5, c: 2);
第三种参数类型是可选位置参数。顾名思义,它们与默认的位置参数类似,但可以在调用函数时省略。它们必须列在任何必需的位置参数之后,并且不能与命名参数结合使用。
int multiply(int a, int b, [int c = 1, int d = 1]) {
return a * b * c * d;
}
// Calling a function with both required and optional positioned parameters.
multiply(3, 5); // 15
multiply(3, 5, 2); // 30
multiply(3, 5, 2, 3); // 90
// The Swift equivalent
func multiply(_ a: Int, _ b: Int, _ c: Int = 1, _ d: Int = 1) -> Int {
return a * b * c * d
}
与命名参数一样,可选位置参数必须具有默认值或可空类型。
一等函数
#与 Swift 一样,Dart 函数也是一等公民,这意味着它们被视为任何其他对象。例如,以下代码展示了如何从一个函数返回一个函数
typedef int MultiplierFunction(int value);
// Define a function that returns another function
MultiplierFunction multiplyBy(int multiplier) {
return (int value) {
return value * multiplier;
};
}
// Call function that returns new function
MultiplierFunction multiplyByTwo = multiplyBy(2);
// Call the new function
print(multiplyByTwo(3)); // 6
// The Swift equivalent of the Dart function below
// Define a function that returns a closure
typealias MultiplierFunction = (Int) -> (Int)
func multiplyBy(_ multiplier: Int) -> MultiplierFunction {
return { $0 * multiplier} // Returns a closure
}
// Call function that returns a function
let multiplyByTwo = multiplyBy(2)
// Call the new function
print(multiplyByTwo(3)) // 6
匿名函数
#Dart 中的匿名函数除了语法上的差异外,与 Swift 中的闭包几乎完全相同。与命名函数一样,您可以像传递任何其他值一样传递匿名函数。例如,您可以将匿名函数存储在变量中,将其作为参数传递给另一个函数,或者从另一个函数返回它们。
Dart 有两种声明匿名函数的方式。第一种,使用花括号,像任何其他函数一样工作。它允许您使用多行,并且需要一个 return 语句来返回任何值。
// Multi line anonymous function
[1,2,3].map((element) {
return element * 2;
}).toList(); // [2, 4, 6]
// Swift equivalent anonymous function
[1, 2, 3].map { $0 * 2 }
另一种方法是使用箭头函数,其名称来源于其语法中使用的箭头状符号。当您的函数体只包含单个表达式并返回该值时,可以使用这种简写语法。这省略了任何大括号或 return 语句的需要,因为这些都是隐含的。
// Single-line anonymous function
[1,2,3].map((element) => element * 2).toList(); // [2, 4, 6]
箭头语法或花括号的选择适用于任何函数,而不仅仅是匿名函数。
multiply(int a, int b) => a * b;
multiply(int a, int b) {
return a * b;
}
生成器函数
#Dart 支持生成器函数,这些函数返回一个惰性构建的可迭代项集合。使用 yield
关键字将项添加到最终的可迭代对象中,或者使用 yield*
添加整个项集合。
以下示例展示了如何编写一个基本的生成器函数
Iterable<int> listNumbers(int n) sync* {
int k = 0;
while (k < n) yield k++;
}
// Returns an `Iterable<int>` that iterates
// through 0, 1, 2, 3, and 4.
print(listNumbers(5));
Iterable<int> doubleNumbersTo(int n) sync* {
int k = 0;
while (k < n) {
yield* [k, k];
k++;
}
}
print(doubleNumbersTo(3)); // Returns an iterable with [0, 0], [1, 1], and [2, 2].
这是一个同步生成器函数的示例。您还可以定义异步生成器函数,它们返回流而不是可迭代对象。在并发部分了解更多信息。
语句
#本节涵盖 Dart 和 Swift 之间语句的异同。
控制流 (if/else, for, while, switch)
#Dart 中所有控制流语句的工作方式与 Swift 中的对应语句类似,只是语法上有一些差异。
if
#与 Swift 不同,Dart 中的 if
语句要求条件周围有括号。尽管 Dart 风格指南建议在流程控制语句周围使用花括号(如下所示),但是当 if
语句没有 else 子句且整个 if
语句适合在一行内时,如果您愿意,可以省略花括号。
var a = 1;
// Parentheses for conditions are required in Dart.
if (a == 1) {
print('a == 1');
} else if (a == 2) {
print('a == 2');
} else {
print('a != 1 && a != 2');
}
// Curly braces are optional for single line `if` statements.
if (a == 1) print('a == 1');
let a = 1;
if a == 1 {
print("a == 1")
} else if a == 2 {
print("a == 2")
} else {
print("a != 1 && a != 2")
}
for(-in)
#在 Swift 中,for
循环仅用于遍历集合。要多次循环代码块,Swift 允许您遍历一个范围。Dart 不支持定义范围的语法,但除了遍历集合的 for-in
之外,还包含一个标准 for 循环。
Dart 的 for-in
循环与 Swift 中的对应循环类似,它可以遍历任何 Iterable
值,如下面的 List
示例所示
var list = [0, 1, 2, 3, 4];
for (var i in list) {
print(i);
}
let array = [0, 1, 2, 3, 4]
for i in array {
print(i)
}
Dart 的 for-in
循环没有像 Swift 字典那样允许您遍历映射的特殊语法。要达到类似的效果,您可以将映射的条目提取为 Iterable
类型。或者,您可以使用 Map.forEach
Map<String, int> dict = {
'Foo': 1,
'Bar': 2
};
for (var e in dict.entries) {
print('${e.key}, ${e.value}');
}
dict.forEach((key, value) {
print('$key, $value');
});
var dict:[String:Int] = [
"Foo":1,
"Bar":2
]
for (key, value) in dict {
print("\(key),\(value)")
}
运算符
#与 Swift 不同,Dart 不允许添加新运算符,但它允许您使用 operator
关键字重载现有运算符。例如
class Vector {
final double x;
final double y;
final double z;
Vector operator +(Vector v) {
return Vector(x: x + v.x, y: y + v.y, z: z+v.z);
}
}
struct Vector {
let x: Double
let y: Double
let z: Double
}
func +(lhs: Vector, rhs: Vector) -> Vector {
return Vector(x: lhs.x + rhs.x, y: lhs.y + rhs.y, z: lhs.z + rhs.z)
}
...
算术运算符
#在大多数情况下,算术运算符在 Swift 和 Dart 中的行为相同,除了除法运算符 (/
) 有一个显著例外。在 Swift(和许多其他编程语言)中,let x = 5/2
的结果是 2
(一个整数)。在 Dart 中,int x = 5/2
的结果是 2.5
(一个浮点值)。要获得整数结果,请使用 Dart 的截断除法运算符 (~/
)。
虽然 ++
和 --
运算符存在于 Swift 的早期版本中,但它们已在 Swift 3.0 中被移除。Dart 中的对应运算符以相同的方式运行。例如
assert(2 + 3 == 5);
assert(2 - 3 == -1);
assert(2 * 3 == 6);
assert(5 / 2 == 2.5); // Result is a double
assert(5 ~/ 2 == 2); // Result is an int
assert(5 % 2 == 1); // Remainder
a = 0;
b = ++a; // Increment a before b gets its value.
assert(a == b); // 1 == 1
a = 0;
b = a++; // Increment a AFTER b gets its value.
assert(a != b); // 1 != 0
类型测试运算符
#这两种语言中测试运算符的实现方式略有不同。
含义 | Dart 运算符 | Swift 等效 |
---|---|---|
类型转换(见下文描述) | expr as T | expr as! T expr as? T |
如果对象具有指定类型,则为 true | expr is T | expr is T |
如果对象不具有指定类型,则为 true | expr is! T | !(expr is T) |
如果 obj
是 T
指定类型的子类型,则 obj is T
的结果为 true
。例如,obj is Object?
总是为 true。
仅当您确定对象是该类型时,才使用类型转换运算符将对象强制转换为特定类型。例如
(person as Employee).employeeNumber = 4204583;
Dart 只有一个单类型转换运算符,其行为类似于 Swift 的 as!
运算符。没有 Swift 的 as?
运算符的等效项。
(person as! Employee).employeeNumber = 4204583;
如果您不确定对象是否为 T
类型,请在使用对象之前使用 is T
进行检查。
在 Dart 中,类型提升会在 if
语句的作用域内更新局部变量的类型。这也适用于空检查。类型提升仅适用于局部变量,而不适用于实例变量。
if (person is Employee) {
person.employeeNumber = 4204583;
}
// Swift requires the variable to be cast.
if let person = person as? Employee {
print(person.employeeNumber)
}
逻辑运算符
#逻辑运算符(例如 AND (&&
)、OR (||
) 和 NOT (!
))在两种语言中都是相同的。例如
if (!done && (col == 0 || col == 3)) {
// ...Do something...
}
位运算符和移位运算符
#位运算符在两种语言中基本相同。
例如
final value = 0x22;
final bitmask = 0x0f;
assert((value & bitmask) == 0x02); // AND
assert((value & ~bitmask) == 0x20); // AND NOT
assert((value | bitmask) == 0x2f); // OR
assert((value ^ bitmask) == 0x2d); // XOR
assert((value << 4) == 0x220); // Shift left
assert((value >> 4) == 0x02); // Shift right
assert((-value >> 4) == -0x03); // Shift right // Result may differ on the web
条件运算符
#Dart 和 Swift 都包含一个条件运算符 (?:
),用于评估可能需要 if-else
语句的表达式
final displayLabel = canAfford ? 'Please pay below' : 'Insufficient funds';
let displayLabel = canAfford ? "Please pay below" : "Insufficient funds"
级联 (.. 运算符)
#与 Swift 不同,Dart 支持使用级联运算符进行级联操作。这允许您在单个对象上链式调用多个方法或属性赋值。
以下示例展示了如何在一个级联操作链中,对一个新构造的对象设置多个属性,然后调用多个方法
Animal animal = Animal()
..name = 'Bob'
..age = 5
..feed()
..walk();
print(animal.name); // "Bob"
print(animal.age); // 5
var animal = Animal()
animal.name = "Bob"
animal.age = 5
animal.feed()
animal.walk()
print(animal.name)
print(animal.age)
集合
#本节涵盖 Swift 中的一些集合类型及其与 Dart 中对应类型的比较。
列表
#Dart 中的 List
字面量与 Swift 中的数组定义方式相同,使用方括号并用逗号分隔。两种语言之间的语法非常相似,但在以下示例中显示了一些细微差异
final List<String> list1 = <String>['one', 'two', 'three']; // Initialize list and specify full type
final list2 = <String>['one', 'two', 'three']; // Initialize list using shorthand type
final list3 = ['one', 'two', 'three']; // Dart can also infer the type
var list1: Array<String> = ["one", "two", "three"] // Initialize array and specify the full type
var list2: [String] = ["one", "two", "three"] // Initialize array using shorthand type
var list3 = ["one", "two", "three"] // Swift can also infer the type
以下代码示例概述了您可以在 Dart List
上执行的基本操作。第一个示例展示了如何使用 index
运算符从列表中检索值
final fruits = ['apple', 'orange', 'pear'];
final fruit = fruits[1];
要将值添加到列表末尾,请使用 add
方法。要添加另一个 List
,请使用 addAll
方法
final fruits = ['apple', 'orange', 'pear'];
fruits.add('peach');
fruits.addAll(['kiwi', 'mango']);
有关完整的 List API,请参阅 List
类文档。
不可修改
#将数组赋值给常量(Swift 中的 let
)会使数组不可变,这意味着其大小和内容无法更改。您也不能将新数组赋值给常量。
在 Dart 中,这略有不同,根据您的需求,您有几种选择
- 如果列表是编译时常量且不应被修改,请使用
const
关键字
const fruits = ['apple', 'orange', 'pear'];
- 将列表分配给
final
字段。这意味着列表本身不必是编译时常量,并确保该字段不能被另一个列表覆盖。然而,它仍然允许修改列表的大小或内容
final fruits = ['apple', 'orange', 'pear'];
- 使用不可修改构造函数(如下例所示)创建
final List
。这会创建一个大小和内容都不能更改的List
,使其行为就像 Swift 中的常量Array
。
final fruits = List<String>.unmodifiable(['apple', 'orange', 'pear']);
let fruits = ["apple", "orange", "pear"]
展开运算符
#Dart 的另一个有用特性是展开运算符 (...
) 和空感知展开运算符 (...?
),它们提供了一种将多个值插入集合的简洁方式。
例如,您可以使用展开运算符 (...
) 将列表的所有值插入另一个列表,如下所示
final list = [1, 2, 3];
final list2 = [0, ...list]; // [ 0, 1, 2, 3 ]
assert(list2.length == 4);
尽管 Swift 没有展开运算符,但上面第二行的等效代码如下所示
let list2 = [0] + list
如果展开运算符右侧的表达式可能为 null
,您可以使用空感知展开运算符 (...?
) 来避免异常
List<int>? list;
final list2 = [0, ...?list]; //[ 0 ]
assert(list2.length == 1);
let list2 = [0] + list ?? []
集合
#Dart 和 Swift 都支持使用字面量定义 Set
。Set 的定义方式与列表相同,但使用花括号而不是方括号。Set 是无序集合,只包含唯一项。这些项的唯一性是通过哈希码实现的,这意味着对象需要哈希值才能存储在 Set
中。每个 Dart 对象都包含一个哈希码,而在 Swift 中,您需要显式应用 Hashable
协议才能将对象存储在 Set
中。
以下代码片段展示了 Dart 和 Swift 中初始化 Set
的区别
final abc = {'a', 'b', 'c'};
var abc: Set<String> = ["a", "b", "c"]
在 Dart 中,您不能通过指定空花括号 ({}
) 来创建空集合;这会导致创建一个空 Map
。要创建空 Set
,请在 {}
声明前加上类型参数或将 {}
赋值给 Set
类型的变量
final names = <String>{};
Set<String> alsoNames = {}; // This works, too.
// final names = {}; // Creates an empty map, not a set.
不可修改
#与 List
类似,Set
也有一个不可修改的版本。例如
final abc = Set<String>.unmodifiable(['a', 'b', 'c']);
let abc: Set<String> = ["a", "b", "c"]
映射
#Dart 中的 Map
类型可以与 Swift 中的 Dictionary
类型进行比较。两种类型都关联键和值。这些键和值可以是任何类型的对象。每个键只出现一次,但您可以多次使用相同的值。
在两种语言中,字典都基于哈希表,这意味着键需要是可哈希的。在 Dart 中,每个对象都包含一个哈希值,而在 Swift 中,您需要显式应用 Hashable
协议才能将对象存储在 Dictionary
中。
这里有一些使用字面量创建的简单 Map
和 Dictionary
示例
final gifts = {
'first': 'partridge',
'second': 'turtle doves',
'fifth': 'golden rings',
};
final nobleGases = {
2: 'helium',
10: 'neon',
18: 'argon',
};
let gifts = [
"first": "partridge",
"second": "turtle doves",
"fifth": "golden rings",
]
let nobleGases = [
2: "helium",
10: "neon",
18: "argon",
]
以下代码示例概述了您可以在 Dart Map
上执行的基本操作。第一个示例展示了如何使用 key
运算符从 Map
中检索值
final gifts = {'first': 'partridge'};
final gift = gifts['first']; // 'partridge'
使用 containsKey
方法检查 Map
中是否已存在某个键
final gifts = {'first': 'partridge'};
assert(gifts.containsKey('fifth')); // false
使用索引赋值运算符 ([]=
) 在 Map
中添加或更新条目。如果 Map
尚未包含该键,则添加该条目。如果该键存在,则更新该条目的值
final gifts = {'first': 'partridge'};
gifts['second'] = 'turtle'; // Gets added
gifts['second'] = 'turtle doves'; // Gets updated
要从 Map
中移除一个条目,请使用 remove
方法;要移除所有满足给定测试的条目,请使用 removeWhere
方法
final gifts = {'first': 'partridge'};
gifts.remove('first');
gifts.removeWhere((key, value) => value == 'partridge');
类
#Dart 没有定义接口类型——任何类都可以用作接口。如果您只想引入一个接口,可以创建一个没有具体成员的抽象类。要更详细地了解这些类别,请查看抽象类、隐式接口和扩展类部分的文档。
Dart 不支持值类型。如内置类型部分所述,Dart 中的所有类型都是引用类型(即使是原始类型),这意味着 Dart 不提供 struct
关键字。
枚举
#枚举类型,通常称为枚举或 enums,是一种特殊的类,用于表示固定数量的常量值。枚举长期以来一直是 Dart 语言的一部分,但 Dart 2.17 为成员添加了增强的枚举支持。这意味着您可以添加持有状态的字段、设置该状态的构造函数、具有功能的方法,甚至覆盖现有成员。有关更多信息,请查看 Dart 语言之旅中的声明增强枚举。
构造函数
#Dart 的类构造函数与 Swift 中的类初始化器类似。然而,在 Dart 中,它们提供了更多设置类属性的功能。
标准构造函数
#标准类构造函数在声明和调用方面都与 Swift 初始化器非常相似。Dart 使用完整的类名而不是 init
关键字。new
关键字曾经是创建新类实例所必需的,现在是可选的,并且不再推荐使用。
class Point {
double x = 0;
double y = 0;
Point(double x, double y) {
// There's a better way to do this in Dart, stay tuned.
this.x = x;
this.y = y;
}
}
// Create a new instance of the Point class
Point p = Point(3, 5);
构造函数参数
#由于在构造函数中编写代码来分配所有类字段通常相当冗余,Dart 提供了一些语法糖来简化此操作
class Point {
double x;
double y;
// Syntactic sugar for setting x and y
// before the constructor body runs.
Point(this.x, this.y);
}
// Create a new instance of the Point class
Point p = Point(3, 5);
与函数类似,构造函数也可以接受可选的位置参数或命名参数
class Point {
...
// With an optional positioned parameter
Point(this.x, [this.y = 0]);
// With named parameters
Point({required this.y, this.x = 0});
// With both positional and named parameters
Point(int x, int y, {int scale = 1}) {
...
}
...
}
初始化列表
#您也可以使用初始化列表,它在构造函数参数中使用 this
直接设置的任何字段之后运行,但在构造函数主体之前运行
class Point {
...
Point(Map<String, double> json)
: x = json['x']!,
y = json['y']! {
print('In Point.fromJson(): ($x, $y)');
}
...
}
初始化列表是使用断言的好地方。
命名构造函数
#与 Swift 不同,Dart 允许类通过命名拥有多个构造函数。您可以选择使用一个未命名构造函数,但任何额外的构造函数都必须命名。一个类也可以只有命名构造函数。
class Point {
double x;
double y;
Point(this.x, this.y);
// Named constructor
Point.fromJson(Map<String, double> json)
: x = json['x']!,
y = json['y']!;
}
Const 构造函数
#当您的类实例始终不可变时,您可以通过添加 const
构造函数来强制执行此操作。删除 const
构造函数对于使用您的类的人来说是一个重大更改,因此请谨慎使用此功能。将构造函数定义为 const
会使类不可修改:类中所有非静态字段都必须标记为 final
。
class ImmutablePoint {
final double x, y;
const ImmutablePoint(this.x, this.y);
}
这也意味着您可以将该类用作常量值,使对象成为编译时常量
const ImmutablePoint origin = ImmutablePoint(0, 0);
构造函数重定向
#您可以从其他构造函数调用构造函数,例如,为了防止代码重复或为参数添加额外的默认值
class Point {
double x, y;
// The main constructor for this class.
Point(this.x, this.y);
// Delegates to the main constructor.
Point.alongXAxis(double x) : this(x, 0);
}
工厂构造函数
#当您不需要创建新的类实例时,可以使用工厂构造函数。一个例子是如果可以返回缓存的实例
class Logger {
static final Map<String, Logger> _cache =
<String, Logger>{};
final String name;
// Factory constructor that returns a cached copy,
// or creates a new one if it's not yet available.
factory Logger(String name)=> _cache[name] ??= Logger._internal(name);
// Private constructor used only in this library
Logger._internal(this.name);
}
方法
#在 Dart 和 Swift 中,方法是为对象提供行为的函数。
void doSomething() { // This is a function
// Implementation..
}
class Example {
void doSomething() { // This is a method
// Implementation..
}
}
func doSomething() { // This is a function
// Implementation..
}
class Example {
func doSomething() { // This is a method
// Implementation..
}
}
Getter 和 Setter
#您可以通过在字段名前加上 get
或 set
关键字来定义 getter 和 setter。您可能还记得每个实例字段都有一个隐式 getter,如果适用,还有一个 setter。在 Swift 中,语法略有不同,因为 get
和 set
关键字需要定义在属性语句内部,并且只能作为语句定义,而不能作为表达式
class Rectangle {
double left, top, width, height;
Rectangle(this.left, this.top, this.width, this.height);
// Define two calculated properties: right and bottom.
double get right => left + width;
set right(double value) => width = value - left;
double get bottom => top + height;
set bottom(double value) => height = value - top;
}
class Rectangle {
var left, top, width, height: Double;
init(left: Double, top: Double, width: Double, height: Double) {
self.left = left
self.top = top
self.width = width
self.height = height
}
// Define two calculated properties: right and bottom.
var right: Double {
get {
return left + width
}
set { width = newValue - left }
}
var bottom: Double {
get {
return top + height
}
set { height = newValue - top }
}
}
抽象类
#Dart 具有抽象类的概念,这是 Swift 不支持的。抽象类不能直接实例化,只能被子类化。这使得抽象类在定义接口(类似于 Swift 中的协议)时很有用。
抽象类通常包含抽象方法,这些方法声明没有实现。非抽象子类被迫覆盖这些方法并提供适当的实现。抽象类还可以包含具有默认实现的方法。如果子类在扩展抽象类时没有覆盖这些方法,则会继承此实现。
要定义抽象类,请使用 abstract
修饰符。以下示例声明了一个抽象类,该类有一个抽象方法和一个包含默认实现的方法
// This class is declared abstract and thus can't be instantiated.
abstract class AbstractContainer {
void updateChildren(); // Abstract method.
// Method with default implementation.
String toString() => "AbstractContainer";
}
隐式接口
#在 Dart 语言中,每个类都隐式定义一个接口,包含该类及其实现的任何接口的所有实例成员。如果您想创建一个类 A
来支持类 B
的 API 而不继承 B
的实现,则类 A
应该实现 B
接口。
与 Dart 不同,Swift 类不隐式定义接口。接口需要显式定义为协议并由开发者实现。
一个类可以实现一个或多个接口,然后提供接口所需的 API。Dart 和 Swift 有不同的实现接口的方式。例如
abstract class Animal {
int getLegs();
void makeNoise();
}
class Dog implements Animal {
@override
int getLegs() => 4;
@override
void makeNoise() => print('Woof woof');
}
protocol Animal {
func getLegs() -> Int;
func makeNoise()
}
class Dog: Animal {
func getLegs() -> Int {
return 4;
}
func makeNoise() {
print("Woof woof");
}
}
扩展类
#Dart 中的类继承与 Swift 非常相似。在 Dart 中,您可以使用 extends
创建子类,使用 super
引用超类
abstract class Animal {
// Define constructors, fields, methods...
}
class Dog extends Animal {
// Define constructors, fields, methods...
}
class Animal {
// Define constructors, fields, methods...
}
class Dog: Animal {
// Define constructors, fields, methods...
}
混入
#混入(Mixins)允许您的代码在类之间共享功能。您可以在类中使用混入的字段和方法,就像它们是类的一部分一样。一个类可以使用多个混入——这在多个类共享相同功能时很有用——而无需相互继承或共享共同祖先。
虽然 Swift 不支持混入,但如果您编写一个协议并结合一个为协议中指定的方法提供默认实现的扩展,则可以近似实现此功能。这种方法的主要问题是,与 Dart 不同,这些协议扩展不维护它们自己的状态。
您可以像普通类一样声明混入,只要它不扩展 Object
以外的任何类且没有构造函数。使用 with
关键字将一个或多个逗号分隔的混入添加到类中。
以下示例展示了 Dart 中如何实现此行为,以及 Swift 中如何复制类似行为
abstract class Animal {}
// Defining the mixins
mixin Flyer {
fly() => print('Flaps wings');
}
mixin Walker {
walk() => print('Walks legs');
}
class Bat extends Animal with Flyer {}
class Goose extends Animal with Flyer, Walker {}
class Dog extends Animal with Walker {}
// Correct calls
Bat().fly();
Goose().fly();
Goose().walk();
Dog().walk();
// Incorrect calls
Bat().walk(); // Not using the Walker mixin
Dog().fly(); // Not using the Flyer mixin
class Animal {
}
// Defining the "mixins"
protocol Flyer {
func fly()
}
extension Flyer {
func fly() {
print("Flaps wings")
}
}
protocol Walker {
func walk()
}
extension Walker {
func walk() {
print("Walks legs")
}
}
class Bat: Animal, Flyer {}
class Goose: Animal, Flyer, Walker {}
class Dog: Animal, Walker {}
// Correct calls
Bat().fly();
Goose().fly();
Goose().walk();
Dog().walk();
// Incorrect calls
Bat().walk(); // `bat` doesn't have the `walk` method
Dog().fly(); // "dog" doesn't have the `fly` method
将 class
关键字替换为 mixin
可以防止混入被用作常规类。
mixin Walker {
walk() => print('Walks legs');
}
// Impossible, as Walker is no longer a class.
class Bat extends Walker {}
由于可以使用多个混入,当它们在同一个类上使用时,它们的方法或字段可能会相互重叠。它们甚至可能与使用它们的类或该类的超类重叠。为了解决这个问题,Dart 将它们堆叠在一起,因此它们添加到类中的顺序很重要。
举个例子
class Bird extends Animal with Consumer, Flyer {
当在 Bird
的实例上调用方法时,Dart 从堆栈底部开始,首先是其自身的类 Bird
,它优先于其他实现。如果 Bird
没有实现,那么 Dart 会继续向上移动堆栈,接下来是 Flyer
,然后是 Consumer
,直到找到一个实现。如果找不到任何实现,则最后检查父类 Animal
。
扩展方法
#与 Swift 类似,Dart 提供了扩展方法,允许您向现有类型添加功能——特别是方法、getter、setter 和运算符。Dart 和 Swift 中创建扩展的语法看起来非常相似
extension <name> on <type> {
(<member definition>)*
}
extension <type> {
(<member definition>)*
}
例如,以下 Dart SDK 中 String
类的扩展允许解析整数
extension NumberParsing on String {
int parseInt() {
return int.parse(this);
}
}
print('21'.parseInt() * 2); // 42
extension String {
func parseInt() -> Int {
return Int(self) ?? 0
}
}
print("21".parseInt() * 2) // 42
尽管扩展在 Dart 和 Swift 中相似,但仍有一些关键区别。以下部分涵盖了最重要的差异,但请查看扩展方法以获取完整概述。
命名扩展
#尽管不是强制性的,但您可以在 Dart 中命名扩展。命名扩展允许您控制其范围——这意味着在它与另一个库冲突时,可以隐藏或显示该扩展。如果名称以下划线开头,则该扩展仅在其定义的库中可用。
// Hide "MyExtension" when importing types from
// "path/to/file.dart".
import 'path/to/file.dart' hide MyExtension;
// Only show "MyExtension" when importing types
// from "path/to/file.dart".
import 'path/to/file.dart' show MyExtension;
// The `shout()` method is only available within this library.
extension _Private on String {
String shout() => this.toUpperCase();
}
初始化器
#在 Swift 中,您可以使用扩展为类型添加新的便利初始化器。在 Dart 中,您不能使用扩展为类添加额外的构造函数,但可以添加一个静态扩展方法来创建该类型的实例。考虑以下示例
class Person {
Person(this.fullName);
final String fullName;
}
extension ExtendedPerson on Person {
static Person create(String firstName, String lastName) {
return Person("$firstName $lastName");
}
}
// To use the factory method, use the name of
// the extension, not the type.
final person = ExtendedPerson.create('John', 'Doe');
重写成员
#重写实例方法(包括运算符、getter 和 setter)在两种语言中也非常相似。在 Dart 中,您可以使用 @override
注解来指示您有意重写一个成员
class Animal {
void makeNoise => print('Noise');
}
class Dog implements Animal {
@override
void makeNoise() => print('Woof woof');
}
在 Swift 中,您将 override
关键字添加到方法定义中
class Animal {
func makeNoise() {
print("Noise")
}
}
class Dog: Animal {
override func makeNoise() {
print("Woof woof");
}
}
泛型
#与 Swift 一样,Dart 支持使用泛型来提高类型安全或减少代码重复。
泛型方法
#您可以将泛型应用于方法。要定义泛型类型,请将其放置在方法名后的 < >
符号之间。然后,该类型可以在方法内部(作为返回类型)或在方法的参数中使用
// Defining a method that uses generics.
T transform<T>(T param) {
// For example, doing some transformation on `param`...
return param;
}
// Calling the method. Variable "str" will be
// of type String.
var str = transform('string value');
在这种情况下,将 String
传递给 transform
方法可确保它返回 String
。同样,如果提供 int
,则返回值是 int
。
通过逗号分隔来定义多个泛型
// Defining a method with multiple generics.
T transform<T, Q>(T param1, Q param2) {
// ...
}
// Calling the method with explicitly-defined types.
transform<int, String>(5, 'string value');
// Types are optional when they can be inferred.
transform(5, 'string value');
泛型类
#泛型也可以应用于类。您可以在调用构造函数时指定类型,这允许您根据特定类型定制可重用类。
在以下示例中,Cache
类用于缓存特定类型
class Cache<T> {
T getByKey(String key) {}
void setByKey(String key, T value) {}
}
// Creating a cache for strings.
// stringCache has type Cache<String>
var stringCache = Cache<String>();
// Valid, setting a string value.
stringCache.setByKey('Foo', 'Bar')
// Invalid, int type doesn't match generic.
stringCache.setByKey('Baz', 5)
如果省略类型声明,则运行时类型为 Cache<dynamic>
,并且对 setByKey
的两次调用都有效。
限制泛型
#您可以使用泛型通过 extends
将代码限制为一系列类型。这确保了您的类使用扩展特定类型的泛型类型进行实例化(并且与 Swift 类似)
class NumberManager<T extends num> {
// ...
}
// Valid
var manager = NumberManager<int>();
var manager = NumberManager<double>();
// Invalid, neither String nor its parent classes extend num.
var manager = NumberManager<String>();
字面量中的泛型
#Map-
、Set-
和 List-
字面量可以显式声明泛型类型,这在类型未推断或推断不正确时很有用。
例如,List
类有一个泛型定义:class List<E>
。泛型类型 E
指的是列表内容的类型。通常,此类型是自动推断的,并在 List
类的一些成员类型中使用。(例如,它的第一个 getter 返回 E
类型的值)。当定义 List
字面量时,您可以如下显式定义泛型类型
var objList = [5, 2.0]; // Type: List<num> // Automatic type inference
var objList = <Object>[5, 2.0]; // Type: List<Object> // Explicit type definition
var objSet = <Object>{5, 2.0}; // Sets work identically
Map
也是如此,它也使用泛型定义其 key
和 value
类型 (class Map<K, V>
)
// Automatic type inference
var map = {
'foo': 'bar'
}; // Type: Map<String, String>
// Explicit type definition:
var map = <String, Object>{
'foo': 'bar'
}; // Type: Map<String, Object>
并发
#Swift 支持多线程,Dart 支持 isolates,它们类似于轻量级线程,此处不作介绍。每个 isolate 都有自己的事件循环。有关更多信息,请参阅isolates 的工作原理。
Future
#原生的 Swift 没有 Dart 的 Future
的对应物。但是,如果您熟悉 Apple 的 Combine 框架或第三方库(如 RxSwift 或 PromiseKit),您可能仍然了解这个对象。
简而言之,Future 表示异步操作的结果,该结果将在稍后可用。如果您有一个函数返回 String
的 Future
(Future<String>
) 而不仅仅是 String
,那么您基本上是在接收一个可能在稍后——未来——存在的值。
当 Future 的异步操作完成时,值就变得可用。但是,您应该记住,Future 也可以在完成时返回一个错误而不是一个值。
一个例子是,如果您发出一个 HTTP 请求,并立即收到一个 Future 作为响应。一旦结果到来,Future 就以该值完成。但是,如果 HTTP 请求失败,例如因为互联网连接中断,则 Future 会以错误而不是值完成。
Future 也可以手动创建。创建 Future 最简单的方法是定义和调用 async
函数,这将在下一节中讨论。当您有一个需要成为 Future
的值时,您可以使用 Future
类轻松地将其转换为 Future
String str = 'String Value';
Future<String> strFuture = Future<String>.value(str);
Async/await
#虽然 Future 不属于原生的 Swift,但 Dart 中的 async/await
语法在 Swift 中有对应的概念,并且以类似的方式工作,尽管没有 Future
对象。
与 Swift 一样,函数可以标记为 async
。Dart 的不同之处在于,任何 async
函数总是隐式返回一个 Future
。例如,如果您的函数返回一个 String
,则此函数的异步对应函数返回一个 Future<String>
。
Swift 中放在 async
关键字之后的 throws
关键字(但仅当函数可抛出时)在 Dart 的语法中不存在,因为 Dart 异常和错误不会被编译器检查。相反,如果异步函数中发生异常,返回的 Future
会因该异常而失败,然后可以适当地处理该异常。
// Returns a future of a string, as the method is async
Future<String> fetchString() async {
// Typically some other async operations would be done here.
Response response = await makeNetworkRequest();
if (!response.success) {
throw BadNetwork();
}
return 'String Value';
}
然后可以按如下方式调用此异步函数
String stringFuture = await fetchString();
print(str); // "String Value"
Swift 中等效的异步函数
func fetchString() async throws -> String {
// Typically some other async operations would be done here.
let response = makeNetworkRequest()
if !response.success {
throw BadNetwork()
}
return "String Value"
}
同样,异步函数中发生的任何异常都可以通过使用 catchError
方法以处理失败的 Future
的相同方式进行处理。
在 Swift 中,异步函数不能从非异步上下文调用。在 Dart 中,您被允许这样做,但必须正确处理结果 Future
。不必要地从非异步上下文调用异步函数被认为是不好的做法。
与 Swift 一样,Dart 也有 await
关键字。在 Swift 中,await
仅在调用 async
函数时可用,但 Dart 的 await
适用于 Future
类。因此,await
也适用于 async
函数,因为 Dart 中的所有 async
函数都返回 Future。
等待一个 Future 会暂停当前函数的执行,并将控制权返回给事件循环,事件循环可以处理其他事情,直到 Future 以值或错误完成。在那之后的某个时候,await
表达式会评估为该值或抛出该错误。
当 Future 完成时,将返回其值。您只能在 async
上下文中 await
,就像在 Swift 中一样。
// We can only await futures within an async context.
asyncFunction() async {
String returnedString = await fetchString();
print(returnedString); // 'String Value'
}
当被等待的 Future 失败时,会在带有 await
关键字的行上抛出一个错误对象。您可以使用常规的 try-catch
块来处理它
// We can only await futures within an async context.
Future<void> asyncFunction() async {
String? returnedString;
try {
returnedString = await fetchString();
} catch (error) {
print('Future encountered an error before resolving.');
return;
}
print(returnedString);
}
有关更多信息和交互式练习,请查看异步编程教程。
Stream
#Dart 异步工具箱中的另一个工具是 Stream
类。虽然 Swift 有自己的 Stream 概念,但 Dart 中的 Stream 与 Swift 中的 AsyncSequence
类似。同样,如果您了解 Observables
(在 RxSwift 中)或 Publishers
(在 Apple 的 Combine 框架中),那么 Dart 的 Stream 应该会感到熟悉。
对于不熟悉 Stream
、AsyncSequence
、Publishers
或 Observables
的人来说,其概念如下:Stream
本质上就像一个 Future
,但它随时间传播多个值,就像一个事件总线。可以监听 Stream 以接收值或错误事件,并且当不再发送事件时可以关闭它们。
监听
#要监听一个流,您可以在 async
上下文中使用 for-in
循环结合流。for
循环会为每个发出的项调用回调方法,并在流完成或出错时结束
Future<int> sumStream(Stream<int> stream) async {
var sum = 0;
try {
await for (final value in stream) {
sum += value;
}
} catch (error) {
print('Stream encountered an error! $err');
}
return sum;
}
如果在监听流时发生错误,则会在包含 await
关键字的行上抛出错误,您可以使用 try-catch
语句进行处理
try {
await for (final value in stream) { ... }
} catch (err) {
print('Stream encountered an error! $err');
}
这不是监听流的唯一方法:您也可以调用其 listen
方法并提供回调,该回调在流发出值时被调用
Stream<int> stream = ...
stream.listen((int value) {
print('A value has been emitted: $value');
});
listen
方法有一些可选的回调函数,用于错误处理或流完成时调用
stream.listen(
(int value) { ... },
onError: (err) {
print('Stream encountered an error! $err');
},
onDone: () {
print('Stream completed!');
},
);
listen
方法返回一个 StreamSubscription
实例,您可以使用它来停止监听流
StreamSubscription subscription = stream.listen(...);
subscription.cancel();
创建流
#与 Future 一样,您有几种不同的方式来创建流。两种最常见的方式是使用异步生成器或 SteamController
。
异步生成器
#异步生成器函数的语法与同步生成器函数相同,但使用 async*
关键字而不是 sync*
,并且返回一个 Stream
而不是 Iterable
。这种方法类似于 Swift 中的 AsyncStream
结构。
在异步生成器函数中,yield
关键字将给定值发送到流。然而,yield*
关键字与流而不是其他可迭代对象一起工作。这允许将来自其他流的事件发送到此流。在以下示例中,函数只有在新生成的流完成之后才会继续执行
Stream<int> asynchronousNaturalsTo(int n) async* {
int k = 0;
while (k < n) yield k++;
}
Stream<int> stream = asynchronousNaturalsTo(5);
您还可以使用 StreamController
API 创建流。有关更多信息,请参阅使用 StreamController。
文档注释
#常规注释在 Dart 中的工作方式与 Swift 中相同。使用双反斜杠 (//
) 注释掉该行双反斜杠之后的所有内容,而 /* ... */
块注释则可以跨越多行。
除了常规注释,Dart 还有文档注释,它们与dart doc
协同工作:这是一个第一方工具,用于生成 Dart 包的 HTML 文档。将文档注释放置在所有公共成员声明之上被认为是最佳实践。您可能会注意到此过程与 Swift 中为各种文档生成工具添加注释的方式类似。
与 Swift 一样,您可以通过使用三个正斜杠而不是两个 (///
) 来定义文档注释
/// The number of characters in this chunk when unsplit.
int get length => ...
在文档注释中,用方括号将类型、参数和方法名括起来。
/// Returns the [int] multiplication result of [a] * [b].
multiply(int a, int b) => a * b;
虽然支持 JavaDoc 风格的文档注释,但您应该避免使用它们并使用 ///
语法。
/**
* The number of characters in this chunk when unsplit.
* (AVOID USING THIS SYNTAX, USE /// INSTEAD.)
*/
int get length => ...
库和可见性
#Dart 的可见性语义与 Swift 类似,Dart 库大致相当于 Swift 模块。
Dart 提供两种访问控制级别:公共和私有。方法和变量默认是公共的。私有变量以 _ 字符 (_
) 为前缀,并由 Dart 编译器强制执行。
final foo = 'this is a public property';
final _foo = 'this is a private property';
String bar() {
return 'this is a public method';
}
String _bar() {
return 'this is a private method';
}
// Public class
class Foo {
}
// Private class
class _Foo {
},
私有方法和变量在 Dart 中作用域限定在其库内,在 Swift 中作用域限定在其模块内。在 Dart 中,您可以在一个文件中定义一个库,而在 Swift 中,您必须为您的模块创建一个新的构建目标。这意味着在一个 Dart 项目中您可以定义 n
个库,但在 Swift 中您必须创建 n
个模块。
属于库的所有文件都可以访问该库中的所有私有对象。但出于安全原因,文件仍需要允许特定文件访问其私有对象,否则任何文件(即使是来自项目外部的文件)都可以注册到您的库并访问可能敏感的数据。换句话说,私有对象不会在库之间共享。
library animals;
part 'parrot.dart';
class _Animal {
final String _name;
_Animal(this._name);
}
part of animals;
class Parrot extends _Animal {
Parrot(String name) : super(name);
// Has access to _name of _Animal
String introduction() {
return 'Hello my name is $_name';
}
}
有关更多信息,请查看创建包。
下一步
#本指南向您介绍了 Dart 和 Swift 之间的主要区别。此时,您可能会考虑转向 Dart 或 Flutter(一个使用 Dart 从单个代码库构建精美、原生编译的多平台应用程序的开源框架)的通用文档,在那里您将找到有关该语言的深入信息和实用的入门方法。