Swift 基本概念(一)

Variables

var i : int = 0   //An integer type!
var c : char = ‘a’ // A character type!
var implicitInt = 70  // Swift can automatically create a variable type
var implicitDouble = 70.0  //This variable will be a double

Constants

let i : int = 0  //An constant integer type!
let c : char = ‘a’ // A constant character type!
let implicitInt = 70  // Swift can automatically create a constants too
let implicitDouble = 70.0  //This variable will be a double

Variables - printing values

var msg = “Hello, Swift”
print(“This is a debug message.”)
print(msg)

It is very useful when doing some debug. It is just show in the Xcode console while never show in actual application itself.

Common Operators

  • Assignment operator (=)
  • Arithmetic operators (+,_,*,/,%)
  • Compound Assignment Operators (+=, -=, *=, /=)
  • Boolean operators (==, !=, <=, >=, !, &&, ||)

Note: Swift DOES NOT Support Increment& Decrement Operators, ++ & — are not available as of Swift 3.0

Data Types - Integers

  • Declared using keyword Int
  • Integer is default size on the platform being used

    E.g. 32-bit OS = Int32, 64-bit OS = Int64

  • You can declare Integers with the following size:

    —Int8, Int16, Int32, Int64

  • You can also declare unsigned Integers:

    -UInt, UInt8, UInt16, UInt32, UInt64

Data Types - Floating Point Numbers & Boolean

  • Swift has two floating point types: Float and Double

    — Floats have 32-bit floating point number precision

    — Double have 64-bit floating point number precision

  • Booleans in Swift use the Bool keyword

    — Value can be either true or false

Data Types - Strings

var age = 10
var name = “Alice”
let message = “Hello \(name)! You are \(age) years old!”

* Swift strings can also use the + operator to concatenate other values
* Strings are now returning to functioning like a collection. This means we now have access to functions like: count, isEmpty(), reversed(), dropFirst() etc.

Data Types - Tuples

Swift also supports tuples which combine multiple values into a single value. Example:

var statusCode = (404, “Not Found”)
let (code, description) = statusCode
print(code )   //Prints 404
print(statusCode.0)   //Prints 404

print(description )   //Prints Not Found
print(statusCode.1)   //Prints Not Found

Data Types - Optionals

var i : int?   // An optional variable set to nil

* In Swift, variables cannot be nil(null)
* You must always declare the value of a variable in order for it to be created and compile.
* However, we may not want to do this (or we may want a variable to have a nil value)
* We can do this by using Optional variables
* Optional variables are now seen as being Optional(data type)
* To get the data out of an optional, we need to “Unwrap it”

Data Types - Optional Unwrapping

If we know a variable contains a value, we can “force unwrap” the variable to get the data out of it.

if number != nil
{
    print(“The number is \(number!)”)
}

We can also perform optional binding to easily determine if value is set:

if let unwrappedNumber = int(number)
{
    print(“The number is \(unwrappedNumber)”)
}
else
{
    print(“Number cannot be unwrapped”)
} 

Data Types - Arrays

Arrays in Swift can be declared in the following ways:

var array = Array<Int>()
var data = [ “Josh”, “Charlie”, “Jason”]

We can easily add and remove elements from an array by using the append and remove functions:

array.append(10)
array.removeLast()

Data Types -Dictionary

Dictionaries are a form of collection used by swift.
They have a structure of Name

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值