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

一、Learn the Essentials of Swift

1、 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 设定值之后不可变,variable 设定值之后可以改变


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 implicitlyUnwrappedOptionalIntInt!



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.

  1. var optionalName: String? = "John Appleseed"
  2. var greeting = "Hello!"
  3. if let name = optionalName {
  4. greeting = "Hello, \(name)"
  5. }

4、A   where clause can be added to a case to further scope the conditional statement.
  1. var optionalHello: String? = "Hello"
  2. if let hello = optionalHello where hello.hasPrefix("H"), let name = optionalName {
  3. greeting = "\(hello), \(name)"
  4. }


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. function 一个可重用的代码段)

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 是class的一个实例,class是object的设计蓝图


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. ailable initializer 可以在初始化后,返回一个nil,所以使用的时候,需要检查是否返回的是nil



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:
  1. func textFieldShouldReturn(textField: UITextField) -> Bool
  2. 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 是排在接受队列中的第一位,接收类似键盘事件,动作事件等




(未完,待续)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值