Tuesday, 7 July 2015

Collection Operators-An introduction.

Objective:
                 To get an understanding of collection operators and to use it effectively.

What is an collection operator?
                                                  Collection operators are specialised key-paths that are passed as the parameter to the valueforkeypath: method .The operator is preceded by (@),some of the collection operators -@avg,@max,@min,@count,@sum;

Example:
               To find the avg of the objects in an array.

Approach-1:
               
fuck...





Thursday, 14 May 2015

Adding Add ons on UITextField

Objective:
                To try and add some interesting things to the UITextfield

What interesting things?

1.Adding a custom fonts;
2.Adjusting the font sizes to fit the width;
3.set an background image to the textfield;
4.setting an logo inside an textfield [left view];
5.Setting maximum character length;

Approach:

1.Import <UITextFieldDelegate and an Instance of UITextField;
2.Create a textField Either by programatically or by Xib or through StoryBoard;



To add a custom Font:




Adjusting the font sizes to fit the width:





Set an background image to the textfield:




when we compile & run the code-the output is :




setting an logo inside an textfield :






Setting maximum character length:

If we have to limit our character length in a textfield,We have to import this delegate method:-

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

and add the following code to the delegate method:














The above method should return true if a planned text substitution should be allowed to happen, false otherwise. The return statement evaluates to true if the new string will be of length 5 or less.





Sunday, 26 April 2015

BackGround Threading.

Objective:
                To understand background threading and perform a simple Math with and without background threading.

Why we need to use background threading?
                 When we build an application,we need to be extremely aware of the processing that goes in the UIThread,else if the process is too time intensive,then the user interface may not respond or may become unresponsive and it will lead to poor user experience.

Understanding the need for background threading:
                 To understand the problem just create this either programmatically or use Xib or StoryBoard,The desirable UI is:

Two buttons:-
                      1.Process in ForeGround;
                      2.Update Label;
One Label:-
                   1.Label;


























Approach:














Now if we run the program,and when we tap the Process in ForeGround button,and immediately if we tap update label button to check the time,the update label will not start performing operation until the process in foreground operation button stops.This leads to poor user performance because the user interface remains unresponsive until particular operation is completed.The reason this happens is because we're doing all the processing within the method directly called by the UI,it's preventing anything from being processed.So,even though you've tapped update label button,it won't respond.

So to overcome this we use backGround threading.

        Create another button Process in BackGround.




























Add the following code in the button method:



















           We first call dispatch_async and send in the dispatch_get_global_queue method to get the DISPATCH_QUEUE_PRIORITY_BACKGROUND.The background queue is kind of a default very low priority queue that can be used for low priority processing.dispatch_get_global_queue is used to get shared concurrent queues,of which background queue is one of them.The background queue will be scheduled only after executing the high priority queues(if present)
       
           After the loop we have created another method dispatch_async .In this call we call dispatch_get_main_queue  and pass in a block which will call BackgroundOperationsDone method.The dispatch_get_global_queue gets the application's main queue which the UI is running on.And once the operations are completed,to get back to the main UI thread we are using the background operations method.


Conclusion:
           Now run the code,we will be able to tap both the buttons[process in BackGround & UpdateLabel] simultaneously and both will perform it's task without any time lag.and the user experience will also be good.This is just the tip of the iceberg,lot can be done using threading.

         

Tuesday, 21 April 2015

IOS-AppStates

App States:[5]

1. Not  Running-When the app is not launched or terminated by the user;

2.Inactive State:When the app is running in the foreground but not receiving any events.

3.Active State:When the app is running in the foreground and also receiving events.

4.BackGround States:When the app is running in the background and is also executing codes.

5.Suspended State:When the app is running in the background and not executing code. 

Frames and Bounds

"Ok,what is the difference between bounds and frame?"asks every interviewer.

Here's the answer:

Bounds:
             Bounds of a UIView is the rectangle,expressed as a location(x,y) and size (width,height) relative to it's own co-ordinate system.

Frame:
          Frame of a UIView is the rectangle,expressed as a location(x,y)and size(width,height)relative to the superview,it's contained within.

To make it more clearer,let's create a label and will check the output:



Now in NsLog,let's check for the corresponding values of frames and bounds:




So the output for the above is:





Wednesday, 25 March 2015

NSArray-Indepth Understanding and Usability

Objective:
               To get a deeper understanding of NSArray and it's usabilities.


               These are the list of things we are about to achieve:
                1.To find the object at index;
                2.To iterate array using FastEnumeration and iterate over normal For Loop;
                3.To find if two array's are equal or not;
                4.To Enumerate array using blocks;
                5.Membership Checking-Bool Checking and Index Checking;
                6.Sort Array;
                7.SubDividing Arrays;
                8.Combine Arrays;
                9.Adding String Inbetween array objects.

What is an NsArray:
           NsArray along with NSMutable Array is an ordered collection of objects in objective c.NSArray creates a static,immutable array. ie.The values inside the NSArray cannot be changed.

Solution:
1.To create an array and to find the object at index:

OUTPUT:




 2.To iterate array using FastEnumeration and iterate over normal For Loop:

Fast enumeration is the most efficient way to iterate over NSArray and it's content are guaranteed to appear in the correct order.


OUTPUT:





The same can be done using traditional for loop:





OUTPUT:




3.To find if two array's are equal or not:

   Method Used:isEqualToArray.









OUTPUT:


4.To enumerate NSArray using blocks

Method Used:EnumerateObjectsUsingBlocks;

Signature:(id obj,NSUInteger idx,BOOL *stop)




OUTPUT:





The objects are passed in the same order as they appear in the NSArray.

5.Membership Checking-Bool Checking and Index Checking:

1.BOOL Checking:

Method Name:ContainsObject;

It returns YES if the object is in the array else it will return NO


OUTPUT:




2.Index Checking:

Method Used:indexOfObject;

This will either return the requested index of object or will return NSNotFound;
OUTPUT:



6.Sort Array:

Sorting is one of the main advantage of using NSArray.

Method Used:SortedArrayUsingComparator.

This accepts an NSComparsionResult(id obj1,id obj2)block,which should one of the following enumerators depending upon the relationship between obj1 & obj2.


Return ValueDescription
NSOrderedAscendingobj1 comes before obj2
NSOrderedSameobj1 and obj2 have no order
NSOrderedDescendingobj1 comes after obj2
OUTPUT:



7.SubDividing Array:

Method Used:SubArrayWithRange.


OUTPUT:



8.Combining Arrays:

Method Name:arrayByaddingObjectsFromArray;

It takes two arrays and combines the two arrays using the above method.this returns as a new array containing all the elements of the original array,along with the contents of original parameters.


OUTPUT:



9.Adding a string element in-between array object:

Method Used:ComponentsJoinedByString;

The above element concatenates each element of the array into a string,separated by the specified  symbols.

OUTPUT:

Wednesday, 11 March 2015

Core Location-Get users location.

Core Location FrameWork:
                                          Core framework provides an interface to get the user's location.But however user must first allow the app to use location services in order to get the details.




Things to do first:
                  1.Add Core location framework;
                  2.Import protocol-CoreLocationManagerDelegate;
                  3.Add 4 Labels & 4 Corresponding Textfields To Display-Latitude,Longitude,Altitude,Speed in the storyboard.
               
Design:





Drag & drop 4text fields Corresponding to their labels from object library .The textfield Border style is changed from default to bezel;

meanwhile,declare these in the .h file.

#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController <CLLocationManagerDelegate>





IBOutlet-is used to define a property of a UIComponent.

textfields are controls on which we will show location data.

Synthesize the above properties in .m file.

Now it's time to do the real work.



CLLocation Manager is responsible for the location data.

in ViewDidLoad,we then instantiate the CLLocation manager object which will be responsible for location data.the delegate is set to self and desired accuracy is set to best,the higher accuracies might take more devices resources,We then call start updating method of the location manager,by doing this we request location updates.

Next step is to import delegate methods of CLLocationManager property

Delegate Method1-Import didFailWithError;
Delegate Method2-DidUpdateLocations;

- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error;

- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations


- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
    
    
    CLLocation *CurrentLocation=[locations lastObject];
    Latitude.text=[NSString stringWithFormat:@"%.8f",CurrentLocation.coordinate.latitude];
    Longitude.text=[NSString stringWithFormat:@"%.8f",CurrentLocation.coordinate.longitude];
    Altitude.text=[NSString stringWithFormat:@"%.0f m",CurrentLocation.altitude];
    Speed.text=[NSString stringWithFormat:@"%.1f m/s",CurrentLocation.speed];
    
    
    
    
    
    
}


This method is called when the device has new location data and it will display on the corresponding textfields.didUpdateLocations method provides an (NSArray*)locations which contains the most recent updates,so the most recent location is the last object of NSArray.We then assign the  lastLocation value to CLLocation object(current location)and we set the location data to the textfields.

Make sure you give right format.

Here,Current.location.latitude &longitude -belongs to CLLocationDegrees Class.

Altitude belongs to CLLocation Distance that's why it's of the format-@"%.0f m".

Speed-CLLocationSpeed-format-@"%.1f m/s"-because distance is measured in metre/seconds.


- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error
{
    
    UIAlertView *LocationUpdateFail=[[UIAlertView alloc]initWithTitle:@"ERROR" message:@"Sorry,Fail to get to get the updates.chk later" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    
    [LocationUpdateFail show];

    
}



That's it,save and run the program.




Thursday, 5 March 2015

Designing a profile.

Objective:
              To create a simple profile with circular profile picture,cover picture and details.
Our desired output has to be like this:




Approach:
1.Create 2 imageviews;
2.Create 3 Labels[studied at:,Lives in,Born on]

Solution:

For the Cover picture-It's very direct,just set the frame to your desired height and create it.

for our profile picture-Create an image view of same width and height and add it is a subview of cover picture.








if we run the program we will not be getting our desired circular profile picture instead it will look like this



But our objective is to make a circular profile picture.so in order to get circular image,we have add layer to the imageview-An instance of CALayer Class [for every view,there is a bundled layer property] & add a corner radius to it.


To make a profile picture circular from a squared one,radius is set to half of the width of UIImageView...Clips to bounds is set to yes to make it work the above statements.if you wish to make it even better try adding border width and corner radius to UIImageView and it will look fabulous.Add 3 labels for showing details and customise the fonts if you like to make it look attractive.

Conclusion:
                 Thus with right imagination & adding simple lines of codes,we can make a cool circular profile pic.











Wednesday, 4 March 2015

Fahrenheit-Celsius Conversion.

Objective:
               To convert a value in fahrenheit  to degree celsius.

Approach:
               use the formula:
                                        celsius=(fahrenheit value  -32 )  x  5/9 
                                                        or
                                       celsius=(fahrenheit value-32)/1.8;
Solution:

              output for the following program will look like this:




To get this as a desired output we have to design a textfield,a button to convert and label to display.

To make it look more aesthetic i have added a background colour,set border width and corner radius for textfield by importing QuartzCore Framework and customised fonts for label.finally added decimal pad keyboard type instead of default keyboard type.

In our button action (CONVERT),we have to first declare two double values for fahrenheit and celsius and do the math
;


Save the value of the celsius in a string and show it in the label's text.









So the task is done .finally you have to disable the textfield's decimal pad keyboard.we can't do this in textfield should return method.

So add touches begin method and complete the task .



Conclusion:
                Using this same approach we can convert different metrics using their appropriate formulas.