[Swift-Summary] basic usage, swift-summary basis

Source: Internet
Author: User
Tags try catch

[Swift-Summary] basic usage, swift-summary basis
Declare variables and constants

// Use var to declare a variable var str = "Hello, playground" str = "Hello world"; // use let to declare a constant let str1 = "I Am a handsome guy "; str1 = "I'm not a handsome guy"; // the following error will be reported: let name = "I Am a handsome guy"; // declare a constant and the type is automatically inferred as String var age: int = 20; // declare a variable to specify the proper type
Output statement
// In swift1, println is changed to print after swift2.0 ("I have said a sentence \ (name)"); print ("My age is \ (age) ");
Integer

Swift provides signed and unsigned 8-bit, 16-bit, 32-bit, and 64-bit types

Obtain the border of an integer
Let minValue = UInt8.min; // get the minimum value of the unsigned 8-bit integer let macValue = UInt8.max; // obtain the maximum value of the unsigned 8-bit integer
Int type

The int type contains almost common integers. The maximum value of a 64-bit platform is 9223372036854775807.

The above two points also apply to the unsigned type

Floating Point Number
Let pi = 3.14; // swift infers the Double type by default.
Sum of integer and floating point
Let sum = 2 + 2.14; // This does not require forced type conversion let count = 2; lei sum = pi + Double (count); // This requires forced type conversion
Typealias type with the same name

Similar to typedef in C Language

Typealias Type = UInt16; // Type is the minimum value of the UInt16 Type var TypeMin = Type. min; // UInt16
Boolean Value

Boolean values true and false in swift
Boolean values of YES and NO in OC

var flag = true;    flag = false;
Tuples

Tuples are combinations of several arbitrary types of values.
For example

Let myTuples = (20, "Boiling boiling"); // myTuples is a binary tuples. The serial number of 20 is 0, and the "Boiling boiling" is 1let myTuples1 = (20, "Boiling", "handsome guy"); // This is a triple
Access to tuples
// Use the serial number to access print ("My age is \ (myTuples.0 )"); // my age is 20 \ n // assign this tuples to another tuples receiving let (myName, myAge) = myTuples; print ("my name is \ (myName) "); // If you only need some values in the tuples, use the following method to let (age1, _) = myTuples; print (" my age is \ (age1 )");

You can also use the following method to declare and use

Let myInfo = (name: "Boiling", age: 22); print ("my name is \ (myInfo. name), my age is \ (myInfo. age )");
Optional value: optional

Optional may be a value or not a value. If it is not a value, it is nil. In swift, nil is a definite type, not a pointer.

Var myStr: String ?; // MyStr is nilvar myStr: String; // myStr is nothing
MyStr = "1234"; var num = Int (myStr); // is inferred as 1234, Int? Type var myStr1 = "hell" var num1 = Int (myStr1); // Conversion failed
Use if let to bind optional values
// Num is an optional value if let constNum = num {print ("num has a value \ (constNum )"); // num is successfully converted to positive number} else {print ("num has no value ");}
Multiple to multiple
// If num has a value, num1 has a value if let constNum = num, constNum1 = num1 {print ("num has a value of \ (constNum )");} else {print ("num has no value ");}
Optional for implicit Parsing

Implicit Parsing is optional. It is mainly used to construct a class.

Implicit choice is actually a common choice, but it can also be used as a common type.

Normal Value

Var myStr = "1234"; var num: Int? = Int (myStr); print ("\ (num !) "); // Get 1234 print (" \ (num) "); // get optional (1234); var myStr1 =" hell "var num1: Int? = Int (myStr1); print ("\ (num1 !) "); // An error is reported because no value can parse print (" \ (num1) "); // nil

Optional values

Var myStr = "1234"; var num: Int! = Int (myStr); print ("\ (num !) "); // 1234 print (" \ (num) "); // 1234var myStr1 =" hell "var num1: Int! = Int (myStr1); print ("\ (num1)"); // nilprint ("\ (num1 !) "); // Error
Error Handling

Use try catch statement or throw exception

Assertions

Assertions ensure that the value is required. Otherwise, an error will be reported.

Usage: when your value must be correct to ensure that the program runs, you can use assertions to ensure that the value is correct.

Let age =-3; assert (age> = 0, "age does not meet the requirements ");

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.