Swift基础(Part1)

作者: Girl_iOS | 来源:发表于2015-11-18 00:13 被阅读356次

    前段时间,做了一些笔记,都是很基础的,接下来准备用几天的时间来做个小的总结整理.

    • Question 1: Swift中何时用?何时用!

    Answer: 一般?声明的为可选类型,可以先读读文档Optional Chaining来理解下.简单来讲就是,如果一个对象的类型为String?那么它就有两种可能:一种是没有设置的,即nil,另一种就是String的值.而当该对象用!来取值时,表示确信其一定会有String值,此时值若为nil,程序会报错崩溃.文章Swift之?和!里面有该问题的深入剖析.另外CS193p 57分钟以后也会有关于此问题的简单讲解.其中有几句话能够便于理解 when it is not set,it's nil. Or something. String? So we would call this an optional String,But it's really an optional,it's not a String that can be nil,it's an optional that can be a String

    • Question 2: 什么时候用Class,什么时候用Struct?

    Answer: 搞清楚这个问题之前,有必要复习下基础知识Classes and Structures
    从文档这句话structure instances are always passed by value, and class instances are always passed by reference.可以看出其最大的区别在于structure为值传递而class为引用传递.这个问题的回答中有个例子,可以解释这其中的区别.而文章When to Use Swift Structs and Classes中指出use structs when you need value semantics, and use classes when you need reference semantics. 也可以看看中文译文.

    • Question 3: "Call can throw,but it is not marked with ’try’ and the error is not handled"是什么错误?

    Answer: 老项目升级到Swift2.0时,遇到了这个问题.从提示中可以看出,我们没有做Error Handle.这是苹果为提高Swift的安全性在Swift2.0所加的新特性.可以看看下面的文章来学习下:

    最后来看集WWDC吧:) What's New in Swift

    Girl学iOS100天 第21天

    相关文章

      网友评论

        本文标题:Swift基础(Part1)

        本文链接:https://www.haomeiwen.com/subject/bpxlhttx.html