iOS Objective-C与Swift开发过程的详细比较

前段时间,本人同时开发了两个项目,一个用的OC,一个用的Swift。在使用中对两种语言进行一次梳理与比较。

基础文件

OC
在这里插入图片描述
Swift
在这里插入图片描述

OC程序里,一个类会有两个文件,.h和.m。.h可以写属性、方法声明等,.m可以写方法的具体实现。
Swift的类只有一个文件,就是.swift方法声明和实现是一起的

AppDelegate

OC

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
	self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.rootViewController = [[JCTabBarController alloc]init];
    [self.window makeKeyAndVisible];
。
。
。
return YES;
}

Swift

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
		window = UIWindow.init(frame: UIScreen.main.bounds)
        window?.rootViewController = JCTabBarController()
        window?.makeKeyAndVisible()
        。
        。
        。
    }

TabBarController

OC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    UITabBar *tabbar = [UITabBar appearance];
    tabbar.tintColor = kTextColor;
    [tabbar setBackgroundColor:kRGBColor(210, 218, 218)];
    tabbar.translucent = NO;
    [self addChildViewControllers];
    [JCTool getInstance].tabbatController = self;
}

- (void)addChildViewControllers
{
    [self addChildViewController:[HomeViewController new] title:@"首页" imageName:@"TabBar_home_23x23_" selectImageName:@"TabBar_home_23x23_selected"];
    [self addChildViewController:[LotteryViewController new] title:@"幸运" imageName:@"TabBar_win_23x23_" selectImageName:@"TabBar_win_23x23_selected"];
    [self addChildViewController:[MoneyViewController new] title:@"资金明细" imageName:@"TabBar_money_23x23_" selectImageName:@"TabBar_money_23x23_selected"];
    [self addChildViewController:[MyViewController new] title:@"我的" imageName:@"TabBar_my_23x23_" selectImageName:@"TabBar_my_23x23_selected"];
}

- (void)addChildViewController:(UIViewController *)childController title:(NSString *)title imageName:(NSString *)imageString selectImageName:(NSString *)selectImageName{
    childController.tabBarItem.image = [UIImage imageNamed:imageString];
    childController.tabBarItem.selectedImage = [[UIImage imageNamed:selectImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childController.title = title;
    JCNavigationController *nav = [[JCNavigationController alloc]init];
    nav.title = title;
    [nav addChildViewController:childController];
    [self addChildViewController:nav];
}

Swift

	override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self
        let tabbar = UITabBar.appearance()
        tabbar.tintColor = kMainColor
        tabbar.isTranslucent = true;
        addChildViewControllers()
    }

    func addChildViewControllers() -> Void {
        addChildViewController(childController: HomeViewController(), title: "首页", imageName: "home", selectImageName: "home_select")
        addChildViewController(childController: MoneyViewController(), title: "充值", imageName: "money", selectImageName: "money_select")
        addChildViewController(childController: NotifactionViewController(), title: "动态", imageName: "notification", selectImageName: "notification_select")
        addChildViewController(childController: MyViewController(), title: "我的", imageName: "my", selectImageName: "my_select")
    }
    
    func addChildViewController(childController:UIViewController,title:String,imageName:String,selectImageName:String) -> Void {
        childController.tabBarItem.image = UIImage.init(named: imageName)
        childController.tabBarItem.selectedImage = UIImage.init(named: selectImageName)
        childController.title = title;
        let navC = JCNavigationController.init(rootViewController: childController)
        navC.title = title;
        addChildViewController(navC)
    }

TableView代理方法

OC

@interface TableViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@end

@implementation TableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ClongCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ClongCell" forIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    ClongModel *model = self.modelArray[indexPath.row];
    cell.model = model;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"点击了");
}
@end

Swift

import UIKit

class TableViewController: UIViewController {
    var modelArray: [HomeModel]
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

//代理方法
extension TableViewController:UITableViewDataSource,UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return modelArray.count;
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let message = self.modelArray[indexPath.row];
        let cell = tableView.dequeueReusableCell(withIdentifier: "message", for: indexPath)
        cell.textLabel?.text = message.title
        cell.detailTextLabel?.text = message.content
        return cell
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("点击了\(indexPath.row)")
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值