4.1中文 swift_swift ... ..< - CSDN
  • swift4.1中文

    2020-07-28 23:32:36
    swift4.1中文
  • \\\Xcode 9.3中的Swift 4.1带来了一些语言上的提升,包括Equatable和Hashable协议的自动实现和条件一致性等。\\条件一致性\\条件一致性即,泛型类型只有在其类型参数满足特定要求的时候才会遵循某个协议。例如,一个...
    \

    看新闻很累?看技术新闻更累?试试下载InfoQ手机客户端,每天上下班路上听新闻,有趣还有料!

    \
    \\

    Xcode 9.3中的Swift 4.1带来了一些语言上的提升,包括EquatableHashable协议的自动实现和条件一致性等。

    \\

    条件一致性

    \\

    条件一致性即,泛型类型只有在其类型参数满足特定要求的时候才会遵循某个协议。例如,一个Array只有当它的元素也遵循Equatable协议的时候,才能实现Equatable协议。这可以用下列声明表示。

    \\
    \extension Array: Equatable where Element: Equatable {\  static func ==(lhs: Array, rhs: Array) -\u0026gt; Bool { ... }\}\
    \\

    这弥补了Swift泛型上的缺陷,使得泛型适配器类型(例如,反映组合类型能力的泛型类型)完全成为可能。例如,在Swift 3中存在4种不同的处理“懒”项目的泛型类型:LazySequenceLazyCollectionLazyBidirectionalCollectionLazyRandomAccessCollection。其中每一种类型都重新实现或者继承了和组合类型相同的API。使用条件一致性,某一种类型的Sequence可以声明遵循懒协议,而无需定义任何中间泛型类型。

    \\

    Synthesized == and hashValue

    \\

    Swift 4.1编译器能够为那些声明遵循EquatableHahable协议的structs和enums,生成==hashValue方法的实现。以前,开发者必须自己实现那些方法,而那是一件非常繁琐的任务:在通常情况下,==方法的实现必须比较structs或unions的每一个相应成员;而hashValue方法必须设法保证返回一个唯一的字符串。在Swift 4.1,开发者只需声明遵循那些协议,相关的方法就会自动生成,提供所有存储的分别符合EquatableHashable的属性或枚举值。

    \\

    Swift 4.1中的其它显著改变如下:

    \\
    \let s = \"Hashable\"\let p = \\String.[s.startIndex]\s[keyPath: p] // \"H\"\
    \\ 完全实现智能KeyPaths。KeyPath现在支持下标、可选的链接和可选的强制换行。 \\ weakunowned关键词不能再用于协议中的属性声明。 \\ 与classes已存在的限制相似,Swift structs不再允许在一个不同的模块中定义一个初始化器来直接初始化struct的字段。在访问struct字段前,struct必须已经调用self.init进行了初始化。\\\

    想要全面了解所有变更,你可以访问官方的变更日志

    \\

    查看英文原文:Swift 4.1 Brings Conditional Conformance and More

    \\

    感谢罗远航对本文的审校。

    \

    给InfoQ中文站投稿或者参与内容翻译工作,请邮件至editors@cn.infoq.com。也欢迎大家通过新浪微博(@InfoQ@丁晓昀),微信(微信号:InfoQChina)关注我们。

    展开全文
  • The Swift Programming Language中文/英文版

    千次阅读 2016-01-16 14:38:57
    github:https://github.com/numbbbbb/the-swift-programming-language-in-chinese The Swift Programming Language 中文版 苹果官网:
    展开全文
  • 该文章翻译自apple官方文档:The Swift Programming Language(Swift 4.1) A Swift Tour -- 一个Swift 旅行 Tradition suggests that the first program in a new language should print the words “Hello, world!...

    该文章翻译自apple官方文档:The Swift Programming Language(Swift 4.1)

    A Swift Tour  -- 一个Swift 旅行

    Tradition suggests that the first program in a new language should print the words “Hello, world!” on the screen. In Swift, this can be done in a single line:
    print("Hello, world!")

    译:传统认为,一种新语言的第一个程序应该打印“Hello world!”在屏幕上。 用Swift,它可以用简单的一行代码搞定。

    If you have written code in C or Objective-C, this syntax looks familiar to you—in Swift, this line of code is a complete program. You don’t need to import a separate library for functionality like input/output or string handling. Code written at global scope is used as the entry point for the program, so you don’t need a main()function. You also don’t need to write semicolons at the end of every statement.

    译:如果你用C或 OC写代码,这语法看起来很熟悉 —— 在 Swift里,这行代码就是一个完整的程序。你不需要为输入/输出或字符串处理之类的功能导入单独的库。在全局范围内编写的代码用作程序的入口点,所以你不需要一个主函数功能。你也不需要在每一个语句的末尾写分号。

    This tour gives you enough information to start writing code in Swift by showing you how to accomplish a variety of programming tasks. Don’t worry if you don’t understand something—everything introduced in this tour is explained in detail in the rest of this book.

    译:这个旅行给你足够的信息可以开始用Swift写代码,它展示给你怎样完成各项编程任务。不要担心如果你不理解一些部分 ——这个旅行里每一项在本书的其他部分都有详细的介绍。

    NOTE

    For the best experience, open this chapter as a playground in Xcode. Playgrounds allow you to edit the code listings and see the result immediately.

    Download Playground

    提示:

    为了一个更好的体验,在Xcode的里用一个playground打开本章内容,Playgrounds 允许你编辑代码列表并且立马看结果。

     

     

    Simple Values -- 简单的值

    Use let to make a constant and var to make a variable. The value of a constant doesn’t need to be known at compile time, but you must assign it a value exactly once. This means you can use constants to name a value that you determine once but use in many places.

    1. var myVariable = 42
    2. myVariable = 50
    3. let myConstant = 42

    译:用 let 表示常量,用 var 表示变量,这个常量的值在编译时不需要知道,但是你必须马上赋给它一个确切的值。这意味着你可以用常量去为一个你定义的值命名,这个值可以在多处使用。

    A constant or variable must have the same type as the value you want to assign to it. However, you don’t always have to write the type explicitly. Providing a value when you create a constant or variable lets the compiler infer its type. In the example above, the compiler infers that myVariable is an integer because its initial value is an integer.
    If the initial value doesn’t provide enough information (or if there is no initial value), specify the type by writing it after the variable, separated by a colon.

    1. let implicitInteger = 70
    2. let implicitDouble = 70.0
    3. let explicitDouble: Double = 70

    EXPERIMENT

    Create a constant with an explicit type of Float and a value of 4.
     

    译:常量或变量必须与你赋给它的值有同样的类型.然而,然而你并不必须写出具体的类型.当你创建一个常量或变量时提供一个值可以让编译器判断它的类型,在上面的例子中,编译器判断 myVarible 是一个整数因为它的初始化值是一个整数.

    如果这个初始化的值没有提供足够的信息(或者如果没有初始化的值),可以通过在变量后面写下指定的类型,用冒号隔开

     尝试1
    创建一个有明确类型为Float的常量,值为4

     

    Values are never implicitly converted to another type. If you need to convert a value to a different type, explicitly make an instance of the desired type.

    1. let label = "The width is "
    2. let width = 94
    3. let widthLabel = label + String(width)

    EXPERIMENT

    Try removing the conversion to String from the last line. What error do you get?

    译:这个值是绝不能换成其他类型的,如果你需要将值转换为不同的类型,明确地做一个期望类型的实例
    实验
    试着从最后一行移除String转换,你得到什么错误?

     

    There’s an even simpler way to include values in strings: Write the value in parentheses, and write a backslash (\) before the parentheses. For example:

    1. let apples = 3
    2. let oranges = 5
    3. let appleSummary = "I have \(apples) apples."
    4. let fruitSummary = "I have \(apples + oranges) pieces of fruit."

    EXPERIMENT

    Use \() to include a floating-point calculation in a string and to include someone’s name in a greeting.
     

    译:这里有一个更简单的方式去在字符串里包含值:将这个值写进圆括号()里,并且在原括号前写一个反斜杠(\),例如:code
    实验
    用\()去在字符串里包含一个浮点值的计算并且在问候语里包含某个人的名字

     

    Use three double quotation marks (""") for strings that take up multiple lines. Indentation at the start of each quoted line is removed, as long as it matches the indentation of the closing quotation marks. For example:

    1. let quotation = """
      I said "I have \(apples) apples."
      And then I said "I have \(apples + oranges) pieces of fruit."
      """

    译:用三个双引号(" " ")来表示多行字符串。只要它匹配上结束引用标记的缩格,在每个引用行行头的缩格就是被删除的,例如:code

    Create arrays and dictionaries using brackets ([]), and access their elements by writing the index or key in brackets. A comma is allowed after the last element.

    1. var shoppingList = ["catfish", "water", "tulips", "blue paint"]
      shoppingList[1] = "bottle of water"
      
      var occupations = [
      "Malcolm": "Captain",
      "Kaylee": "Mechanic",
      ]
      occupations["Jayne"] = "Public Relations"

    To create an empty array or dictionary, use the initializer syntax.

    1. let emptyArray = [String]()
      let emptyDictionary = [String: Float]()

    If type information can be inferred, you can write an empty array as [] and an empty dictionary as [:]—for example, when you set a new value for a variable or pass an argument to a function.

    1. shoppingList = []
    2. occupations = [:]

    译:
    1.用([]) 方括号创建数组和字典,通过元素的索引或字典的key值访问到方括号中的元素。最后一个元素后面允许逗号.ex: code

    2. 用初始化语法创建一个空数组或字典

    3.如果类型信息可以被判断,你可以写一个空数组[],或空字典[:] -- 例如,当你设置一个值为一个变量或传一个参数给函数时.

     

     

    展开全文
  • 该文章翻译自apple官方文档:The Swift Programming Language(Swift 4.1)Swift 4.0 版本相对于前面几个版本来说,已经比较稳定,也终于可以利用下班时间关注和学习它了,鉴于官方文档是英文的,自己翻译过后也需要...

    该文章翻译自apple官方文档:The Swift Programming Language(Swift 4.1)

    Swift 4.0 版本相对于前面几个版本来说,已经比较稳定,也终于可以利用下班时间关注和学习它了,鉴于官方文档是英文的,自己翻译过后也需要记录下来,那就一起分享吧,一定有理解不充分和不足的部分,请斧正!

    既然不知从哪里开始合适,那就一页页来吧~

    About Swift

    Swift is a fantastic way to write software, whether it’s for phones, desktops, servers, or anything else that runs code. It’s a safe, fast, and interactive programming language that combines the best in modern language thinking with wisdom from the wider Apple engineering culture and the diverse contributions from its open-source community. The compiler is optimized for performance and the language is optimized for development, without compromising on either.

    Swift is friendly to new programmers. It’s an industrial-quality programming language that’s as expressive and enjoyable as a scripting language. Writing Swift code in a playground lets you experiment with code and see the results immediately, without the overhead of building and running an app.

    Swift defines away large classes of common programming errors by adopting modern programming patterns:

    • Variables are always initialized before use.

    • Array indices are checked for out-of-bounds errors.

    • Integers are checked for overflow.

    • Optionals ensure that nil values are handled explicitly.

    • Memory is managed automatically.

    • Error handling allows controlled recovery from unexpected failures.

    Swift code is compiled and optimized to get the most out of modern hardware. The syntax and standard library have been designed based on the guiding principle that the obvious way to write your code should also perform the best. Its combination of safety and speed make Swift an excellent choice for everything from “Hello, world!” to an entire operating system.

    Swift combines powerful type inference and pattern matching with a modern, lightweight syntax, allowing complex ideas to be expressed in a clear and concise manner. As a result, code is not just easier to write, but easier to read and maintain as well.

    Swift has been years in the making, and it continues to evolve with new features and capabilities. Our goals for Swift are ambitious. We can’t wait to see what you create with it.


    Swift 是一门写软件很好用的语言,不管是对于手机端,前端,后台还是任何一个可以运行代码的平台,它都是一门安全,快速,能相互影响的编程语言,它结合了最好的现代化语言思维和来自更广的苹果工程文化的智慧以及来自开源社区的多种多样的贡献。这编译器在性能方面做了优化,并且这语言在开发上做了优化,不妥协。

    Swift 对于初学者是很友好的,是一门既满足工业标准又脚本语言一样充满表现力趣味编程语言

    Swift通过接受现代化编程模式定义了大量的常用的编程错误的类

             变量在用之前要被初始化

             检查数组时候越界

             检查整数是否溢出

             确保nil 明确地被处理过

             自动管理内存

             错误处理允许从异常失败中控制恢复。

    Swift 代码充分利用现代硬件编译和优化。它的语法和标准库是基于‘写代码很显然的方式也应该运行的最好’的指导原则设计的。

    安全和快速的结合使得Swift 无论从最简单的‘hello world’还是对于开发整个操作系统,都成为一个杰出的选择

    Swift 结合了强大的类型推断和模式匹配与现代的,轻量级的语法,使复杂的想法,以明确和简明的方式表达。因此,代码不仅容易编写,而且更易于阅读和维护。

    Swift 已经发展多年,并且在继续发展新的特点和功能。我们的目标远大,我们已经迫不及待地想看看你能用它创造出什么了。




    展开全文
  • 引言 本文旨在帮助开发者快速从OC...另外需要说明的是,笔者也仅仅是刚刚接触Swift不久,如果有说的不对的地方,还望指正,这里贴出Swift中文翻译地址,方便大家可以深入了解SwiftSwift简介 Swift是一门开发...
  • \\\Swift 4.1发布正式版,它为Swift包管理器和Foundation带来了新的语言特性、构建选项和增强功能。\\正如InfoQ之前报道的那样,当Swift 4.1在Xcode 9.3 beta版本中初步可用时,语言级别最重要的新特性是条件一致性...
1 2 3 4 5 ... 20
收藏数 590
精华内容 236
关键字:

4.1中文 swift