Swift Learning (III.)

Source: Internet
Author: User

I. Type derivation & BASIC operations in Swift

Type deduction in Swift
    • If an identifier is defined with a direct assignment, then the type of the preceding identifier can be deduced based on the type of the subsequent assignment, so that the preceding identifier (: type) can be omitted
    • View identifier type: Option + left mouse button

  

    • Integral type
      • There are signed
        • 8-bit, 16-bit, 32-bit, 64-bit is the same as in C language.
        • Int: Peace table related, default equivalent to Nsinteger in OC
      • No sign
        • 8-bit, 16-bit, 32-bit, 64-bit is the same as in C language.
        • UINT: The peace table is related, the default equivalent to Nsuinteger in OC
Basic operations in Swift
    • In swift, you must ensure that the type is the same when you perform the basic operation, or you will get an error
      • Operations can be performed between the same types
      • Because there is no implicit conversion in swift
    • Conversion of data types
      • int type to double type: double (identifier)
      • Double type to int type: INT (identifier)

    

Logical branches in Swift

One, if branch statements
    • There is a certain difference between the IF statement in OC and the
      • Judgment sentence can not add ()
      • Judgment sentence result must be a true or false value

    

      • Correct wording:
        • Let y = 10
          If y > 0 {
          Print ("y > 0")
          } else {
          Print ("y <= 0")
          }

Second, switch branch statement
    • Basic usage consistent with OC
    • Different:
      • Switch can not follow ()
      • Break is not allowed after case (default is Break)
    • Example:
      • Let sex = 0
        Switch Sex {
        Case 0:
        Print ("male")
        Case 1:
        Print ("female")
        Default:
        Print ("from Thailand")
        }
    • Supplementary one: A case judge can judge multiple values, multiple values, separated
      • Let Sex1 = 0
        Switch Sex1 {
        Case 0, 1:
        Print ("normal")
        Default
        Print ("from Thailand")
        }
Normal person
    • Add two: Fallthrough keyword can be implemented case penetrating
      • Let Sex2 = 0
        Switch Sex2 {
        Case 0:
        Print ("Normal 0")
        Fallthrough
        Case 1:
        Print ("Normal 1")
        Default
        Print ("other")
        }
Normal person 0 Normal person 1  
    • Switch Special usage One: Supports floating-point type
      • Let PI = 3.14
        Switch PI {
        Case 3.14:
        Print ("π")
        Default
        Print ("notπ")
        }
    • Switch Special usage Two: support string type
      • Let M = 5
        Let n = 10
        var result = 0
        Let opration = "+"
        Switch Opration {
        Case "+":
        result = m + N
        Case "-":
        result = M-n
        Case "*":
        result = M * N
        Case "/":
        result = m/n
        Default
        result = 0
        }

        Print (Result)
the  
    • Switch Special usage two:switch support interval judgment
    • There are two common types of zones in Swift
      • Opening interval: 0.. <10 said: 0~9, not including 10
      • Closed interval: 0 ... 10 means: 0~10
      • Let score = 80
        Switch Score {
        Case 0..<60:
        Print ("Failed")
        Case 60..<80:
        Print ("Pass")
        Case 80..<90:
        Print ("good")
        Case 90..<100:
        Print ("excellent")
        Default
        Print ("Out of the box")
        }
Good three or three-mesh operator (consistent with OC)

Iv. Use of Guard
      • Guard is a new syntax for Swift2.0
      • It is very similar to the IF statement and is designed to improve the readability of the program
      • The guard statement must have an else statement with the following syntax:
        • When the conditional expression is true, skips the contents of the Else statement and executes the statement group contents
        • The conditional expression is false when the content in the Else statement is executed, and the jump statement is typically return, break, continue, and throw
      • Format:
        • Guard conditional expression-else {
          Bar Change Statement
          Break
          }
          Statement Group
      • Example:
        • var manAge = 18
          Func hit (age:int), Bool {
          Guard Age >=-else {
          Print ("Hitting is not illegal")
          return False
          }
          Print ("Hitting the crime")
          return True
          }
          Hit (ManAge)
        • Hitting a crime.

Loops in Swift

One, for loop
    • General wording
      • for var i = 0; I < 3; i++ {
        Print (i)
        }
    • Interval for loop
      • For I in0. <3 {
        Print ("--", i)
        }
    • Special wording: If subscript i is not required in the For loop
      • For_in0 ... 3 {
        Print ("Hello Siri")
        }
Second, while & does while loops
    • While the judgment of the while in swift must be correct and true, no 0 is True
      • var AA = 0
        While AA < 3 {
        aa++//This sentence will die if you don't write it.
        Print ("Hi Siri")
        }
    • Do While loop: Use the Repeat keyword instead of the Do
      • var bb = 0
        Repeat {
        bb++
        Print ("BBB")
        } while BB > 3
BBB

Swift Learning (III.)

Related Article

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.