Posts

Showing posts with the label objective-c

ObjC Optimization Tips

Image
Over the past few weeks, I've been focus on doing performance tuning on a iPad app.  A little background of the app It is an business app, doing financial investment. It has lots of moving parts on the GUI, user can drag, slide and flipping, a graph, and lots of numbers and dates. User can play around with different combination of input and see their investment prediction over few years. There were around 50 parameters (or more) that affects actual calculation result. The app has at least 8000 lines of codes just for doing calculation.  The story The Product Owner has been complaining to the team that the app has a serious performance issue. It's sluggish and the financial result does not reflect user's interaction immediately (PO generally don't care how complicate you back-end is). The financial graphs took ~5 seconds to redraw on screen, when user is moving the slider, and they are moving it continuously back and forth. Tools I uses for this task. ...

NSString's Split and Join crash course

Split a NSString is using the NSString instance method " componentsSeparatedByString " NSString * mystring = @"coding;is;kind;of;magic" ; NSArray * mysplitedstring = [mystring componentsSeparatedByString : @";" ]; The above code will produce an array containing the following element: coding is kind of magic To join an array of NSString back to a single NSString, use the NSArray instance method " componentsJoinedByString " NSString * mystring = [mysplitedstring componentsJoinedByString : @";" ]; That will give you back the NSString "coding;is;kind;of;magic"

Debugging a freaking EXC BAD ACCESS in iOS App

Image
One of the exception that I often run into in any iOS app development is the freaking EXC BAD ACCESS . An error which is occurs due to error in memory management, e.g. accessing an object that has been release or passing an invalid pointer to a system call (delegate call).   Enough saying, so how do you trace it down and find the object that you are trying to access and cause that exception? Well there is 2 ways: NSZombie which is mentioned in Apple developer library's Memory Management guide Instrument tools to profile your app. See this article for details on this tools. To use NSZombie class you simple add  NSZombieEnabled  into your project Environment Variable . You can access the  Environment Variable settings screen by clicking  Product > Edit Scheme from the menu.  Expand Environment Variables section and click on the + icon. Add NSZombieEnabled into the field. (you can also add  NSAutoreleaseFreedObjectC...

Built-in UITableViewCell's Styles (UITableViewCellStyle)

Image
UITableView has some built-in styles which can be use with in most scenario. You might want to look into these styles before starting to built a customize TableViewCell. There are 4 types of styles for the UITableViewCell , enum representation of this is UITableViewCellStyle . The details is as follow:   UITableViewCellStyleDefault , Default style with label on the left.    UITableViewCellStyleValue1 , Label on the left, and a blue label on the right.    UITableViewCellStyleValue2 , Blue label on the left side (right aligned text), label on the right side (left aligned text).    UITableViewCellStyleSubtitle , This is almost same with the Default style except that it has a grey smaller text under the label. It functions as the description / subtitles. To change the default style of a  UITableViewCell , replace the styles enum in initWithStyle with any of the enum mentioned above, simple as that. Example: // Obj...

Converting NSDate to NSString

Often we need to display a NSDate object on the screen with a specific format or by specific time zone. We can do this to the NSDate object easily. Quick note for doing this is: Create NSDateFormatter object (e.g. dateFormatter) Call dateFormatter setDateFormat Call dateFormatter stringFromDate Sample code NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:"yyyy-MM-dd 'at' HH:mm"]; NSString *dateDisplay = [dateFormatter stringFromDate:date]; NSLog(@"%@",dateDisplay); // 2012-02-08 at 18:30 To display AM / PM NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:"yyyy-MM-dd 'at' h:mm aaa"]; NSString *dateDisplay = [dateFormatter stringFromDate:date]; NSLog(@"%@",dateDisplay); // 2012-02-08 at 6:30 PM You can also set the NSDateFormatter to a difference timezone. (You might want to do this if your app's data has different timezone setti...

NSString's Substring Crash Course

NSString * s1 = @"abc1234"; NSLog(@"s1 = %@",s1); NSLog(@"substringFromIndex,4 : %@",[s1 substringFromIndex:4]); NSLog(@"substringToIndex,4 : %@",[s1 substringToIndex:4]); NSLog(@"substringWithRange,NSMakeRange,2,4 : %@",[s1 substringWithRange:NSMakeRange(2, 4)]); NSRange indexOfString = [s1 rangeOfString:@"bc"]; NSLog(@"substringFromIndex,indexOfString,bc : %@",[s1 substringFromIndex:indexOfString.location]); Will Produce the following output: s1 = abc1234 substringFromIndex,4 : 234 substringToIndex,4 : abc1 substringWithRange,NSMakeRange,2,4 : c123 substringFromIndex,indexOfString,bc : bc1234

Customizing UISearchDisplayController's row height

Image
UISearchDisplayController is an elegant way to perform search on your UITableView controller, but occasionally (99% of the time) you would want to customize the look and feel of the UITableViewCell , and when you changed the height of the rows, your UISearchDisplayController doesn't pick up the value automatically (because it is a separate view controller). Which means when a user is searching with the UISearchBar, the rows in the search result has a different height than the table. My first attempt is by just changing the height directly in viewDidLoad with the following: self.searchDisplayController.searchResultsTableView.rowHeight = 82.0 ; But this won't work because the searchResultsTableView is recreated automatically by the UISearchDisplayController when results are shown, and the above line won't get called again. Thus, to overcome this we need to make sure whenever the result is shown, the above line will get executed. A quick search a...