Swift学习(持续更新中…)

发布于 / Swift / 0 条评论

Tuple

元组打包。

#1

var vector = (1,2,3)

vector.0

vector.1

vector.2

 

#2

var vector =(day:1, unit:2 ,ver:3.0)

vector.day

vector.unit

vector.ver

 

 

Optional

var 变量名 : 类型? 

有时候一些变量名可以为空,上面这条语句默认该类型为nil

也可以写成如下语句

var 变量名 : 类型? = “xxx” 

 

字符串操作

var a : String ? =  “先生”

var b : Character? =   “刘”

 

+ 为字符串连接符  不可将a和b相加,若想相加得用字符串中的append方法 (return void)

如:  a.append(b)

另外,字符串转为字符需要用到字符串中的characters方法转为集合,然后用for循环打印

如: for index in a.characters {

print(index)

}

 

数组

http://www.runoob.com/swift/swift-arrays.html

 

初始化数组:

var someArray = [SomeType]() var a = [10,20,30] var a : [Int] = [10,20,30]

初始化定长数组

var someArray = [SomeType](repeating: InitialValue, count: NumbeOfElements)

eg:(创建四个零的数组)

var a = [Int](repeating: 0, count: 4)

 

向数组中添加元素

a.append(x)  或者

a+=[x]

 

元素计数:count

是否为空:isEmpty

插入:insert

移除:remove

集合

let a:Set = [1,2,3,4]

元素计数:count

空否:isEmpty

是否包含某元素:contains

Set转数组:.sorted()

交差并补:

 

字典

var someDict =  [KeyType: ValueType]()
var someDict = [Int: String]()

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]
import Cocoa

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]

var someVar = someDict[1]

print( "key = 1 的值为 \(someVar)" )
print( "key = 2 的值为 \(someDict[2])" )
print( "key = 3 的值为 \(someDict[3])" )

Switch-Case

switch <#value#> {

case <#pattern#>:

    <#code#>

default:

    <#code#>

}

 

函数

func funcname(形参) -> returntype
{
   Statement1
   Statement2
   ……
   Statement N
   return parameters
} eg:
import Cocoa

func runoob(site: String) -> String {
    return (site)
}
print(runoob(site: "www.runoob.com")) 元组作为返回值

func minMax(array: [Int]) -> (min: Int, max: Int) {

    var currentMin = array[0]

    var currentMax = array[0]

    for value in array[1..<array.count] {

        if value < currentMin {

            currentMin = value

        } else if value > currentMax {

            currentMax = value

        }

    }

    return (currentMin, currentMax)

}

let bounds = minMax(array: [8, -6, 2, 109, 3, 71])

print("最小值为 \(bounds.min) ,最大值为 \(bounds.max)")

 

枚举

enum enumname {
   // 枚举定义放在这里
}

 

// 定义枚举

enum DaysofaWeek {

    case Sunday

    case Monday

    case TUESDAY

    case WEDNESDAY

    case THURSDAY

    case FRIDAY

    case Saturday

}

var weekDay = DaysofaWeek.THURSDAY

weekDay = .THURSDAY

switch weekDay

{

case .Sunday:

    print("星期天")

case .Monday:

    print("星期一")

case .TUESDAY:

    print("星期二")

case .WEDNESDAY:

    print("星期三")

case .THURSDAY:

    print("星期四")

case .FRIDAY:

    print("星期五")

case .Saturday:

    print("星期六")

}

 

结构体和类

定义结构体和类
class className {

}

struct structName {

}

 

struct Resolution {

    var width = 0

    var height = 0

    

    func resolutionFun() {

        print(“method of struct Resolution”)

    }

}

class VideoMode {

    var resolution = Resolution()

    var interlaced = false

    var frameRate = 0.0

    var name: String?

    

    func VideoMode() {

        print("method of class VideoMode")

    }

}

 

struct Resolution {
    var width = 0
    var height = 0
}

let resolution = Resolution(width:100,height:120)
print("width:\(resolution.width),height:\(resolution.height)")
结果:
width:100,height:120

而类并不具备这样的初始化器,如果需要特定的初始化需要去自定义,那么如何创建类似这样的初始化器呢?
首先要知道初始化器是系统本身的方法,我们只能去重写或者重载它

class VideoMode {
    var _name: String
    
    init(){
        _name = "Jin"
    }
}

let videoModeNormal = VideoMode()
print("name:\(videoModeNormal._name)")
结果:
name:Jin

我们重写了系统默认的init()初始化方法,并且设定我们想定义的功能。

转载原创文章请注明,转载自: 静沐暖阳 » Swift学习(持续更新中…)
Not Comment Found