Tuesday, March 28, 2017

Swift 3.0 Cheat Sheet


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
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.



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;



Using Pointer symbol?
Objective C
NSString *str = @""Hello world"";

Swift
var str = "Hello world"



============= 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




Imports
Objective C 
#import <UIKit/UIKit.h>

Swift 
import UIKit


Line Termination
Objective C
NSString *a = @"Hello";

Swift 
var a = "Hello"


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

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{}

Thursday, March 23, 2017

Fun with Gradle

Hi All,

Greetings for the day!!!

Have been wondering from many days as to why should I use Gradle if same features are provided by xcodebuild also

  • A command line tool provided by Apple that creates builds/packages.
  • It can take input target, scheme, ipa name, signing information etc in the arguments.
  • Supports multiple tasks and environments.
  • Can be integrated with Jenkins too.


Then why Gradle??
  • Build Management along with Dependency Management. Xcodebuild can only do build management and for dependency management it needs Cocoapods.
  • If you are working on multiple platforms like iOS and Android then Gradle is the common choice for both.

Now how to Use Gradle with Xcode

  • In terminal java -version should be 7 
  • if not type $ brew cask install java  or $ brew cask install Caskroom/versions/java7
  • Download gradle via homebrew $ brew install gradle
  • Type $ gradle -v to check its version

2) Create build.gradle file for your project.
3) Navigate to project directory in terminal.
4) $ gradle build
5) Project builds
ta da....


Sample build.gradle file

plugins {
  id "org.openbakery.xcode-plugin" version "0.14.5"
}

xcodebuild {
     target = 'Hello-Gradle'

/* If need to differentiate between iOS and android */
     type = 'IOS'             

/* Set ipa file name*/
     ipaFileName = ipaName 

 /* if u wish to create archive its necessary to set simulator as FALSE coz for archive we need DEVICE*/
     simulator = false        
}


To execute above file code you can also use:

$ gradle xcodebuild 

Wednesday, March 22, 2017

Fun with Collection Views



Hi all,

Lets start the new year with some fun. I was always interested to create grid like structures in my app and for many times i tried third party tools to fulfill my need but this time i decided to work with Apple's UICollectionView and trust me its awesome.

I know its easy for many who have worked with them but for those who haven't here is a path to start.
You want to show a grid view layout on your view controller. For this we need 3 things :

1) UICollectionViewCell               : This will decide the UI of the grid controls
2) UICollectionViewFlowLayout : This will provide the UI placements and positions
3) UICollectionViewController    :  This will help in UI rendering


First of all create a class named "CollectionViewCell" that is derived from UICollectionViewCell.
This class must have an attached XIB [though u can create programmatically too]. :)

CollectionViewCell.h



CollectionViewCell.m



CollectionViewCell.xib

CustomFlowLayout.h



CustomFlowLayout.m







CollectionViewController.h




CollectionViewController.m





Now here we set the collection view info


The important collection View DataSource Methods









Compile & Run the App
  &
Here is the final cartoon grid view





Tuesday, March 21, 2017

Packaging via ICEBERG in 10 Steps


Hi all,

Learned a new thing today.
Thought of sharing it.

Hope you like it!!!!

If you are willing to create installer packages then ICEBERG by WhiteBox is the right choice for.

Lets follow the simple steps

Step - 1 : Download Iceberg.dmg from Whitebox
Step - 2 : Once downloaded install it.
Step - 3 : This does not comes with default screen instead a top menu bar would be visible



Step - 4 : Click on File and then New Project
Step - 5 : Choose Package from Core Template and click Next

 




Step - 6 : Select the Project Name and directory location. ~/ is the default location of users Home directory.





Step - 7 : The default interface becomes visible with all the default settings. Will be shown via images.  
Do not change any default settings as of now. 
Jump to Files section






















































































































































Step - 8 : Now in Files section
Drag and drop the folder which contains all the files you need to add in package.





Step - 9 : Go to Build menu and click Build or CMD + B. You can see a success here.



Step - 10 : Now where is the package saved??? YES you got right. The default location u chose.






Enjoy and have fun with packaging :)