In fact, the example of swift and the class method of the distinction is very simple, like to see the source code, certainly a glance to understand. A class method is defined by adding a class modifier to the instance method. Just attach an example code.
In Viewcontroller.swift
viewcontroller.swift// class methods and instance methods are defined//// Created by Mac on 16/2/6.// copyright©2016 year ZY. All rights Reserved.//import Uikitclass Viewcontroller:uiviewcontroller { var _view1:newview!; @IBOutlet weak var btn:uibutton! Override Func Viewdidload () { super.viewdidload () _view1 = Newview (Frame:cgrectmake (100,50,100,100)); _view1.backgroundcolor = Uicolor.orangecolor (); Self.view.addSubview (_view1); Btn.addtarget (Self, Action: "Btnaction:", forControlEvents:UIControlEvents.TouchUpInside); } Func btnaction (Btn:uibutton) { //Call class method Newview.addtouch (); Call instance method _view1.movetouch (); } }
In Newview.swift
newview.swift// class methods and instance methods are defined//// Created by Mac on 16/2/6.// copyright©2016 year ZY. All rights Reserved.//import Uikitclass Newview:uiview { override init (frame:cgrect) { Super.init (frame:frame ); } Required init? (Coder Adecoder:nscoder) { FatalError ("Init (coder:) have not been implemented") } //Definition of class method class func Addtouch () { print ("+++++++"); } Definition of the instance method Func Movetouch () { print ("------");} }
Write format for instance methods and class methods in Swift