Swift extension详解
发布时间:2020-12-14 06:14:24 所属栏目:百科 来源:网络整理
导读:OC_category和Swift extension 在 Objective-C 中,我们有 .h 文件和 .m 文件。同时管理这两个文件(以及在工程中有双倍的文件)是一件很麻烦的事情,好在我们只要快速浏览 .h 文件就可以知道这个类对外暴露的 API,而内部的信息则被保存在 .m 文件中。在 Sw
OC_category和Swift extension 在 Objective-C 中,我们有 .h 文件和 .m 文件。同时管理这两个文件(以及在工程中有双倍的文件)是一件很麻烦的事情,好在我们只要快速浏览 .h 文件就可以知道这个类对外暴露的 API,而内部的信息则被保存在 .m 文件中。在 Swift 中,我们只有一个文件。 //可以扩展类型时,让类型遵守协议
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)");
}
} (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- react native错误排查-TypeError: window.deltaUrlToBlobUr
- oracle – 具有多个数据源的Tomcat 6/7 JNDI
- actionscript-3 – 在动画大量铅笔工具绘制的图形时,如何保
- ruby-on-rails – Rails / ActiveRecord:检测列是否是一个
- 持续集成工具之Jenkins pipline简单示例
- Ajax 关键点(get请求---post请求)
- ajax的基本实现和jquery实现
- CCTexture2D(纹理图片)和 CCTextureCache(纹理缓存)
- 模型选择之诊断偏离bias和方差variance的区别
- ruby-on-rails – 为什么Pry不会在Heroku的控制台中运行?