Posts

Lightweight Database Migration in CoreData

Image
Lightweight Migration : This is an automatic feature of Core Data, but is limited to only simple migrations. You can use this technique if the changes are among the following: 1. Adding a new attribute to an entity. 2. Renaming of an entity or an attribute. 3. Changing an attribute to optional or non-optional, whether it requires a value on creation. Schema Changes, Versioning and Migrations When you are developing an application you will almost never get the schema right the first time. So, Core Data has support for schema versioning and data migration. This allows you to define a new version of the Core Data schema by adding a new attribute or entity to the store. Then define how the store has to migrate from one change to another. Creating a new xcdatamodel (Xcode 4.3.2) If you want to do a lightweight migration first step is to create a new xcdatamodel for new schema. 1. Click on your xcdatamodeld file present in bundle. 2. Click on Editor Men...

Handling Multiple API calls in same UIController class

Problem: I was facing problem in handling multiple API calls in same UIController class, as responses of all come into same delegate method named "requestFinished" and we have to use flags & if-else loops for handling each response. Solution: 1. In this code i have made 3 Asynchronous API calls in same UIController class. 2. On button action all 3 API web metods are called at once. 3. For handling each API i have made a separate NSObject class. 4. I am passing API urls from UIController class to their respective NSObject class. 5. When API is called, control is handled to its respective NSObject class. 6. Each NSObject class has its own delegate method named "requestFinished" to handle API response. 7. As API response is received control is again passed on to UIController class. 8. In this manner we can make Multiple Async API calls at once from same UIController class. In some cases one API call depends on other API call response. In such situati...

Check part of String is present in NSString

Below is code snippet to check whether part of string is present in a provided NSString  NSString *myString = @"This is an APPLE";   if ([ myString   rangeOfString : @"APPLE" ]. location != NSNotFound )    {         NSLog(@"Found");    }    else    {         NSLog(@"Not Found");    }

Find URL from NSString

If you want to fetch a URL from a String: NSString *StringContainingURL = @"You can click  http://objectivecwithsuraj.blogspot.in/ "; NSDataDetector *Detector = [ NSDataDetector dataDetectorWithTypes : NSTextCheckingTypeLink error : nil ]; NSArray *matches = [Detector matchesInString: StringContainingURL  options:0 range:NSMakeRange(0, [StringContainingURL  length])];         for (NSTextCheckingResult *match in matches)         {             if ([match resultType] == NSTextCheckingTypeLink)             {                 NSURL *url = [match URL];                 NSLog(@"URL : %@",url);      ...

Detect Touch on UILabel

In one situation i want to detect touch on my Label, so i want to share the code : - (void)viewDidLoad {     [super viewDidLoad];     UITapGestureRecognizer *taplabelGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureTap)];     UILabel *label = @"Suraj Mirajkar";     [label setFrame: CGRectMake(5o, 50, 100, 80)];     [self.view addSubview: label];     label .userInteractionEnabled = YES;     [label  addGestureRecognizer:taplabel Gesture ];     [taplabel Gesture  release ]; } -(void) gestureTap {    NSLog("Hey, you detected touch on Label"); }

Remove part of string between two strings

In below sample i have removed string between two angular brackets i.e. between < & > I want the strings between the angular brackets so, -(void) FetchString {     NSString   * descriptionString = @"<hi this is> Suraj <and this is my> blog";     NSString   *outputString        =  [self  removeNodesFromXMLString:  descriptionString   ];     NSLog(@" outputString is :-  %@", outputString); } -(NSString *) removeNodesFromXMLString: (NSString *) descriptionString {     NSMutableString *resultString = [descriptionString mutableCopy];         NSRegularExpression *regex = [NSRegularExpression                                      ...

MultiLine UILabel

In many cases we need a label with multi line.  For doing this you need to set 2 properties : 1. Number of Lines - 0, it means any number of lines. 2. Line break Mode - WordWrap CGSize labelsize; UILabel *TextLabel = [[UILabel alloc] init]; [TextLabel setNumberOfLines:0]; [TextLabel setBackgroundColor:[UIColor clearColor]]; NSString *text=@"yourtextString"; [TextLabel setFont:[UIFont fontWithName:@"Helvetica"size:14]]; labelsize=[text sizeWithFont:TextLabel.font constrainedToSize:CGSizeMake(268, 2000.0) lineBreakMode:UILineBreakModeWordWrap]; TextLabel.frame=CGRectMake(10, 24, 268, labelsize.height); [cell.contentView addSubview:TextLabel]; [TextLabel release];