Sunday 28 September 2014

Learning Swift with iOS 8 tutorials part 1

What is Swift ?

Swift is Apple's latest programming language for iOS and OS X apps that builds on the best of C and Objective-C, without the constraints of C compatibility. Swift adopts safe programming patterns and adds modern features to make programming easier, and flexible.

The Basics:
Swift Providing below data types in his way of syntax. 

Int             : Used for integer type values.
Double      : Used for floating points values.
Float         : Used for floating points values.
Bool          : Used for Boolean values.
String        : Used for Text data values
Array         : Used for collection.
Dictionary  : Used for collection.


Constants and Variables
In Swift, Constants and Variables need to be declared first before using them.

let  x = 10
var y = 0

You can declare multiple constants and variables in a single line using coma as a separator.
let x = 10, y=11,z=0
var  a = 1, b=10, c=3


Thursday 25 September 2014

Best iOS interview questions on swift programming language


I started this post to all  iOS developers/beginners to prepare your self for Swift Interview questions. As its time to prepare with Swift interview questions. Swift is going to replace objective-c so we all have to prepare for best iOS interview questions on swift.

What is Swift ?

Swift is Apple's latest programming language for iOS and OS X apps that builds on the best of C and Objective-C, without the constraints of C compatibility. Swift adopts safe programming patterns and adds modern features to make programming easier, and flexible. 

Swift will replace Objective-C as the primary language in which iPhone and Macintosh applications are written.

How we declare variables in Swift ?

Constants and variables must be declared before they are used. Declare constants with the 'let' keyword and variables with the 'var' keyword.

What is difference between 'let' and 'var' declaration ?

constants are expressed with the ‘let’ keyword. So once assigned value can not be change, but assigned values using 'var' keyword can be change.

In terms of objective -c or compare with  objective -c, 'var' is  Mutable and  'let' is NonMutable.

let kMyConstant = 40
Or
let  kMyConstant: Int = 40

var myString = "This is my string."

What is Initialization ?

Initialization is the process of preparing an instance of a class, structure, or enumeration for use. This process involves setting an initial value for each stored property on that instance and performing any other setup or initialization that is required before the new instance is ready for use.
You implement this initialization process by defining initializers, which are like special methods that can be called to create a new instance of a particular type. Unlike Objective-C initializers, Swift initializers do not return a value. Their primary role is to ensure that new instances of a type are correctly initialized before they are used for the first time. 

Initializers
Initializers are called to create a new instance of a particular type. In its simplest form, an initializer is like an instance method with no parameters, written using the init keyword:
init() {
// perform some initialization here
}

In Swift How to connect UI under Swift Environment ?

UI connectivity has no change, its similar to objective-c. Xib/Storyboard under Interface builder.

Sunday 21 September 2014

Best way to modular code using multiple Storyboards in iOS app development

In my earlier post  Best way to Avoid merge conflicts with Storyboards I have made clear why to use multiple storyboards in your iOS app.


Xcode creates only single storyboard with name “Main.storyboard” at the time of creating new project. Lets see below image



Xcode Default Storyboard



Xcode is smart IDE tool its provides option to create another storyboard. how? as simple as creating new file. Under File menu choose New and follow below image steps.

Easy way to use multiple storyboards in iOS app development

Next save it with proper name. Finally you can see two storyboards in the same app.  see below image for better idea.

Managing Multiple storyboards in Xcode objective-c


Next Problem is loading another storyboard in iOS application.  But you can not connect a scene and or segue from one storyboard to another storyboard. Don’t worry we have another solution for working with multiple storyboards in iOS app. We can load another storyboard using little bit coding on an IBAction method and or a selector.

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main” bundle:nil];
UIViewController* myStoryBoardInitialViewController = [storyboard instantiateInitialViewController];

[self.navigationController pushViewController:myStoryBoardInitialViewController animated:YES];


You can use another method to load a scene/ View-controller  using identifier. If you given a identifier to a scene.
Loading scene using identifier is very similar to old way where we were using XIB name to navigate from one View-controller to another View-controller.

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main” bundle:nil];
UIViewController* myStoryViewController = [storyboard instantiateViewControllerWithIdentifier:@“sceneIdentfier”];

[self.navigationController pushViewController: myStoryViewController animated:YES];



Using above code you can load UI from multiple storyboards.

If you have some other Best way to use multiple storyboards in iOS app development please share with me in comments section.





Thursday 18 September 2014

Best way to Avoid merge conflicts with Storyboards

As of using storyboard in iOS app development, it is observed that working as a team on an Application, we face issue of  merge conflicts in storyboard changes. for example, if  same time two developers make changes in UI, it creates issue in merging code because of having single storyboard for all team members.

All though there are lots of compare-merge tools to solve merging issues. These tools are very useful for  merging other files like .h and .m files but these tools are not so useful to compare and merge storyboard changes.

So Creating multiple storyboards in the application is the best way to Avoid merge conflicts in case of working as a team. For big projects, recommends to use multiple storyboards in practice for more modular code.

 Xcode gives option to add multiple storyboards in application. It would be good if we create one storyboard for one module or related screens 
Like Login, Registration and Forgot Password related scenes/Screens can be create in one storyboard and User Profile, Settings can be in separate storyboard.

By doing this, we can work as team even without storyboard conflicts. And without wait for other member to finish his UI changes first and get updated code.

Summary:
* Small project can have a single storyboard.
* For big projects it is recommended to create multiple storyboards in practice to more modular code.
* Multiple storyboards practice avoid merge conflicts when working as a team.

Saturday 13 September 2014

How to Convert iPhone Storyboard in to iPad Storyboard

Some times it happens that, first we develop an iOS app for iPhone environment and latter it requires to develop same for the iPad environment in very short time of period. As a developer I face this issue. So I would like to share my experience to all of you. Don’t worry, its very simple to change  iPhone Storyboard in to iPad Storyboard .
Let me share the steps to help you understand.

Would not be a good idea if I share some images in between of textual content?  Wow.. Its great to follow.

Steps:

1- Open you iPhone application.
2-Select iPhone storyboard (mostly named as “Main.Storyboard”), Using mouse or laptop keypad right click over the storyboard and select option to open As- Source Code.
See below image to better understand.




Source code will open as a XML code for all of the UI. Here is everything written as code. whatever we create UI in Scene it contains the coding part for that.

3- Find in this XML file targetRuntime="iOS.CocoaTouch"
See below image for better understanding.




4- Simple change it to targetRuntime="iOS.CocoaTouch.iPad”.
 See below image for better understanding.






don’t worry I am not going to share more steps its done all about. Now you need to come back from this XML file in to Storyboard.
Write click over Storyboard in left side panel where all files listed and Select open As - Interface builder-iOS Storyboard.

See below Images for better understanding.




Now you will see that all of Scenes of Storyboard changed to iPad size. If this not happens don’t worry we have one more steps to change it manually.
Select your view controller object in storyboard and change it size to iPad-Full screen. See below image for better understanding.




Wow its great. Now enjoy with iPad Storyboard.