您的位置:首页 > 移动开发 > Swift

Start Developing iOS Apps (Swift) 学习笔记 (1)

2015-10-13 09:15 471 查看
一、Learn the Essentials of Swift

1、A constant is a value that stays the same after it’s declared the first time, while a variable is
a value that can change. A constant is referred to as immutable, meaning that it can’t be changed, and a variable is mutable.(constant 设定值之后不可变,[b]variable 设定值之后可以改变[/b])

2、An implicitly unwrapped optional is an optional that can also be used like a nonoptional value, without the need to unwrap
the optional value each time it’s accessed. This is because an implicitly unwrapped optional is assumed to always have a value after that value is initially set, although the value can change.(隐式解包可选类型可以像非可选类型值一样使用,并不需要在每次使用时都解包一次。这种用法的前提是隐式解包类型始终是有值的)

var implicitlyUnwrappedOptionalInt: Int!




2、Use optionals to
work with values that might be missing. An optional value either contains a value or contains
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.

var optionalInt: Int?
= 9

3、Use optional binding in an
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)"

}


4、A
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)"

}


5、The half-open range operator (
..<
) 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.(..<
是一个前闭后开的区间,... 是一个闭区间)


6、 The underscore (
_
)
represents a wildcard, which you can use when you don’t need to know which iteration of the loop is currently executing. (_
是一个通配符)


7、A function is a reusable, named piece of code that can be referred to from many places in a program.([b]function 一个可重用的代码段)[/b]

8、An object is an instance of a class,
which can be thought of as a blueprint for that object. Classes store additional information about themselves in the form of properties, and define their behavior using methods.(object 是[b]class的一个实例,[b]class是[b]object的设计蓝图[/b][/b][/b])

9、. An initializer is a method that prepares an instance of a class for use, which involves setting an initial value for each
property and performing any other setup.(initializer是一个method,该method为class实例的使用做一些准备工作)

10、 A failable initializer can return
nil
after
initialization. Use
init?
to declare a failable initializer.([b]ailable
initializer 可以在初始化后,返回一个nil,所以使用的时候,需要检查是否返回的是nil
)[/b]

11、A
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
表明,每个继承的子类都需要实现自己的构造器)


12、Use the optional type cast operator (
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.(可选类型转换,需要在转换后检查是否转换成功)

Use the forced type cast operator (
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.

13、A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality.(protocol 定义了特定任务的蓝图,不需要实现)

二、Build a Basic UI

1、The
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. The
AppDelegate
class
is where you write your custom app-level code. (定义了AppDelegate类,app delegate的蓝图)

三、Connect the UI to Code

1、In a storyboard, a scene represents
one screen of content and typically one view controller.(scene是指一个screen

2、The
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 是排在接受队列中的第一位,接收类似键盘事件,动作事件等)


(未完,待续)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: