Swift learns a

Source: Internet
Author: User

http://blog.csdn.net/kuloveyouwei/article/details/30511303

At the end of this WWDC global developer conference, Apple has unveiled a new swift programming language and a new version of Xcode for developers, which Apple says is "fast, modern, secure, interactive", and is better than the Objective-c language. "For the past 20 years, we have only supported objective-c. "The next Apple developer platform will support the new Swift programming language," Cregg Federich, vice president of software engineering, said on stage.

Interested can go to official download Swift Official document, below we first look at the basic data type,

1. Definitions of constants and variables

In Swift, we use let to define a constant, using var to define a variable, for example:

[OBJC]View Plaincopy
    1. Let a=10;
    2. var b=12;

No, we don't need to specify a type for it, and the compiler determines the type of the variable based on the value of the variable; Of course, if you don't set an initial value for a variable, you should declare the type of the variable, such as:

[OBJC]View Plaincopy
    1. var b:string;
    2. b="ADC";

Of course, the following semicolon can not, but I still add, so the code is more, the structure will be clear ... Almost any character in Swift can be used as a constant and variable name, including Unicode, but the name cannot contain mathematical symbols, arrows, invalid Unicode, horizontal lines-and tabs, and cannot begin with a number, although numbers can be included in the name. Once the declaration is complete, you cannot declare a variable or constant of the same name again, or change its type. For example:

[OBJC]View Plaincopy
    1. Let Prince ="Wangzi";
    2. var adult ="Daren";
    3. var 22="2222"; //contains numbers, error
    4. var _a="a";
    5. var king 2="2";

2. Output Constants and variables

Swift uses the println () function to output constants or variables, and of course the print function, println with line breaks, for example:

[OBJC]View Plaincopy
    1. println (a);
    2. Print (b);

The PRINTLN function can also format the output of some log information, just like the behavior of the NSLog function in cocoa, which can include constants and variables themselves. Swift inserts the variable name as a placeholder in the string, represented by \ (), for example:

[OBJC]View Plaincopy
    1. var b:string;
    2. b="ADC";
    3. println ("I am \ (b)");

Output: I am the ADC

3. int type and UINT type

We know that under different platforms, the int type is different in length, in OC, we can use Nsinteger to represent an int type, which can define the length of an int based on the platform on which it is located.

[OBJC]View Plaincopy
    1. #if  __lp64__ | |   (target_os_embedded && ! Target_os_iphone)  | |  target_os_win32 | |  ns_build_32_like_64  
    2. typedef  long nsinteger;  
    3. typedef unsigned  long nsuinteger;  
    4. #else   
    5. typedef int nsinteger;  
    6. typedef unsigned int  nsuinteger;  
    7. #endif   

In SWITF, we can use int to define an int type, in general, the programmer does not need to select the number of digits when writing the code, SWIFT provides an additional integer type int, which is the same integer number as the current machine environment with a length of 32 bits on a 32-bit machine, The 64-bit length is represented on a 64-bit machine. Swift also provides an unsigned type uint, as well as the word looks of the current machine environment.

[OBJC]View Plaincopy
    1. var cc:int=20;

Swift is a type-safe language. Type safety means that you need to figure out the type of variable when programming. If your code section requires a string, you cannot mistakenly pass an integer type.

Because Swift is type-safe, it will check your code at compile time, and any type mismatch will result in an error. This allows programmers to capture and remediate errors as early as possible in the development process.

Type checking can help avoid errors when using different types of values. However, this does not mean that you must specify each constant and the type declared by the variable. If you do not specify the type you want, Swift uses type deduction to specify the appropriate type. Type deduction allows the compiler to automatically derive the type of a particular expression at compile time by the initialization value you provide.

Type deduction makes swift require fewer type declaration statements than C or objective-c. Constants and variables are still explicitly typed, but most of the work that specifies their type is swift already done for you.

4. Type aliases

We often use TypeDef in other languages, such as C + +, OC, to define a new name for a data type, and to define alternative names in Swift with Typealias for existing types, for example:

[OBJC]View Plaincopy
    1. Typealias MyInt = Int;
    2. var dd:myint;

5. Tuple type

Tuples, this is not in OC, it can be a number of data types to form an element, the data type can be the same, or can be different, with (), such as (int,string,int), etc., programmers can arbitrarily create their own required tuple type, such as (int, int, int ), or (String, Bool), and so on. The number of types that make up a tuple at the same time is unlimited. For example:

[OBJC]View Plaincopy
  1. Let httpstatus= (404,"not Found");
  2. var person: (int,string) = (23,"Rose");
  3. var Person2: (int,string) = (age:23, name: "Rose"); Element name cannot be added with explicitly specified element type
  4. var person3= (age:23, Name:"Rose");
  5. //You can use the underscore _ to ignore the value of an element and remove the value of another element
  6. var person4 = (20,"Jack");
  7. var (_, name) = Person4;
  8. println (Httpstatus);
  9. println (person. 0);
  10. println (Person3, age);
  11. println (name);

6. Optional type and forced unpacking

What is an optional type, and the optional type is that the value of this type is either present, equal to a value of XX, or does not exist. For example, to find a character in a string, either the character is present, or the character does not exist.

[OBJC]View Plaincopy
    1. //format of the optional type: type name?
    2. var kindex:int?;
    3. Question mark? indicates that the value of the kindex is optional, may be an int, or the value does not exist
    4. The value of the kindex is either an int or nil (nil means that the value does not exist)
    5. Kindex default is nil, so the above statement is equivalent to
    6. var kindex:int? =Nil

[OBJC]View Plaincopy
    1. You cannot assign an optional type directly to a specific type
    2. For example, you cannot assign an int type directly to an int type, because int is not sure if there is an integer value
    3. var people:int?=10;
    4. var people2:int=people;

The above indicates an error, but in turn, you can assign the int type value to int? type, if you want to make the above expression correct, we can force the unpacking, with!

[OBJC]View Plaincopy
    1. var people:int?=10;
    2. var people2:int=people!;

This means that I am sure that the variable people does exist with a value of 10, note that if the value of the optional type does not exist, the forced unpacking will be reported an error

[OBJC]View Plaincopy
    1. var num:int?
    2. var numvalue =num! //This guild error

We can use the IF condition statement to determine whether a value of an optional type exists,

[OBJC]View Plaincopy
    1. Let nums = "". ToInt ();
    2. if (nums)
    3. {
    4. println ("num value is \ (nums!)")
    5. }Else
    6. {
    7. println ("The value of num does not exist")
    8. }

By default: If you want to assign the value of an optional type to a specific type, such as assigning an int to an int type, you need an exclamation point ! To force the unpacking , we can declare it as an implicit unpacking,

[OBJC]View Plaincopy
    1. var num:int! =20
    2. var numvalue:int =num //automatic unpacking, no need to use! To force unpacking
    3. Num is an optional type that is declared to be an implicit unpacking int!









Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Swift learns a

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.