Swift extension详解

OC_category和Swift extension

在 Objective-C 中,我们有 .h 文件和 .m 文件。同时管理这两个文件(以及在工程中有双倍的文件)是一件很麻烦的事情,好在我们只要快速浏览 .h 文件就可以知道这个类对外暴露的 API,而内部的信息则被保存在 .m 文件中。在 Swift 中,我们只有一个文件。
为了一眼就看出一个 Swift 类的公开方法(可以被外部访问的方法),我们可以把内部实现都写在一个私有的 extension 中。

//可以扩展类型时,让类型遵守协议
extension ViewController: UITableViewDelegate{
    internal func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("the index is \(indexPath.row)");
    }
}

private extension ViewController{
    //不对外暴露的函数和属性
}
//
//  ViewController.m
//  test_extension_01
//
//  Created by jeffasd on 17/6/20.
//  Copyright © 2017年 jeffasd. All rights reserved.
//

#import "ViewController.h"

static NSString *const kCellIdentifier = @"kCellIdentifier";

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view addSubview:self.tableView];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    ViewController *mainVC = [ViewController new];

    [self.navigationController pushViewController:mainVC animated:YES];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier forIndexPath:indexPath];

    cell.textLabel.text = @"hello world";

    return cell;

}

- (UITableView *)tableView{
    if (_tableView == nil) {
        CGRect frame = (CGRect){0, 128, self.view.frame.size.width, self.view.frame.size.height - 64};
        _tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kCellIdentifier];
    }
    return _tableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

@interface UIViewController (Jeffasd)

@end

@implementation UIViewController (Jeffasd)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    printf("tableview");
}

@end

Swift实现

//
//  ViewController.swift
//  test_swift_tableView_02
//
//  Created by jeffasd on 17/6/20.
//  Copyright © 2017年 jeffasd. All rights reserved.
//

let kCellIdentifier = "cellIdentifier"


import UIKit

class ViewController: UIViewController, UITableViewDataSource {

    //fileprivate weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()


        view.addSubview(self.tableView)

    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        //let mainVC: MainViewController = MainViewController()

        let mainVC: ViewController = ViewController()

        navigationController?.pushViewController(mainVC, animated: true)
    }

    fileprivate lazy var tableView: UITableView = {

        let frame: CGRect = CGRect(x: 0, y: 128, width: self.view.frame.width, height: self.view.frame.height - 64)

        let tableView: UITableView = UITableView(frame: frame, style: UITableViewStyle.plain)

        tableView.delegate = self
        tableView.dataSource = self

        tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: kCellIdentifier)

        return tableView
    }()

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: kCellIdentifier, for: indexPath)
        cell.textLabel?.text = "hello world"
        return cell
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

extension ViewController: UITableViewDelegate{
    internal func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("the index is \(indexPath.row)");
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值