var implicitlyUnwrappedOptionalInt: Int!
nil
(no value) to indicate that a value is missing. Write a question mark (
?
) after the type of a value to mark the value as optional.
if
statement to check whether an optional contains a value.
var optionalName: String? = "John Appleseed"
var greeting = "Hello!"
if let name = optionalName {
greeting = "Hello, \(name)"
}
where
clause can be added to a case to further scope the conditional statement.
var optionalHello: String? = "Hello"
if let hello = optionalHello where hello.hasPrefix("H"), let name = optionalName {
greeting = "\(hello), \(name)"
}
..<
) doesn’t include the upper number, so this range goes from
0
to
3
for a total of four loop iterations. Use the
closed range operator
(
...
) to make a range that includes both values.
(..< 是一个前闭后开的区间,... 是一个闭区间)
_
) represents a wildcard, which you can use when you don’t need to know which iteration of the loop is currently executing.
(_ 是一个通配符)
nil
after initialization. Use
init?
to declare a failable initializer.
(ailable initializer 可以在初始化后,返回一个nil,所以使用的时候,需要检查是否返回的是nil)
required
keyword next to an initializer indicates that every subclass of the class that has that initializer must implement its own version of the initializer (if it implements any initializer).
(构造器前的required
表明,每个继承的子类都需要实现自己的构造器)
as?
) when you’re not sure if the downcast will succeed. This form of the operator will always return an optional value, and the value will be
nil
if the downcast was not possible. This lets you check for a successful downcast.
(可选类型转换,需要在转换后检查是否转换成功)
as!
)
only when you’re sure that the downcast will always succeed. This form of the operator will trigger a runtime error if you try to downcast to an incorrect class type.
AppDelegate.swift
source file has two primary functions:
-
It creates the entry point to your app and a run loop that delivers input events to your app. This work is done by the
UIApplicationMain
attribute (@UIApplicationMain
), which appears toward the top of the file.UIApplicationMain
creates an application object that’s responsible for managing the life cycle of the app and an app delegate object, which is described below.(创建了app的入口点和一个run loop, 该run loop分发输入事件给app)
-
It defines the
AppDelegate
class, the blueprint for the app delegate object. The app delegate creates the window where your app’s content is drawn and provides a place to respond to state transitions within the app. TheAppDelegate
class is where you write your custom app-level code. (定义了AppDelegate类,app delegate的蓝图)
UITextFieldDelegate
protocol contains optional methods, which means that you’re not required to implement them. But to get the specific behavior you want, you’ll need to implement two of these methods for now:
func textFieldShouldReturn(textField: UITextField) -> Bool
func textFieldDidEndEditing(textField: UITextField)
To understand when these methods get called and what they need to do, it’s important to know how text fields respond to user events. (UITextFieldDelegate自带两个方法,理解他们何时被调用,以及他们做的工作)
textFieldShouldReturn(_:)
method, which gets called when the user taps Return (or in this case, Done) on the keyboard. (textFieldShouldReturn方法在用户轻按键盘上的return键或Done键时,被调用)
textFieldDidEndEditing(_:)
, is called after the text field resigns its first-responder status.(textFieldDidEndEditing方法,在text field放弃第一响应者身份时,被调用
)
3、 In an app, the first responder is an object that is first on the line for receiving many kinds of app events, including key events, motion events, and action messages, among others.(first responder 是排在接受队列中的第一位,接收类似键盘事件,动作事件等)
(未完,待续)