zoukankan      html  css  js  c++  java
  • swift中实例方法和类方法的书写格式

    其实swift中的实例方法和类方法的区分很简单,喜欢看源代码的,肯定一眼就看懂了。类方法的定义就是在实例方法前面加一个class修饰即可。还是附上一篇实例代码吧。

    ViewController.swift中

    //
    //  ViewController.swift
    //  类方法和实例方法的定义
    //
    //  Created by mac on 16/2/6.
    //  Copyright © 2016年 ZY. All rights reserved.
    //
    
    import UIKit
    
    class 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){
            
            //调用类方法
            
            NewView.addTOuch();
            
            //调用实例方法
            
            _view1.moveTOuch();
            
            
        }
        
    
    }
    

    NewView.swift中

    //
    //  NewView.swift
    //  类方法和实例方法的定义
    //
    //  Created by mac on 16/2/6.
    //  Copyright © 2016年 ZY. All rights reserved.
    //
    
    import UIKit
    
    class NewView: UIView {
    
       
        override init(frame: CGRect) {
            
            super.init(frame: frame);
            
    
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        //类方法的定义
        class func addTOuch(){
            
            print("+++++++");
        }
    //实例方法的定义
        func moveTOuch(){
            
            print("------");
        }
    
    }
    
  • 相关阅读:
    python 字符串前面加u,r,b的含义
    文本检测: CTPN
    ocr 识别 github 源码
    python 中写hive 脚本
    Win10 环境安装tesseract-ocr 4.00并配置环境变量
    OCR 识别原理
    pandas set_index和reset_index的用法
    整理 pandas 常用函数
    js es6 map 与 原生对象区别
    js 暂时性死区
  • 原文地址:https://www.cnblogs.com/zxh-iOS/p/5185263.html
Copyright © 2011-2022 走看看