Swift (a) simple value

Source: Internet
Author: User

According to international practice, the first program to learn a new language is to output a "Hello, world!" on the screen. In Swift, a line of code is done:

If you have written C or objective-c code before, you must look familiar with this syntax-in swift, this line of code is already a complete program. You do not need to import a class library that is used for functions like input/output or string processing alone. Code written at the global scope is used as the entry point for the program, so the main function is not required. You do not need to add a semicolon after each line of statements.

This series of tutorials gives you enough information to start writing code with Swift by completing a series of programming examples. If there's something you don't understand, don't worry-everything in this tutorial will be explained in more detail in other articles.

Define constants using let, define variables, and use Var. The value of a constant does not need to be known at compile time, but it must be assigned only once. That is, you can use constants to name a value, one assignment at a time.




A constant or variable must have the same type as the value it specifies. However, you do not have to explicitly write out the type. A value is provided when a constant or variable is defined, and the compiler will speculate on its type. In the example above, the compiler speculates that myvariable is an integral type because its initial value is an integer.

If the initial value does not provide enough information (or no initial value), write a colon after the variable name, plus the type name.





Values are not implicitly converted to other types. If you need to convert a value to a different type, you need to display an instance of the creation target type.




在字符串里加入值还有个更简单的方法:将值写在括号里,再在括号前加上反斜杠\。如:



要创建数组和字典,使用方括号 [] ,要访问他们的元素,就在方括号里写索引活着键名。



To create an empty array or dictionary, use the following initialization syntax.



If the type can be inferred, you can write [] to represent an empty array, [:] to represent an empty dictionary--for example, when you set a new value for a variable or pass a parameter to a function.

Swift (a) simple value

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.