01-swift Basic Syntax

Source: Internet
Author: User
Tags uikit

First, Swift basic operation

    1. Import Frame

      OC is #import <UIKit/UIKit.h> Swift is import UIKit

    2. Swift definition Identifiers: Constants or variables must be specified

VAR (variable)/let (constant) identifier name: type of identifier = initialization value

var a: Int = ten;   a = ; B = 3.11 wrong wording (type not correct)

let B: Double = 3.14;


3. The statement can not follow after the end;

Premise: A row has only one statement, if there are multiple statements in a row, you need to use; split


4. Print Content

Print (a)

Print ("Hello World")

Second, the use of constants and variables note points:

1. In the development of the first use of constants, only when the need to modify, when modified to Var

2. Constant essence: The saved memory address can not be modified, but can get the object through the memory address, and then modify the properties inside the object

Let View:uiview = UIView ()

view = UIView () error notation

View.backgroundcolor = Uicolor.redcolor ()

3. Create an Object

1. Create UIView object and make frame

Let view: UIView = UIView(frame: cgrect(x: 0, y: 0, Width: , Height:) )


2. Set properties of the UIView

View . BackgroundColor = uicolor. Bluecolor ()


//3. To add child controls inside a view object

3.1. Creating child controls

Enumeration used in //Swift : 1. type of enumeration . Specific type 2: Type of specific

let btn: UIButton = UIButton(type:. Contactadd)


3.2. setting The location of the BTN

btn. Center = cgpoint(x: y:-)


3.3. adds a child control to the in View

View . Addsubview(btn)

Third, the data type

// Type deduction

var a:int = 30

var a =


// You can view the type of an identifier by : Option + left mouse button

Let B = 3.14


01-swift Basic Syntax

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.