Posts

Showing posts from February, 2012

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  NSAutoreleaseFreedObjectCheckEnabled &  NSDebugEnabled,

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: // ObjC version - (UITableViewCell * ) tableView: (U

Mapping folder into a drive letter on Windows

Sometime I had to work with folders site deep under my file system structure, problem with this is I’ll have to browse to that folder every time I need to get something or put something in, this is quite annoying since the Windows Explorer don’t always remember your recent used folder. To fix this I use a command that available in MS DOS call SUBST, the description of this command is as below C:\>subst /? Associates a path with a drive letter. SUBST [drive1: [drive2:]path] SUBST drive1: /D drive1: Specifies a virtual drive to which you want to assign a path. [drive2:]path Specifies a physical drive and path you want to assign to a virtual drive. /D Deletes a substituted (virtual) drive. Type SUBST with no parameters to display a list of current virtual drives. What I do is just type SUBST K: C:\Documents and Settings\ChaoTze\My Documents\MyProjects\Phase1\Version1\Eng\Project1-2010 After that I can just use the mapped drive K: to acesss all my files

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

Using Workspace in XCode 4

Workspace in XCode 1) Create new Workspace (refer as WS) 2) Configure Workspace Settings File > "Workspace Settings" > Build tab > Advance > Build Location > Derived Data Location 3) Add project to WS (refer as P), make sure it runs. 4) Configurating (P) "PROJECT > Build Settings" > "Search Paths" > "Always Search User Paths" = "Yes" > "User Header Search Paths" = "../(libraryFolderName)/(libraryFolderName)" OR > "Header Search Path" = "../(libraryFolderName)/(libraryFolderName)" > "Linking" > "Other Linker Flags" = "-ObjC" 5) Add library project to WS (refer as L), by right clicking on empty space in the "Project Navigator" (Optional) Make sure library project "TARGET > Build Settings > Deployment > Skip Install" = "Yes" (Optional)  If you plan test s

Import Private Key to KeyChain

Importing Private key to the Keychains sometimes throws an error, but if we use the security  to import via Terminal, there is no erro at all. Its probably a bug with the Keychains App. To import Private Key to key to Keychains using Terminal, type the following in the Terminal, where the *.p12 file is the private key and *.pem file is the cert, login.keychain refer to the the current login user's keychains store. security import PushFeedKey.p12 -k ~/Library/Keychains/login.keychain security import PushFeedCert.pem -k ~/Library/Keychains/login.keychain security import DeveloperKey.p12 -k ~/Library/Keychains/login.keychain security import DeveloperCert.pem -k ~/Library/Keychains/login.keychain

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 at iOS D