分享playground
import UIKit
import Foundation
var va1 = "test...."
let va2 = "test...fsd"
//va1 = "fsdfesfd"
//va2 = "fsesdf"
//申明字典
var dic1 = [String: NSObject]()
var tuple1: (Int, String) = (404, "page not found")
var tuple2 = (first: 3, second: 3, result: "night")
tuple1.0
tuple2.first
//类,结构体,枚举
class SomeClass {
// class definition goes here
var width: Int = 0
var height: Int = 0
class func hello2(){
print("someClass")
}
}
SomeClass.hello2()
//var someClass = SomeClass()
struct SomeStructure {
// structure definition goes here
var width: Int
var height: Int
func hell0(){
print("hello word!")
}
static func hello2(){
print("what is this")
}
}
//var someStruct = SomeStructure(width: <#T##Int#>, height: <#T##Int#>)
enum CompassPoint: Int {
case North
case South
case East
case West
}
var east: CompassPoint = .East
east.rawValue
enum Result<Value> {
case Success(Value)
case Failure(ErrorType)
}
func getDataFromNet() {
// (value: T?, error: ErrorType?) -> Void
// if let error = error {
// // handle error
// } else if let value = value {
// // handle value
// } else {
// // all nil?
// }
// balaba <get data asnyn from net
let result: Result = Result.Success("some data get from net")
switch result {
case .Success(let value):
print(value)
case .Failure(let err):
print(err)
}
}
//optional
var someValue: Int = 4
var someValue2: Int? = 5
var someValue3: Optional<Int> = 6
var someValue4: Int?? = 7
print(someValue)
print(someValue2)
print(someValue3)
print(someValue4)
print(someValue3!)
print(someValue + someValue4!!)
var someArray: [Int]?
someArray = [1, 2, 3]
someArray.map({ "No.\($0)"})
//控制语句
let flag = true
if flag {
print("hello swift!")
} else {
print("don't borther me!")
}
switch tuple2 {
case (_, _, let result):
print(result)
case (1, 2, let result):
print("haha")
}
var i = 10
repeat {
i++
print("hello swift")
} while(i < 100)
while i < 120{
print(11)
i++
}
//函数,闭包
func sum(a:Int,b:Int)->Int{
return a + b
}
sum(2, b: 3)
//curried func
func sum(a:Int)->Int->Int{
return { b in
return a + b
}
}
var addThree = sum(3)
addThree(2)
sum(3)(3)
//协议扩展
extension Int {
func cube() -> Int {
return self * self * self
}
}
2.cube()
extension UIView {
func removeAllSubview() -> Void {
for view in self.subviews {
view.removeFromSuperview()
}
}
}
let uiview = UIView.init()
uiview.removeAllSubview()
//guard 特性
func readCampusConfig() {
// 金字塔
// if let value = NSUserDefaults.init(suiteName: "campus") {
// if let uin = value.stringForKey("uin") {
// print(uin)
//// .....
// }
// }
guard let value = NSUserDefaults.init(suiteName: "campus") else {
return
}
guard let uin = value.stringForKey("uin") else {
return
}
print(uin)
// .....
}
//闭包
var names = [4, 3, 5, 7, 1, 12, 8]
var name2 = names.flatMap { (int) -> String? in
return "\(int)"
}
//name2
names.sort({ (a: Int, b: Int) -> Bool in
return a < b
})
names.sort({ a, b in
return a < b
})
names.sort({
$0 < $1
})
names.sort( <)
names.sort( >)
// 函数式编程
var someVar:Int? = 3
someVar.map { some -> Int in
return some + 2
}
网友评论