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

栏目: Objective-C · 发布时间: 5年前

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

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

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

基础文件

OC

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

Swift

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

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

Swift的类只有一个文件,就是.swift方法声明和实现是一起的

AppDelegate

OC

<code>- (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;
}
</code>

Swift

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

TabBarController

OC

<code>- (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];
}
</code>

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

<code>@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
</code>

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)")
    }
}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

CSS精粹

CSS精粹

Rachel Andrew / 曹明伦 / 人民邮电出版社 / 2007-10 / 39.00元

本书采用问答的形式,为CSS使用过程中一些有价值的经典问题提供了精彩的实践解决方案。本书内容包括文本样式、CSS图像、导航、表格数据、注册表和用户界面、浏览器和设置支持、CSS定位和布局以及未来相关技术。 本书的目标读者是每一个需要使作CSS的Web设计人员和开发人员。本书通过经典的问题和精彩的解答将理论融于实践,使每一个带着问题阅读本书的读者都能找到自己满意的答案。一起来看看 《CSS精粹》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

SHA 加密
SHA 加密

SHA 加密工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具