如果您想了解Swift - Swift 3 新特性汇总和不同于以往版本的新变化的知识,那么本篇文章将是您的不二之选。我们将深入剖析Swift - Swift 3 新特性汇总的各个方面,并为您解答不同于以往版本的新变化的疑在这篇文章中,我们将为您介绍Swift - Swift 3 新特性汇总的相关知识,同时也会详细的解释不同于以往版本的新变化的运用方法,并给出实际的案例分析,希望能帮助到您!
之前
Apple在
WWDC上已将
Swift 3整合进了
Xcode 8 beta中,而本月苹果发布了
Swift 3的正式版。这也是自
2015年底Apple开源Swift之后,首个发布的主要版本(
Swift 3.0),该版本实现了
Swift演变过程中所讨论并通过的90多个提议。这里我对
Swift 3的新特性、新变化进行一个总结。
一、彻底移除在 Swift 2.2 就已经弃用的特性
这些特性在我们使用
Xcode 7.3的时候就已经有告警提示,在
Swift 3中已将其彻底移出。
1,弃用 ++ 与 -- 操作符
过去我们可以使用
++与
--操作符来实现自增自减,现已废弃。
1
2
3
4
5
vari = 0
i++
++i
i--
--i
可以使用复合加法运算(+=)与减法运算(-=),或者使用普通的加法运算(+-)实现同样的功能。
5
6
7
8
//使用复合加法运算(+=)与减法运算(-=)
i = 0
i += 1
i -= 1
//使用普通的加法运算(+)与减法运算(-)
i = i + 1
i = i - 1
2,废除C语言风格的for循环 我们过去可能习惯下面风格的for循环,现在也已废弃。
3
for
i=1; i<100; i++ {
print("\(i)")
}
现在可以使用for-in循环,或者使用for-each加闭包的写法实现同样的功能。
8
9
//for-in循环
for
iin1...10 {
(i)
}
//for-each循环
(1...10).forEach {
($0)
}
3,移除函数参数的 var 标记 在
Swift函数中,参数默认是常量。过去可以在参数前加关键字var将其定义为变量,这样函数内部就可以对该参数进行修改(外部的参数任然不会被修改)。
6
age = 22
add(age)
func
add(age:Int) {
age += 1
现在这种做法已经被废弃,
Swift 3不再允许开发者这样来将参数标记为变量了。 4,所有函数参数都必须带上标签 过去如果一个函数有多个参数,调用的时候第一个参数无需带标签,而从第二个参数开始,必须要带标签。
1
letnumber = additive(8,b: 12)
additive(a:
,b:) ->{
returna + b
现在为了确保函数参数标签的一致性,所有参数都必须带上标签。
number = additive(a: 8,51); font-family:arial; font-size:14px">这个变化可能会造成我们的项目代码要进行较大的改动,毕竟涉及的地方很多。所以苹果又给出了一种不用给第一个参数带标签的解决方案。即在第一个参数前面加上一个下划线。 (不过这个只是方便我们代码从Swift2迁移到Swift3的一个折中方案,可以的话还是建议将所有的参数都带上标签。)
ASP.NET 线程池的大小有一定限制,这是ASP.NET可伸缩性方面的一个已知问题。在CPU繁忙的请求不构成问题的情况下,I/O繁忙的请求也可能会让服务器陷于 无线程可用的境地。性能计数器会告诉你服务器其实并不忙,但是却无法处理更多的请求。通过使用异步Controller,开发人员就可以避免这种情况的发生。
The static using declaration allows invoking static methods without the class
name.
In C# 5
using System;
Console.WriteLine("Hello,World!");
In C# 6
using static System.Console;
WriteLine("Hello,World");
2. 表达式方法(Expression-Bodied Methods)
使用表达式方法,只有一条语句的方法可以使用lambda语法写。
With expression-bodied methods,a method that includes just one statement can
be written with the lambda Syntax.
In C# 5
public bool IsSquare(Rectangle rect)
{
return rect.Height == rect.Width;
}
In C# 6
public bool IsSquare(Rectangle rect) => rect.Height == rect.Width;
3. 表达式属性(Expression-Bodied Properties)
跟表达式方法类似,只有一个get访问器的单行属性可以使用lambda语法写。
Similar to expression-bodied methods,one-line properties with only a get accessor
can be written with the lambda Syntax
In C# 5
public string FullName
{
get
{
return FirstName +"" + LastName;
}
}
In C# 6
public string FullName => FirstName +"" + LastName;
Auto-implemented properties can be initialized with a property initializer.
In C# 5
public class Person
{
public Person()
{
Age = 24;
}
public int Age {get; set;}
}
In C# 6
public class Person
{
public int Age {get; set;} = 42;
}
5. 只读自动属性(Read-Only Auto Properties)
C# 5需要完整的属性语法实现只读属性,C# 6可以使用自动属性实现。
To implement read-only properties,C# 5 requires the full property Syntax. With
C# 6,you can do this using auto-implemented properties.
In C# 5
private readonly int _bookId;
public BookId
{
get
{
return _bookId;
}
}
In C# 6
public BookId {get;}
With the new nameof operator,names of fields,properties,methods,or types can
be accessed. With this,name changes are not missed with refactoring.
In C# 5
public void Method(object o)
{
if (o == null) throw new ArgumentNullException("o");
In C# 6
public void Method(object o)
{
if (o == null) throw new ArgumentNullException(nameof(o));
7. Null传递操作符(Null Propagation Operator)
Null传递操作符简化了空值检查。
The null propagation operator simplifies null checks.
In C# 5
int? age = p == null ? null : p.Age;
var handler = Event;
if (handler != null)
{
handler(source,e);
}
In C# 6
int? age = p?.Age;
handler?.Invoke(source,e);
8. 字符串插值(String Interpolation)
字符串差值移除了对string.Format的调用,使用表达式占位符取代数字格式占位符。
The string interpolation removes calls to string.Format. Instead of using
numbered format placeholders in the string,the placeholders can include
expressions.
In C# 5
public override ToString()
{
return string.Format("{0},{1}",Title,Publisher);
}
In C# 6
public override ToString() => $"{Title} {Publisher}";
9. 字典初始化器(Dictionary Initializers)
字典可以使用类似集合的字典初始化器初始化。
Dictionaries can Now be initialized with a dictionary initializer―similar to the
collection initializer.
In C# 5
var dict = new Dictionary<int,string>();
dict.Add(3,"three");
dict.Add(7,"seven");
In C# 6
var dict = new Dictionary<int,string>()
{
[3] ="three",[7] ="seven"
};
10. 异常过滤器(Exception Filters)
异常过滤器允许你在捕获异常前进行过滤。
Exception filters allow you to filter exceptions before catching them.
In C# 5
try
{
//etc.
} catch (MyException ex)
{
if (ex.ErrorCode != 405) throw;
// etc.
}
In C# 6
try
{
//etc.
} catch (MyException ex) when (ex.ErrorCode == 405)
{
// etc.
}
11. 在Catch使用Await(Await in Catch)
await可以在catch块中直接使用,C# 5中需要变通使用。
await can Now be used in the catch clause. C# 5 required a workaround.
In C# 5
bool hasError = false;
string errorMessage = null;
try
{
//etc.
} catch (MyException ex)
{
hasError = true;
errorMessage = ex.Message;
}
if (hasError)
{
await new MessageDialog().ShowAsync(errorMessage);
}
In C# 6
try
{
//etc.
} catch (MyException ex)
{
await new MessageDialog().ShowAsync(ex.Message);
}
以上是小编为你收集整理的Chris Lattner 对 Swift 3 的总结与对 Swift 4 的展望全部内容。
如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。
关于Swift - Swift 3 新特性汇总和不同于以往版本的新变化的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于Acer Swift 3笔记本怎么样 Acer Swift 3笔记本上手图赏、ASP.NET MVC 2 新特性汇总、C# 6.0 新特性汇总、Chris Lattner 对 Swift 3 的总结与对 Swift 4 的展望的相关信息,请在本站寻找。