1 代码风格很像javascript
2 类的声明定义很像 java
例如
var myVariable = 32;//弱类型,变量
let myConstant = 23;//弱类型,常量
object and class
class Share
{
var numberofSize =1
func doSome() -> String{//字符串也跟java的一样,而不再像objective-c nsstring
return "A share with \(numberofSize) sides."//也不像oc要使用@"",而像java的“”
}
}
var share = Share()
share.numberofSize = 8
var shareDescription = share.doSome();//太像java了
在子类中的方法要覆盖父类中的方法要使用override
such as
class Square:NamedShape{
override func dosome () ->String {//太像java
return "A squre with sides of length \(sideLength)";
}
}
//如果optionalSquare为nil,表达式返回nil,否则 返回Square(sideLength: 2.5, name: "optional square")
let optionalSquare: Square? = Square(sideLength: 2.5, name: "optional square")
//如果optionalSquare为nil,返回nil,否则返回optionalSquare.sideLeng
let sideLength = optionalSquare?.sideLeng
swift 支持结构体,结构体跟类相似,最大的区别时,结构体在代码中传递是整块传递,而类创建的对象传递的是引用
使用protocol 声明协议
such as
protocol TestProtocol {
func doTest() -> String{}
}//类,枚举,结构体都能够实现协议