Detailed comparison between IOS Objective-C and Swift development process

Some time ago, I developed two projects at the same time, one for OC and one for Swift. In the use of the two languages for a comb and comparison.

Basic document

OC

Swift

In the OC program, a class will have two files,. H and. m. . h can write properties, method declarations, and. m can write the specific implementation of methods.
swift's class has only one file, that is, the. swift method declaration and implementation are together

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:@"home page" imageName:@"TabBar_home_23x23_" selectImageName:@"TabBar_home_23x23_selected"];
    [self addChildViewController:[LotteryViewController new] title:@"lucky" imageName:@"TabBar_win_23x23_" selectImageName:@"TabBar_win_23x23_selected"];
    [self addChildViewController:[MoneyViewController new] title:@"Capital details" imageName:@"TabBar_money_23x23_" selectImageName:@"TabBar_money_23x23_selected"];
    [self addChildViewController:[MyViewController new] title:@"My" 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: "home page", imageName: "home", selectImageName: "home_select")
        addChildViewController(childController: MoneyViewController(), title: "Recharge", imageName: "money", selectImageName: "money_select")
        addChildViewController(childController: NotifactionViewController(), title: "dynamic", imageName: "notification", selectImageName: "notification_select")
        addChildViewController(childController: MyViewController(), title: "My", 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 proxy method

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(@"Click.");
}
@end

Swift

import UIKit

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

//Agent method
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("Click.\(indexPath.row)")
    }
}

Tags: Swift

Posted on Thu, 26 Dec 2019 13:46:47 -0500 by telvitajoel