iOS Development Note--swift Syntax basics

Source: Internet
Author: User
<span id="Label3"></p> <ol> <ol> <li><strong>Underlying data type</strong></li> </ol> </ol><p><p>The types of Swift are proposed on the basis of C and objective-c, int is integer, double and float are floating-point types; bool is boolean; string is a string. Swift also has two useful collection types, array and dictionary, please refer to the <span style="color: #0000ff;">collection type</span> .</p></p><p><p>Swift also adds types such as tuples (tuple) that are not in the OBJECTIVE-C. Tuples allow you to create or pass a set of data, such as the return value of a function, you can use a tuple to return multiple Values.</p></p><p><p>Swift also adds an optional (Optional) type to handle the case of missing Values.</p></p> <ol> <ol> <li><strong>1. Declaring constants and variables</strong></li> </ol> </ol><p><p>Constants and variables must be declared before use, declare constants with let, and declare variables with var. The following example shows how to use constants and variables to record the number of times a user attempts to log on:</p></p><p><p>Let maximumnumberofloginattempts = 1</p></p><p><p><span class="keyword">var currentloginattempt = 0</span></p></p> <ol> <ol> <li><span class="keyword">2. Type Callout</span></li> </ol> </ol><p><p>When you declare a constant or variable, you can add a type callout (type annotation), indicating the type of the value to be stored in a constant or variable. If you are adding a type callout, you need to add a colon and a space after the constant or variable name, and then add the type Name.</p></p><p><p>This example adds a type callout to the Welcomemessage variable, which indicates that the variable can store a value <span class="keyword">of type string<br></span></p></p><p><p><span class="keyword">var welcomemessage:string</span></p></p><p><p>Note: in general, you rarely need to write type Callouts. If you assign an initial value when declaring a constant or variable, swift can infer the type of the constant or variable, please refer to type safety and type Inference. In the above example, Welcomemessage is not assigned an initial value, so the type of the variable welcomemessage is specified by a type callout, not by the initial Value.</p></p><p><p></p></p> <ol> <ol> <li><span style="font-size: larger;">3. Type safety and type speculation</span></li> </ol> </ol><p><p>Swift is a type safe language. A Type-safe language gives you a clear idea of the type of value the code will Handle. If your code requires a string, you can never accidentally pass in an int.</p></p><p><p></p></p> <ol> <ol> <li><span style="font-size: larger;">4. Boolean values</span></li> </ol> </ol><p><p>Swift has a basic bool (boolean) type, called Bool. Boolean values are logical (logical), because they can only be true or False. Swift has two Boolean constants, true and False:</p></p> <ol class="dp-c"> <ol class="dp-c"> <ol class="dp-c"> <li class="alt">Let Orangesareorange = <span class="keyword">true</span></li> <li>Let turnipsaredelicious = <span class="keyword">false<br></span></li> </ol> <li><span style="font-size: larger;">5. Tuples</span></li> </ol> </ol><p><p>Tuples (tuples) combine multiple values into a single composite Value. Values within a tuple can be of any type and are not required to be of the same type.</p></p><p><p>In the following example, (404, "not Found") is a tuple that describes the HTTP status Code. The HTTP status code is a special value returned by the Web server when you request a Web Page. If the page you requested does not exist, it will return a 404 Not Found status Code.</p></p> <ol class="dp-c"> <ol class="dp-c"> <li class="alt">Let Http404error = (404, <span class="string">"not Found")</span></li> <li><span class="comment">The type of Http404error is (Int, String), The value is (404, "not Found")</span></li> </ol> </ol><p><p>(404, "not Found") tuples combine an int value with a string value to represent two parts of an HTTP status code: a number and a human-readable Description. This tuple can be described as "a tuple of type (Int, String)".</p></p><p><p>You can combine any sequence of types into a single tuple that can contain all Types. As long as you want, you can create a tuple of type (int, int, int) or (String, Bool) or any other combination you Want.</p></p><p><p>You can decompose the contents of a tuple (decompose) into separate constants and variables, and then you can use them normally:</p></p> <ol class="dp-c"> <ol class="dp-c"> <ol class="dp-c"> <li class="alt">Let (statusCode, Statusmessage) = Http404error</li> <li>println (<span class="string">"the status code is \ (statusCode)")</span></li> <li class="alt"><span class="comment">Output "the status code is 404"</span></li> <li>println (<span class="string">"the status message is \ (statusmessage)")</span></li> <li class="alt"><span class="comment">Output "the status message is not Found"<br></span></li> </ol> </ol> </ol><p><p>If you only need a subset of the tuple values, you can mark the part you want to ignore with an underscore (_) when decomposing:</p></p> <ol class="dp-c"> <ol class="dp-c"> <li class="alt">Let (justthestatuscode, _) = Http404error</li> <li>println (<span class="string">"the status code is \ (justthestatuscode)")</span></li> <li class="alt"><span class="comment">Output "the status code is 404"</span></li> </ol> </ol><p><p>In addition, you can use the subscript to access a single element in a tuple, with the subscript starting from zero:</p></p> <ol class="dp-c"> <ol class="dp-c"> <ol class="dp-c"> <li class="alt">println (<span class="string">"the status code is \ (http404error.0)")</span></li> <li><span class="comment">Output "the status code is 404"</span></li> <li class="alt">println (<span class="string">"the status message is \ (http404error.1)")</span></li> <li><span class="comment">Output "the status message is not Found"<br></span></li> </ol> </ol> </ol><p><p>You can name a single element when you define a tuple:</p></p> <ol class="dp-c"> <ol class="dp-c"> <li class="alt">Let Http200status = (statuscode:200, description: <span class="string">"ok")<br></span></li> </ol> </ol><p><p>After naming the elements in a tuple, you can get the values of these elements by name:</p></p> <ol class="dp-c"> <ol class="dp-c"> <ol class="dp-c"> <li class="alt">println (<span class="string">"the status code is \ (http200status.statuscode)")</span></li> <li><span class="comment">Output "the status code is 200"</span></li> <li class="alt">println (<span class="string">"the status message is \ (http200status.description)")</span></li> <li><span class="comment">Output "the status message is ok"<br></span></li> </ol> <li><span style="font-size: larger;">6. Optional</span></li> </ol> </ol><p><p>Use optional (optionals) to handle situations where values may be missing. An optional representation:</p></p><p><p>-have a value equal to X or no value</p></p><p><p>Swift's String type has a method called ToInt that converts a string value into an int value. however, not all strings can be converted to an integer. The string "123" can be converted to the number 123, but the string "hello, world" does not Work.</p></p><p><p>The following example uses the ToInt method to try to convert a string into an int:</p></p> <ol class="dp-c"> <ol class="dp-c"> <li class="alt">Let Possiblenumber = <span class="string">"123"</span></li> <li>Let Convertednumber = Possiblenumber.toint ()</li> <li class="alt"><span class="comment">Convertednumber is assumed to be of type "int?", or type "optional int"</span></li> </ol> </ol><p><p>Because the ToInt method may fail, it returns an optional (optional) int instead of an int. An optional int is written int? instead of int. The question mark implies that the value contained is optional, that is, may contain an int value or may not contain a Value. (cannot contain any other value, such as a bool value or a string value.) It can only be int or nothing. )</p></p><p><p><strong>If statements and forced parsing</strong></p></p><p><p>You can use the IF statement to determine whether an optional value is Included. If there is an optional value, the result is true, and if there is no value, the result is False.</p></p><p><p>After you have determined that the optional package does contain a value, you can add an exclamation mark (!) to the optional Name. To get the Value. This exclamation point says "i know this option has a value, please use it." "this is called forced parsing of optional values (forced unwrapping):</p></p><p><p></p></p><p><p><span style="font-size: larger;"><strong>Assertion</strong></span></p></p><p><p>Optional allows you to determine if a value exists, and you can gracefully handle the absence of a value in your Code. however, in some cases, if the value is missing or the value does not meet a specific condition, your code may not need to proceed. At this point, you can trigger an assertion (assertion) in your code to end the code run and debug to find the reason for the missing Value.</p></p><p><p></p></p><p><p><strong>Using Assertions for debugging</strong></p></p><p><p>The assertion determines whether a logical condition is true at run Time. In the literal sense, the assertion "asserts" whether a condition is True. You can use assertions to ensure that some important conditions have been met before running other Code. If the condition evaluates to true, the code will continue to run, and if the condition evaluates to false, the code is stopped and your app is Terminated.</p></p><p><p></p></p><p><p>If your code triggers an assertion in the debug environment, such as when you build and run an app in Xcode, you can clearly see where the illegal state occurs and check the state of your app when the assertion is Triggered. additionally, assertions allow you to attach a debug message.</p></p><p><p></p></p><p><p>You can use the global assert function to write an Assertion. An expression with a result of true or false is passed to the assert function, and a message is displayed when the expression is false:</p></p> <ol class="dp-c"> <ol class="dp-c"> <ol class="dp-c"> <li class="alt">Let Age =-3</li> <li>Assert (age >= 0, <span class="string">"A person ' s cannot is less than Zero")</span></li> <li class="alt"><span class="comment">Because age < 0, the assertion triggers<br></span></li> </ol> </ol> </ol><p><p></p></p><p><p>iOS Development Note--swift Syntax basics</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.