Write format for instance methods and class methods in Swift

Source: Internet
Author: User
Tags instance method

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.