swift基础

ios开发 myhloli 2485次浏览 已收录 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)
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(14)个小伙伴在吐槽
  1. dict["sex"] = "Female"
    win7专家322015-03-11 20:08 回复
  2. 这个.....看不懂啊,呵呵。
  3. true
    myhloli2015-03-12 00:48 回复
  4. true
    myhloli2015-03-12 00:48 回复
  5. 都在研究swift呢
    LiuJianTao2015-03-14 23:40 回复
  6. 哇塞,博主你的代码高亮做的好帅啊,请问你用的是什么方案?
    雁过留名2016-04-07 21:24 回复
    • http://googlo.me/archives/3700.html
      myhloli2016-04-08 10:19 回复
      • 噢~我看到你分享的内容了,可是我用的是Hexo搭建的静态博客....
        雁过留名2016-04-08 10:53 回复
        • 静态博客那没办法了。。
          myhloli2016-04-08 11:31 回复
          • 还是谢谢楼主哈,我再试试其他办法。祝好~
            雁过留名2016-04-08 12:41
加载中……