swift基础

ios开发 myhloli 1953次浏览 已收录 14个评论

swift-716x297-v2

字符串连接

 

import Foundation

var str = "hello"
var s:String = "world"
var i:Int = 1000
str = str + " \(s),myhloli,\(i),\(8888)"


println(str)

hello world,myhloli,1000,8888

字符数组

import Foundation

var arr = ["hello","iloveloli",100,2.3]

println(arr)

(
hello,
iloveloli,
100,
“2.3”
)

字典

import Foundation

var dict = ["name":"myhloli","age":"17"]

dict["sex"] = "Female"

println(dict)

println(dict["name"])

[sex: Female, name: myhloli, age: 17]
myhloli

循环

import Foundation

var arr = String[]()

for index in 0..100{
arr.append("Item \(index)")
}
println(arr)

for value in arr{
println(value)
}
var i = 0
while i<arr.count {
println(arr[i])
i++
}
var dict = ["name":"myhloli","age":"17"]

for (key,value) in dict{
println("\(key),\(value)")
}

name,myhloli
age,17

流程控制

import Foundation

for index in 0..100{
if index%2==0 {
println(index)
}
}
var myName:String?="myhloli"

if let name=myName {
println("hello \(name)")
}

hello myhloli

函数

import Foundation

func sayHello(name:String){
    println("Hello \(name)")
}


sayHello("myhloli")


func getNums()->(Int,Int){
    return(2,3)
}

let (a,b) = getNums()
println(a)

var fun = sayHello
fun("zhangsan")

Hello myhloli
2
Hello zhangsan

面向对象

import Foundation

class Hi{
func sayHi(){
println("hi myhloli")

}
}

class Hello:Hi{

var _name:String

init(name:String) {
self._name = name
}

override func sayHi(){
println("Hello \(self._name)")
}

}

var hi = Hi()
hi.sayHi()

var h = Hello(name: "ZhangSan")
h.sayHi()

 

hi myhloli
Hello ZhangSan

  萝莉社,版权所有丨如未注明,均为原创丨本网站采用BY-NC-SA协议进行授权,转载请注明转自:https://myhloli.com/swift-basic.html
喜欢 (1)
加载中……