Pages

Showing posts with label Advance Programming. Show all posts
Showing posts with label Advance Programming. Show all posts

Learning Key Value Observing with an example

Key value observing is a feature in Swift that depends on dynamically dispatched methods. You might ask what thats means, well dynamically dispatched methods are those instance methods of an object which are run by querying the related object. For further explanation I recommend that you refer to my post "Key Value Observing In Swift". Let us learn it with an example in which we will have two text fields first one is for entering name, second one if for entering age && a button in storyboard. Now create two outlets for the two textfields && an action function fro button below shown picture is showing this arrangement:

Handling touch events with Swift

Well guys as I explained about how the events are handled in my post "Responder chain", here today we will be working on how to handle touch events. There are basically four touch functions that works on handling the touch events in any application. Here are these functions:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent)
override func touchesMoved(touches: NSSet, withEvent event: UIEvent)
override func touchesEnded(touches: NSSet, withEvent event: UIEvent)
override func touchesCancelled(touches: NSSet!, withEvent event: UIEvent!)

Convenience && Designated && Required Initializers

Designated Initializers:--> There are two types of Initializers in Swift. First type is Designated Initializers and Second type is Convenience Initializers. All a designated initializer do is just to initialize an object completely by sending an initializer message to its super class. The class that implements designated initializer must also implements designated initializer of its super class. Here is an example of designated initializer:

Key Value Observing In Swift

Key value observing is a feature from Objective C. It depends on dynamically dispatched methods. You might ask what thats means, well dynamically dispatched methods are those instance methods of an object which are run by querying the related object. Objective C supports dynamic dispatching. But dynamic dispatching comes with a disadvantage of its low performance. Because in dynamic dispatching first the object is asked which code is to execute for this particular method, this query is asked every time the method is called. But Swift supports static dispatching means when you call a particular method of an object CPU jumps to it directly, without asking any query to corresponding object. This is the main reason that Swift is much faster than Objective C. And  as Key Value Observing feature only works with Dynamically Dispatched method, will not work with statically dispatching.

How our application start in iOS

As you know that AppDelegate.Swift class is that main starting or entry point of our application. As we tap the build button in xCode && all Swift source files are compiled && also if the main.swift file exists then this file will be set as the entry point of our application. In AppDelegate.Swift file a very special attribute named "@UIApplicationMain" exists whose task is just to synthesize the main method for the application && set the associated file as AppDelegate.Swift file. As you also know that UIResponder is the parent class of all the UI content of iOS, && our AppDelegate.Swift file inherits this class. The main function calls the UIApplicationMain method that reads the Info.plist file that isa existing in our Project Navigator. If in this file all the key value pair for launch screen && Main.storyboard exists then the application will start with the extra functionality that are present in the Info.plist file. Here is the file named as Info.plist :

Adding textFields to the UIAlertController

UIAlertController is a class that display Alerts on the screen as per the different actions of the user. It is a class that replaces the UIAlertView && UIActionSheet. Alerts can be of any style like Destructive that will be shown when user did something awfully wrong && more. The action sheets can be shown differently based on the devices like on iPad it can be shown as Popover Controller. Basically on the controller you can add different actions with addAction() method. UIalertAction works with a completion handler in which you further can define other actions corresponding to the action that you will perform accordingly.

Extracting data like thumbnail images , Artist Name etc. from Audio file

   As a iOS developer if you are to design your custom media player then how will you get the information like thumbnail images, artist name, singer name etc. this is what I will show you in this post. But before that you should know about some classes && functions because you will be needed those classes && their functions in the code.

AVAsset : AVAsset is basically an abstract class that represents media such as Audio files or video files. And each asset contains a collection (big/small) of tracks that will be processed together. An AVAsset object defines different properties of the tracks. You instantiate asset using AVURLAsset class that represents media like mp3 files, mp4 files avi fils etc.

Crucial facts about iOS app development

1. Hiding the Status Bar of the ios simulator:

When you are designing an application that uses the table view or content view then the very basic situation you will fell into is the table or content view is colliding with the status bar of the ios simulator, then to remove it the function you will be using is:
---->>   override func prefersStatusBarHidden() -> Bool {
                                return true
                   }

 What this function do is hide the status bar of the ios simulator for you by returning "true". If this function will return value "false". The status bar will not be hidden it will be as good as not to use this function.