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.



















                                                                                       







What's app like status update-Create.

Objective:
               To write a status and update on table as in what's app & read the content in next page .

Approach:
               Class1-Design,implementation of text view,button,tableview;
               Class2-Display the content after selecting the row.

Solution:
             1.We need three mandatory UIelements(textview,button,table).I prefer writing it programmatically,you can use either xib or Storyboard.

             2.import protocols-UItextViewDelegate,UITableViewDataSource,UITableViewDelegate.

             3.since the textview's text has to posted on the table it's necessary to create an instance of NSString class to store the value.Create an instance for an NSmutable array to add the string object,so that we can post it on tableview cell.

            4.I added border width and corner radius to my textView so i imported "Quartz Core Framework".if you wish to add those make sure you import quartz Core framework.


So this is what my .h file looks like







       
Since we have added protocols,it's mandatory to import the required methods.

UITextViewDelegate doesn't have any required methods,it only offers optional methods.for our requirement we will use [- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text] Method to return the keyboard in textview.












But tableview offers two required methods from UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;


Next step will be designing uielements,Make sure to return the array in Number of rows inSections method and add object for index in cell for row at index path method.

So now wen we run the program,this is how it should look like(not exactly like mine,..but something like this.LOL..)

























The trick lies here:

Do the following in your post Button method:

1.remove tableview from superview;

2.Save your textview's text in an string and add that to NSMutableArray;





















Add the array in the delegate methods-number of rows in section & in cell for row at index path;




And the output of the following code will look like this:

Writing a status Update.
After KeyBoard is Disabled.


The Status is posted to the table.





















If we wish to show the contents of the cell in next page import an optional method -didselectrowat indexpathmethod;













Here,m_pCopyString is an Nsstring of Class2;

Add the following in class 2:

















Conclusion:

                 This is just a demo to show how we can update a status like on what's app.you can further improvise if you like.