swift 基本知识点

19. December 2015 Swift 0

import Cocoa

varstr = “Hello, playground”

//playground幽灵般的存在

fori in-20… 20

{

varj = i * i

vark = i * i * i

}

//定义二进制,八进制和十六进制可以在旁边实时显示十进制

varbinary:Int= 0b10

varbinary1 = 0x10

varbinary2 = 0o10

//数字可以使用-分开,

letsum = 1_000_00

//强制转换

leta:Int= Int(1.0)

//定义了float类型,虽赋予Int类型,自动转变为float

letb:Float= 1

//定义可选类型变量

varg:Int?

varss = “sdfsgdsgdf s sf ff gsa”

vary:Character= “1”

varstru:NSString= “kdjfldjf”

//判断字符串首部和尾部是否含有指定字符串

ss.hasPrefix(“s”)

ss.hasSuffix(“a”)

//字符串首字母转化大写

ss.capitalizedString

//转化成大写字母或者小写字母

ss.uppercaseString

ss.lowercaseString

ss.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())

//删除制定字符串

ss.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString:”sd”))

//以指定字符串分割

ss.componentsSeparatedByString(” “)

ss.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString:” “))

//链接字符串

varste = “-“

ste.join([“2″,”dddd”])

//从字符串末尾返回制定字符串的位置

ss.rangeOfString(“sf”, options: NSStringCompareOptions.BackwardsSearch)

//确定字符串的查找范围

letstrartIndex = ss.startIndex

letendIndex = advance(ss.startIndex,10)

letyyy = Range<</font>String.Index> (start:strartIndex,end:endIndex)

//字符串截取插入删除等基本操作

vartoIndex = advance(ss.startIndex,10)

ss.substringToIndex(toIndex)

varToIndex = advance(ss.startIndex,7)

ss.substringFromIndex(ToIndex)

ss.removeAtIndex(ToIndex)

ss.insert(“l”, atIndex: ToIndex)

//定义数组

varaaa:Array<</font>String> = []

aaa.append(“ddddd”)

letsss:[String] = [“gdfgdf”]

letyyooooy: [String] = [“dsfdskfk”]

println(“ddddfdsgdg”)

//定义空数组

variiiii = [String]()

iiiii= [“3423″,”dsfsdf”]

iiiii.insert(“lijian”, atIndex: 0)

iiiii.removeAtIndex(0)

//———————————-

letuuu = Range(start:0, end: 1)

iiiii.removeRange(uuu)

varlijian:[String] = [“s4444:dfds”,”dfd:dddddf”]

lijian[0]

//遍历数组

forhuu inlijian{

println(huu)

}

for(vv,bb) inenumerate(lijian){//打印数组索引

println(“\(bb)”)

}

//————————————–

vardictionnary = []

//字典存储的是键和值的数据对

//所存储的数据中,键和值可以是任意的数据类型

//一个字典职能存储固定的一种键合值的数据类型搭配


Leave a Reply

Your email address will not be published. Required fields are marked *