swift-1-Introduction & Simple Values

Source: Internet
Author: User

1. Introduction:

Swift is a new programming language for iOS and OS X to be programmed, based on C and objective-c, without some compatibility constraints for C. Swift uses a secure programming model and adds modern features to make programming easier, more flexible, and more fun. The interface is based on the cocoa and cocoa touch framework, which is popular with the people, and shows the new direction of software development.

2.Swift Getting Started a new language learning should start with the print "Hello, World". In Swift, it is a line: println ("Hello, World")

Unlike the print functions of printf and objective_c,swift in C, which are println, and the swift language, there is no need to add semicolons as end flags at the end of each statement, and of course, add is not wrong.

Playground

Allows you to edit the code and immediately

See the results

For better results, you can open the Create playgroud on x-code6 and playground allow you to edit the code and see the results immediately.

3.Swift Basic Use

1. Simple values: Variable constants

1.1 Make let to define constants, VAR defines variables. The value of the variable? Do not need to be specified at compile time, but at least one time to assign a value. This means that you use a variable to name a value, and you find that you only need to be sure that it is used in more than one place.

Variable: var

var myvariable = 42

myvariable = 50

Constant let:

Note: The constants here are defined like the variables in a functional programming language, once assigned, cannot be modified. To make a lot of use for good health .

Let myconstant = 42

Myconstant = 50 (will error)

A constant or variable must have the same type as the assignment. So you don't? Use strictly defined types. Provide a value you can create a constant or variable and have the compiler infer its type. In the upper-case, the compiler infers that myvariable is an integer type because its initialization value is an integer

If the initialization value is not provided? enough information (or no initialization value), you can write the type after the variable name, separated by a colon;

As follows: When initializing, the variables are directly described as integral types

var item:int = 5;

To force type conversions:

Values are never implicitly converted to other types.        What if you need to convert? A value to a different type, a definite construct? An instance of the desired type. Let label = "the width is"

Let width = 94 let Widthlabel = Label + String (width)

Note: string (width) Converts the width constant from an integral type to a string type, and two string variables are linked with a plus sign, at which point the value of Widthlabel is "the width is 94".

Expand:

there is a simpler way to convert a value to a string: write the value in parentheses, and write a backslash before the parentheses. For example:Let apples = 3let oranges = 5let applesummary = "I has \ (apples) apples." Let fruitsummary = "I had \ (apples + oranges) pieces of fruit." Input fruitsummary = "I have 8 pieces of fruit; use \ () to convert a floating-point calculation into a string, and add someone's name, and say hello to him. use square brackets [] to create arrays and dictionaries, and use subscripts or keys (key) to access elements, similar to the new features of X-code in OC codevar shoppinglist = ["Catfish", "water", "tulips", "Blue paint"]shoppinglist[1] = "bottle of water" var occupations = ["Malc Olm ":" Captain "," Kaylee ":" Mechanic "," Jayne ":" Public Relations "]occupations[" Jayne "] =" Public Relations " to create an empty array or dictionary, use the initialization syntax. Let Emptyarray = string[] () Let emptydictionary = Dictionary () If the type information can be inferred, you can use [] and [:] To create an empty array and an empty dictionary--just as you declare a variable or give a function the same as when passing parameters. Shoppinglist = []//GO shopping and buy something

swift-1-Introduction & Simple Values

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.