首页 > 代码库 > 邻家swift初长成
邻家swift初长成
import Foundation
let str = "123"
var message = "tuqiushuang , str"
message+="t"//字符追加
if str.isEmpty||200>90
{
print("hao")
}
if str==message&&100>90
{
print("hao")
}
else
{
print("hao==")
}
var a = 1
var b = 2
let m = "\(str) and \(Double(a)*0.1)"//字符串的插入
var d = a>b ? "优秀" : "良"
var arr: [String] = ["eggs","milk","dd"]//数组
for item in arr{//遍历
print(item)
}
for(index, value)in EnumerateSequence(arr)
{
print("item \(index+1):\(value)")//遍历
//元组类型下标与值
}
arr.append("jidan")
arr+=["wo","he"]
print(arr.count)
arr.insert("insert", atIndex: 2)//在某个位置插入
let aa = arr.removeAtIndex(5)//删除的那个值
let apple : Int? = 123//可选类型
var httpStatus=(s:200,d:"ok",m:90)//tuple元组类型,类似字典
httpStatus.m
/*
字典<尖括号里是:指定类型>
*/
var airporits:Dictionary <String,String>= ["K0":"V0","K1":"V1"]
airporits["k2"]="v2"//追加
airporits.count//对的个数
if let removevalue=http://www.mamicode.com/airporits.removeValueForKey("K1")
{
print(removevalue)
}else{
print("NO have")
}
for i in 0 ..< 10 {
if i%2==0{
continue;//跳转语句
}
print("i = \(i)")
}
//函数嵌套
func chooseStepFunction(backwards:Bool) ->(Int)->Int{
func stepForward(input:Int)->Int{
return input+1
}
func stepBackward(input:Int)->Int{
return input-1
}
return backwards ? stepBackward : stepForward
}
var currentValue = http://www.mamicode.com/-4
let moveNearerToZero = chooseStepFunction(currentValue>0)
while currentValue != 0 {
currentValue = http://www.mamicode.com/moveNearerToZero(currentValue)
print(currentValue)
}
//闭包
struct imageFile{//结构体 图片文件
var fileName = String()
var fileID = Int()
}
var images:[imageFile] = []
var new = imageFile(fileName: "hehe",fileID: 20)
var old = imageFile(fileName: "heihei",fileID: 30)
images.sort({ $0.fileID>$1.fileID})
images.sort({image1,image2 in image1.fileID > image2.fileID})
old.fileID
new.fileID
//计算属性
struct Point{
var x = 0.0, y = 0.0
}
struct Size {
var width = 0.0, height = 0.0
}
struct Rect {
var origin = Point()
var size = Size()
var center: Point {
get {
let centerX = origin.x + (size.width / 2)
let centerY = origin.y + (size.height / 2)
return Point(x:centerX, y:centerY )
}
set(newCenter){
origin.x = newCenter.x - (size.width / 2)
origin.y = newCenter.y - (size.height / 2)
}
}
}
var square = Rect(origin: Point(x: 0.0, y: 0.0), size: Size(width: 10.0, height: 10.0))
let initialSquareCenter = square.center
square.center = Point(x: 15.0, y: 15.0)
print("矩形边长:\(square.origin.x),\(square.origin.y)")
邻家swift初长成