Swift vs Objective-C
iOS Application Project
We can write both Swift and Objective-C at the same project.
Certainly, we have to decide base project language.(Objective-C, Swift)
But, we can call each other to use Bridge Header, or Swift Header
Example(Combined)
Objective-C base AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. // Objective-C //self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; //self.window.rootViewController = [[ObjcViewController alloc] init]; //[self.window makeKeyAndVisible]; // Swift self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.rootViewController = [[SwiftViewController alloc] init]; [self.window makeKeyAndVisible]; return YES; }
Objective-C ViewController Example
Header
#import <UIKit/UIKit.h> @interface ObjcViewController : UIViewController @end
Code
#import "ObjcViewController.h" @interface ObjcViewController () @property (nonatomic) UIImageView *imageView; @property (nonatomic) UILabel *titleLabel; @end @implementation ObjcViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SteveJobs"]]; self.titleLabel = [[UILabel alloc] init]; self.titleLabel.text = @"Stay hungry, stay foolish!"; self.titleLabel.textAlignment = NSTextAlignmentCenter; [self.view addSubview:self.imageView]; [self.view addSubview:self.titleLabel]; } - (void)viewWillLayoutSubviews { CGRect frame = self.view.frame; self.imageView.frame = CGRectMake((frame.size.width - 200.0) / 2.0, 100, 200, 100); self.titleLabel.frame = CGRectMake(0, CGRectGetMaxY(self.imageView.frame), frame.size.width, 40); } @end
Swift ViewController Example
import Foundation import UIKit @objc class SwiftViewController : UIViewController { private var imageView : UIImageView? private var titleLabel : UILabel? override func viewDidLoad() { self.view.backgroundColor = UIColor.whiteColor() self.imageView = UIImageView(image: UIImage(named: "SteveJobs")) self.titleLabel = UILabel() self.titleLabel?.text = "Stay swift, stay foolish!" self.titleLabel?.textAlignment = .Center self.view.addSubview(self.imageView!) self.view.addSubview(self.titleLabel!) } override func viewWillLayoutSubviews() { let frame : CGRect = self.view.frame self.imageView?.frame = CGRectMake((frame.width - 200) / 2.0, 100, 200, 100) self.titleLabel?.frame = CGRectMake(0, CGRectGetMaxY((self.imageView?.frame)!), frame.width, 40) } }
Swift is not need to create header file
Swift
- Swift still uses ARC(Automatic Reference
- Swift is faster(Run level, Compiler level) than swift
- we can call each other, Swift can call Objective-C class, Objective-C can call Swift class(Objective-C support class)
- UIKit(iOS UI base class) is still Objective-C
Swift Compiler is optimised compare to Objective-C
This is the reason that Swift is faster.