Thursday, January 17, 2013

Copy vs MutableCopy


The difference between 'copy' and 'mutableCopy' can be simply understood with polymorphism in Object Oriented Programming concepts.


We will take the example of Array in objective-C. MutableArray is the extension of NSArray class. Therefore, all the methods available in NSArray is available in NSMutableArray, but the additional methods present in NSMutableArray is not known to NSArray class.

Now moving ahead, the copy method on an NSArray will return an object of type NSArray(The array that can not be modified). And mutableCopy method will return an object of mutable type (The array that can be modified).

Now, there can below cases :

Case 1:

    NSArray *arr1=[NSArray arrayWithObjects:@"A",@"B",@"C"nil];
    NSArray *arr2=[arr1 copy];

    NSLog(@"arr1:%@",[arr1 description]);
    NSLog(@"arr2:%@",[arr2 description]);

In this case an NSArray object is returned and is received in an NSArray object.
So, the array received can not be modified.

Therefore, we will not be able to use below statement
[arr2 insertObject:@"Z" atIndex:0];


Case 2:

    NSArray *arr1=[NSArray arrayWithObjects:@"A",@"B",@"C"nil];
    NSArray *arr2=[arr1 mutableCopy];

    NSLog(@"arr1:%@",[arr1 description]);
    NSLog(@"arr2:%@",[arr2 description]);

In this case an NSMutableArray object is returned and is received in an NSArray object.
Since the receiver object is of type NSArray, it doesn't know the methods present in NSMutableArray, arr2 will not be able to use any of the methods of NSMutableArray.
That is, the method mutableArray will make no sense in this scenario.

So, we will not be able to use below statement
[arr2 insertObject:@"Z" atIndex:0];

Case 3:

    NSArray *arr1=[NSArray arrayWithObjects:@"A",@"B",@"C"nil];
    NSMutableArray *arr2=[arr1 copy];

    NSLog(@"arr1:%@",[arr1 description]);
    NSLog(@"arr2:%@",[arr2 description]);

In this case an NSArray object is returned and is received in an NSMutableArray type object. The receiver arr2 is now pointing to an object address that is of type NSArray. However arr2 has the additional methods than NSArray, it will not be able to use those methods coz the pointed object NSArray does not know the additional methods present in arr2(NSMutableArray).

Hence, we will not be able to use below statement
[arr2 insertObject:@"Z" atIndex:0];


Case 4:

    NSArray *arr1=[NSArray arrayWithObjects:@"A",@"B",@"C"nil];
    NSMutableArray *arr2=[arr1 mutableCopy];

    NSLog(@"arr1:%@",[arr1 description]);
    NSLog(@"arr2:%@",[arr2 description]);
    [arr2 insertObject:@"Z" atIndex:0];

In this scenario, the receiver(arr2) of type NSMutableArray receives an object of type NSMutableArray. Therefore, the receiver knows the additional methods of NSMutableArray as well as the object that is being pointed by receiver(arr2).

And finally below statement will work like charm,
[arr2 insertObject:@"Z" atIndex:0];

Log before insertion will be:
arr1:(
    A,
    B,
    C
)
arr2:(
    A,
    B,
    C
)
And log after insertion will be:
arr1:(
    A,
    B,
    C
)
arr2:(
    Z,
    A,
    B,
    C
)



Hope above description helped you.
Refer:The link below clearly explains the difference between copy and MutableCopy
http://sarojsblog.blogspot.in/2011/07/difference-between-copy-and-mutablecopy.html

ELCPickerController

The need to pick multiple images from a photo Album in one go made me to look into ELCPickerController.It's similar to UIImagePickerController except that it allows us to select multiple images.
It has 2 Delegate method one will send us an array of dictionaries.Each dictionary for each image so the array contains info about multiple images.

Friday, January 11, 2013

NSDate Helper methods

1.To display time in relative time stamp use this method


-(NSString *)dateDiff:(NSString *)origDate {
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setFormatterBehavior:NSDateFormatterBehavior10_4];
    [df setDateFormat:@"EEE, dd MMM yy HH:mm:ss VVVV"];
    NSDate *convertedDate = [df dateFromString:origDate];
    [df release];
    NSDate *todayDate = [NSDate date];
    double ti = [convertedDate timeIntervalSinceDate:todayDate];
    ti = ti * -1;
    if(ti < 1) {
     return @"never";
    } else  if (ti < 60) {
     return @"less than a minute ago";
    } else if (ti < 3600) {
     int diff = round(ti / 60);
     return [NSString stringWithFormat:@"%d minutes ago", diff];
    } else if (ti < 86400) {
     int diff = round(ti / 60 / 60);
     return[NSString stringWithFormat:@"%d hours ago", diff];
    } else if (ti < 2629743) {
     int diff = round(ti / 60 / 60 / 24);
     return[NSString stringWithFormat:@"%d days ago", diff];
    } else {
     return @"never";
    } 
}

2.If NSDateFormatter returns nil then use the below method to fix 
NSDateFormatter *parser = [[NSDateFormatter alloc] init];
[parser setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];

Thursday, December 20, 2012

Sequence of Different animations


Refer
http://stackoverflow.com/questions/11737658/how-to-chain-different-caanimation-in-an-ios-application

removeObjectAtIndex


removeObjectAtIndex:

Removes the object at index .
- (void)removeObjectAtIndex:(NSUInteger)index
Parameters
index
The index from which to remove the object in the array. The value must not exceed the bounds of the array.
Important: Raises an NSRangeException if index is beyond the end of the array.
Discussion
To fill the gap, all elements beyond index are moved by subtracting 1 from their index.
E.g:
    CAPropertyAnimation * nextAnimation = [[self sequenceOfAnimations] objectAtIndex:0];
    [[self sequenceOfAnimations] removeObjectAtIndex:0];

Assigning nextAnimation with object at index 0 and after that we removed.Thing is all objects beyond index 0 are shifted one place left to fill the gap.So the above two lines act like a queue,always first object will be removed.

sequenceOfAnimations array has contents in this fashion
[obj1,obj2,obj3,obj4,obj5,nil];
removeObjectAtIndex:0
now sequenceOfAnimations will be [obj2,obj3,obj4,obj5,nil];
removeObjectAtIndex:0
now sequenceOfAnimations will be [obj3,obj4,obj5,nil];




Tuesday, December 18, 2012

UnCategorized compilation error in StoryBoard


The problem is that you've got IBOutlets that link to prototype cells.
Note that prototype cells are just that: prototypes. So you can 

have multiple instances of each. 

Therefore Xcode won't know which instance to link the 

IBOutlet variable to.
You'll have to wait until the cells are instantiated inside 

cellForRowAtIndexPath to assign the properties

courtesy: 

http://stackoverflow.com/questions/10527602/uncategorized-compilation-failed-error-in-ios5-storyboard

Tuesday, December 11, 2012

Rechability class

1.To Disable ARC
Targets->BuildPhases->CompileSources->select the files->double click and type -fno-objc-arc for the required files

2.undefined-symbols-for-architecture-i386-scnetworkreachabilitysetcallback
Including SystemConfiguration.framework will fix this problem

3.Tableview not displaying anything,not triggering to call datasource and delegate methods
Probably Tableview's delegate and datasource might not have set properly.
Set the delegate and datasource for the table view.