Swift's first day of learning: recognize the constants and variables of swift and Swift

Source: Internet
Author: User
Tags uikit



One: Understanding Swift

// 1. import framework

// # import <UIKit / UIKit.h>
import UIKit


// 2. Define an identifier
// int a = 10;
// define identifier in swift: must specify whether the identifier is a constant or a variable
// var (variable) / let (constant) identifier name: type of identifier = initialization value
var a: Int = 10;
let b: Double = 3.14;
a = 29;
// b = 3.11 incorrect writing


// 3. You can skip following the statement;
// Premise: only one statement per line, if there are multiple statements in one line, you need to use; split
var m: Int = 20
let n: Double = 3.44


// 4.Print content
// NSLog (@ "% d", a)
print (a)
print ("hello world")


/ **
 
 Summary: 1: import framework: oc import framework: #import <UIKit / UIKit.h> swift import framework: import UIKit
      2: define identifier: define identifier in swift: you must first specify whether the identifier is a constant or a variable: common format: var (variable) / let (constant) identifier name: type of identifier = initialization value
      3: You don't need to follow after the statement is finished; Premise: There is only one statement in a line, you can not add a semicolon, if there are multiple statements in a line, you need to use; split
      4: Print content: print function, basic data type: print (a) print string without @, print ("hello world")

 * /
What is Playground?
Appears from Xcode6 (Swift starts to appear)
Translate to: Playground / Playground
Very convenient for learning basic Swift syntax
What You See Is What You Get (Quick Look at Results)
You can quickly see when the grammatical features have changed.
The most basic syntax changes in Swift
Import framework
When defining an identifier, you must declare whether the identifier is a variable or a constant
Declaration Identifier Format: Variable / Constant Key Name: Data Type
No need to add at the end of the statement;
If there are multiple statements on the same line, you still need to add
But it is not recommended to have more than one statement per line
Print statement in Swift: print (printed content)
Two: constants and variables in swift

1: what are constants and variables

In Swift: When defining an identifier, you must clearly indicate whether the identifier is a constant or a variable
Use let to define constants, which cannot be modified after definition
Use var to define variables. After definition, you can modify 2:
2: Basic use of constants and variables
import UIKit

let a: Int = 10
// Wrong expression, cannot be modified when a field is defined as a constant
// a = 20

var b: Int = 20
// Because b is defined as a variable, it can be modified
b = 30


3: Use of constants and variables Note:
note:
In the real use process, it is recommended to define constants first, and then modify them into variables if necessary (more secure)
The pointed object cannot be modified any more (that is, the memory address cannot be modified). But after the object can be obtained through the pointer, the internal properties of the object can be modified
// Note: declared as a constant cannot be modified means that the pointer can no longer point to other objects. But you can get the object through the pointer and modify the properties
// view: UIView = [[UIView alloc] init];
// Not required in Swift objects *
var view: UIView = UIView ()
view = UIView ()

let view1: UIView = UIView (frame: CGRect (x: 0, y: 0, width: 100, height: 100))
view1.backgroundColor = UIColor.redColor ()

// Use of enum types: type.value of enum
let btn: UIButton = UIButton (type: UIButtonType.Custom)
btn.backgroundColor = UIColor.blueColor ()
btn.setTitle ("Button", forState: UIControlState.Normal)
btn.frame = CGRect (x: 20, y: 20, width: 60, height: 30)
view1.addSubview (btn)
 

 

 

import UIKit

// 1. Constants are used preferentially in development, only when they need to be modified, they are modified to var

// 2. The nature of the constant: the saved memory address cannot be modified, but you can get the object through the memory address and then modify the internal properties of the object

let view: UIView = UIView ()
// view = UIView () incorrect writing
view.backgroundColor = UIColor.red
 

Among them UIView (), represents the creation of a UIView object, the memory address of the object is stored in the view pointer, let modification, that is, the memory address or the object pointed to by the pointer cannot be modified, but the object can be set through the pointer to set the object Internal attributes

 

 

The first day of learning in swift: understanding swift and its constants and variables

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.