原创blog,转载请注明出处
http://blog.csdn.net/hello_hwc
之所以把这两个放在一起,是因为在swfit中,类和结构体的关系非常紧密,甚至很多时候很难区分是用类实现还是用结构体实现好。
在其他语言中,类对应对象,而在swfit中,常常称作类对应实例,结构体也对应实例。class HwcClass{ var classVariable = 0 func print(){ println("From class") } } struct HwcStruct{ var structVariable = 1 func print(){ println("From struct") } }
var someHwcClassInstance = HwcClass() var someHwcStructInstance = HwcStruct()用点(.)来访问类或者结构体的属性和方法
var classVariable = someHwcClassInstance.classVariable var structVariable = someHwcStructInstance.structVariable someHwcClassInstance.print() someHwcStructInstance.print()这里要提到的是,与Objective-C不同,swfit允许直接修改结构体的某一个子属性,另外array
class csdnID{ var name:String = “jack” let ID:String = "123456" init(name:String,ID:String){ self.name = name; self.ID = ID } } var myID = csdnID(name:"HelloHwc",ID:"12345568") myID.name = "CallMeHwc" //myID.ID = "1234556" wrong因为,用户名可以修改,但是系统赋予给用户的识别ID不能修改,对于不想修改的,我们只需要在生命中声明为let即可,注意,及时是let,可以在构造的时候进行赋值
class CSDN{ lazy lazycoder:NSMutableArray = NSMutableArray(array:["jack","lucy","tom"]) //Something else }当我们想要使用这个lazy变量的时候直接使用,因为我们不是每一次都要知道那些人lazy,所以不需要每次在构造的时候都初始化这个变量。
var instance = CSDN() var lazyCoders = instance.lazycoder
class Line{ let startPoint:Int = 0 var endPoint:Int = 15 var centerPoint:Int{ get{ return startPoint+endPoint/2 } set(newCenter){ endPoint = 2*newCenter - startPoint } } }
class Line{ let startPoint:Int = 0 var endPoint:Int = 15 var centerPoint:Int{ get{ return startPoint+endPoint/2 } set{ endPoint = 2*newValue - startPoint } } } var line1 = Line() println(line1.endPoint) println(line1.centerPoint) line1.centerPoint = 10 println(line1.endPoint)
class Line{ let startPoint:Int = 0 var endPoint:Int = 15 var centerPoint:Int{ get{ return startPoint+endPoint/2 } } } var line1 = Line() line1.centerPoint = 10//Wrong
class Test{ var testVar:String = "oldvalue"{ willSet{ println("Value to be set is \(newValue)") } didSet{ println("Old value is \(oldValue)") } } } var instance = Test() instance.testVar = "newvalue"可以看到输出是
Value to be set is newvalue Old value is oldValue
struct Point{ var x:Int = 0 var y:Int = 0 static var description:String = "This is a struct about Point" } println(Point.description)
struct PersonInfo{ var name:String; var id:String; func showInformation(){ println("\(name)\n\(id)") } } class MyCSDN{ var info:PersonInfo var visitTimes:Int init(info:PersonInfo,visitTimes:Int){ self.info = info //用self来区分 self.visitTimes = visitTimes } func showCSDNInfo(){ info.showInformation() println(visitTimes) } } var onePerson = PersonInfo(name:"hwc",id:"7865123") var myCsdn = MyCSDN(info:onePerson,visitTimes:6800) myCsdn.showCSDNInfo()可以看到输出
class CSDN{ func testWithFirst(first:Int,second:Int){ println(first+second) } } var instance = CSDN() instance.testWithFirst(1,second:2)当第一个参数不提供外部参数的时候,通常会在函数名中用with,by等表明第一个参数是干嘛的
class CSDN{ func testWith(#first:Int,second:Int){ println(first+second) } } var instance = CSDN() instance.testWithFirst(first:1,second:2)3、mutating关键字
struct Line{ var startPoint:Int = 0 var endPoint:Int = 15 mutating func changeEndPoint(newValue:Int){ endPoint = newValue } } var line = Line() line.changeEndPoint(20) println(line.endPoint)4 类型方法
举例
private var name:String = "Hello_hwc" class CSDN{ class func print(){ println(name) } } CSDN.print()
ADD:由于swift语言版本更新的问题,暂时不支持在class中声明实例变量了