Hi All,
Started learning Swift 3.0. Need transformed to LOVE :)
Gathered some data that I would like to share with all.
So below is Objective C Code and its corresponding Swift 3 Code
============= Variables =============
Strings declaration
Objective C : Type is Must for Variable or Constant
Objective C : Type is Must for Variable or Constant
double price = 23.55;NSString *firstMessage = @"Swift 3 is awesome. ";
Swift : Variable Type is Not Required, this feature is called "type inference"
var price = 123.55
let firstMessage = "Swift 3 is awesome.
Swift : Var is for mutable variable and let is for immutable constant, VAR is must if LET is not used
Swift
Mutable and Immutable + variables and constants
Objective C
double price = 23.55;
int const temp = 100;
Swift : Var is for mutable variable and let is for immutable constant, VAR is must if LET is not used
var price = 23.55
let temp = 100;
let temp = 100;
Using Pointer symbol?
Objective C
NSString *str = @""Hello world"";
Swift
var str = "Hello world"
Strings declaration
============= Strings =============
Strings declaration
Objective C
NSString *str = @"Hello world";
Swift
var str = "Hello world"
String Concatenation
Objective C
NSString *a = @"Hello";
NSString *b = @"World";
NSString *str = [NSString stringWithFormat:@"%@%@",a,b];
Swift
var a = "Hello"
var b = "World"
var str = a + b
String Comparison
Objective C
[a equalsTostring:b]
Swift
a == b
How to call methods using delegates
Imports
Objective C
#import <UIKit/UIKit.h>
Swift
import UIKit
Line Termination
Line Termination
Objective C
NSString *a = @"Hello";
Swift
var a = "Hello"
Logs
Logs
Objective C
NSLog(@"test");
Swift
print("test")
Colors
Objective C
[UIColor redcolor];
Swift
UIColor.red
Pragma Marks
Objective C
#pragma mark - Separators
Swift
// MARK:
Arrays
Objective C
NSMutablearray *arr = [NSMutableArray alloc] init];
[arr addObject:@"first"];
[arr addObject:@"second"];
[arr addObject:@"third"];
Swift
var arrayOfInts: [Int]
var numbers: [Int] = []var listOfNames = ["Sam", "Andrew", "George"] // an array of strings
var arrayOfStrings: [String] = ["We", "Love", "Swift"]
var listOfNumbers = [1, 22, 13, 10, 100] // an array of numbers
Dictionaries
Objective C : Keys can only be string
NSMutableDictionary *dict = [NSDictionary alloc] init];
[dict setValue"@"SWATI" forKey:@"NAME"];
Swift : keys can be anything
var dict = [String:String]()
dict["NAME"] = "SWATI"
Button Clicks
Button Clicks
Objective C
-(IBAction)buttonClicked:(NSString*)str;
Swift
@IBAction func buttonClicked();
Parameterized Methods
Objective C
Signature:- (void)showAlertWithTitle:(NSString*)title andMessage:(NSString*)message;
Calling:[self showAlertWithTitle:@"Hello" andMessage:@"Welcome to Objective C"];
Swift
Signature:showAlert(title:String , message:String)
Calling:showAlert(title:"Hello", message:"Welcome To Swift")
Enums
Objective C
enum WebServiceType
{
WebServiceTypeLogin,
WebServiceTypeFetchData
}
Usage : WebServiceTypeLogin use as it is when required
Swift
enum WebServiceType:Int
{
case Login = 1
case FetchData = 2
}
enum NetworkStatusCode:Int
{
case Success = 1
case Failure = 2
}
Usage : NetworkStatusCode.Success
Protocols
Objective C
@protocol WebServiceResponseDelegate:NSObject
{
@optional
- (void)test;
@required
- (void)webServiceResponse:WebservicesManager
finishedTaskType:WebServiceType status:NetworkStatusCode id:remoteData
finishedTaskType:WebServiceType status:NetworkStatusCode id:remoteData
}
Swift
protocol WebServiceResponseDelegate:class
{
func test();
func webServiceResponse(assistant:WebServicesManager , finishedTaskType:WebServiceType , status:NetworkStatusCode , remoteData:Any)
}
How to make some methods in a protocol as Optional coz by default methods are mandatory to implement
@objc protocol abc { func test() }
How to call methods using delegates
Objective C
[delegate webServiceResponse:self
finishedTaskType:finishedTaskType
status:NetworkStatusCodeSuccess
remoteData:@""];
Swift
delegate.webServiceResponse(assistant:self,
finishedTransaction:finishedTransaction,
status:NetworkStatusCode.Success,
remoteData: "");
Exception Handling
Objective C
@try {}
catch (NSException *e) {}
finally {}
Swift
do{}catch{}