Mishra Abhishek

Swift iOS 24-Hour Trainer


Скачать книгу

allows you check if an optional has a value and, at the same time, extract this value into a constant or variable. Optional bindings can only be used in if and while statements, both of which are covered later in this lesson. Consider the following example:

      In this example, the optional score will automatically be unwrapped into the constant unwrappedScore if it contains a value. unwrappedScore can now be used just like a normal constant and does not need any further unwrapping.

      If an optional is guaranteed to always contain a value, then you can skip having to unwrap it every time by implicitly unwrapping it when you declare the optional. An implicitly unwrapped optional is declared with an exclamation mark after the data type, instead of a question mark. Attempting to assign nil to an implicitly unwrapped option will result in a compile time error.

      Some scenarios where you may want to use an implicitly unwrapped optional are:

      • As a return value from a function, this ensures the function will not return nil.

      • As an argument to a function, this ensures the function cannot be called with a nil argument.

      • When creating IBOutlets to elements in xib files. Interface builder and xib files will be covered in Lesson 9.

      In the following example, score is an implicitly unwrapped optional:

      Control Flow Statements

      A control flow statement allows you to modify the order of statements executed, execute certain statements multiple times, or execute certain statements conditionally.

      if-else

      The if statement is one such control statement. In its basic form an if statement executes a block of statements only if a specific condition is met.

      The test condition is usually a Boolean variable, or an expression that evaluates to a Boolean variable. If the test condition evaluates to true, the following statement (or block of statements) is executed. The following is a simple example:

      In this hypothetical game example, a player is required to collect a certain number of red and blue marbles. A player wins the game if he collects more red marbles than blue ones. The test condition in this case is the expression

      which evaluates to true in this particular case. Note the complete if statement contains both the test condition and the block of statements that go with the test condition.

      Конец ознакомительного фрагмента.

      Текст предоставлен ООО «ЛитРес».

      Прочитайте эту книгу целиком, купив полную легальную версию на ЛитРес.

      Безопасно оплатить книгу можно банковской картой Visa, MasterCard, Maestro, со счета мобильного телефона, с платежного терминала, в салоне МТС или Связной, через PayPal, WebMoney, Яндекс.Деньги, QIWI Кошелек, бонусными картами или другим удобным Вам способом.

/9j/4AAQSkZJRgABAQEAlgCWAAD/2wBDAAQCAwMDAgQDAwMEBAQEBQkGBQUFBQsICAYJDQsNDQ0LDAwOEBQRDg8TDwwMEhgSExUWFxcXDhEZGxkWGhQWFxb/2wBDAQQEBAUFBQoGBgoWDwwPFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhb/wAARCAMPA2gDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFV