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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fenghuangjc/article/details/82817832

前段时间,本人同时开发了两个项目,一个用的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)")
    }
}

阅读更多
换一批

没有更多推荐了,返回首页