Simple use of Swift

Source: Internet
Author: User

"Swift is a new programming language for writing Ios,os X and watchOS applications. Swift combines the advantages of C and objective-c and is not limited by C compatibility. Swift uses a secure programming model and adds a lot of new features, which makes programming easier, more flexible, and more fun. Swift is based on a mature and beloved Cocoa and Cocoa Touch framework, and its advent will redefine software development.


Swift's development began a long time ago. To lay the groundwork for Swift, Apple has improved the compiler, debugger, and framework structure. We use automatic reference counting (Automatic Reference counting, ARC) to simplify memory management. We build a framework stack based on foundation and Cocoa to modernize and standardize it completely. The objective-c itself supports blocks, set syntax, and modules, so the framework can easily support modern programming language technology [...] "


Constants and variables

Func Demo3 () {//only defines the type, and does not set a value let X:int//constants Define completion type, can have ' once ' set chance x = 10//Set, no further modification//x = 20 Constant ' x ' used before being initialized//constants must be initialized before using the print (x)}///if the defined variable/constant needs to specify the type you want, you can use the ': Type ' func Demo2 () {Let x:double = ten let y = 10.5 print (x + y)}///Auto Derivation-the type of the left variable/constant can be deduced based on the calculation results on the right///Option + Click Hotkey Non- Often important, you can quickly see the exact type of the variable///However: Swift is very strict with type conversion, and data between any different types is not allowed to be computed///Int and Double are structs, not basic data types! Func demo1 () {///integer default type is int, if the 64-bit machine, the LONG/32-bit machine is int let x = ten//Swift, decimal default is double, double, precision higher/OC default is float, floating point number let y = 10.5//Binary operator ' + ' cannot is applied to operands of type ' Int ' and ' Double '//Print (x + y)//If you want to calculate for different types of data, you must manually convert the type//OC if you want to do type conversion ' (int) y '//Swift, using the ' int () ' constructor ' init ' print (x + int    (y)) print (Double (x) + y)}func demo () {//define constant Let, once set, cannot modify//define VAR, once set, can be modified//int x = 10; Let x = ten//x = var y = y = + print (x)   Print (y)} Knowledge point collation constants and variable definitions define constants Let, once set, cannot be modified; variable var, once set, can be modified by allow x = 10//x = 20//constant cannot modify var y = 20y = 100 integer with decimal default type integer default The type is int, if the 64-bit machine, the LONG/32 bit machine is the INT decimal number default is double, double precision, precision higher than the calculation between different data types, need to pay attention to what swift requires for type conversion is very strict, the data between any different types is not allowed to calculate, You must manually convert the type let x = 10let y = 10.5//Binary operator ' + ' cannot is applied to operands of type ' Int ' and ' Double '//print (x +  Y)//If you want to calculate for different types of data, you must manually convert the type//OC if you want to do type conversion ' (int) y '//Swift, using the ' int () ' constructor ' init ' print (x + int (y)) print (Double (x) + Y) Automatic derivation is what can be derived from the right side of the calculation results, the left variable/constant type//because 10 by default is the type of int, the derivation of the type of x is Intlet x = 10//because the 10.5 default is a double type, deduced that the type of y is doublelet y = 10 The. 5option + Click Hotkey is very important to quickly see the exact type of variable displayed for the specified constant or variable type if the defined variable/constant needs to specify the type you want, you can use: Type//display specifies that the type of x is Double, using: Type let x:double = 10 constant and variable usage note constants or variables must be initialized before they are used, x:int//is not initialized, the exception is reported: Constant ' x ' used before being Initializedprint (x) constants define the completion type, You can set the opportunity once, after the constant is set, do not allow to modify the let x:int//constant after defining the completion type, you can have the ' once ' set opportunity x = 10//constant Set, do not allow to modify//X = 20 constants and variable selection only when the need to change, should be used If Var is never changed, Xcode will prompt you to use letint and the nature of the double is what structure
Constant ' x ' used before being initialized (constants must be initialized before use) Binary operator ' + ' cannot be applied to operands of type ' Int ' and ' double ' (+ cannot be applied in int type and double type) Supplement: All Swift basic data types are struct Int Double Bool String floatswift As long as constant is used, as long as the variable is var






Simple use of Swift

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.