Posts Tagged ‘question’

Snow Leopard, 64bits, Ruby and MySQL

I recently started again wanting to play a little bit with the so acclaimed Ruby on Rails framework. Being on a Mac, I had not much to do to start working, with Ruby & RoR already installed with my Snow Leopard default OS X install (maybe because I’ve already installed the XCode Developer package, as well as the iPhone’s one… maybe you should check). So I was playing with my Ruby on Rails’ guide and I had already started building a project, having it displayed in my browser, so I knew the core of the install was up and ready.

Next step was to connect with MySQL. It proved not to be so simple as expected… Running rake db:migrate to check Ruby was well connected with MySQL lead to the following errors (both errors came repeatedly during my way to the solution, given below…):

  • uninitialized constant MysqlCompat::MysqlRes
  • dyld: lazy symbol binding failed: Symbol not found: _mysql_init
    Referenced from: /Library/Ruby/Gems/1.8/gems/mysql-2.7/lib/

After some research and tests following different articles on the net, I finally got with a working solution (however please notice that it applies to an Intel Mac with 64bits processor under Snow Leopard):

  • Install the latest x86_64 MySQL release (currently the mysql-5.1.53-osx10.6-x86_64). Several articles on the net will tell you the problems are due to the 64 bits version, you should install the 32 bits one, and so on. In my case, I tried both and it worked with the 64 bits one.
  • Make it work! Check the install documentation, connect with mysql, start the server… to be sure everything is working. A good thing would be to be able to connect to it with phpMyAdmin for example (which I did).
  • If you had already install the mysql gem (which you should have to need this page because you must have faced the previously mentioned issues…), uninstall it with sudo gem uninstall mysql.
  • The trick is then to reinstall it with the following command line: sudo env ARCHFLAGS="-arch x86_64" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config which ensures the gem is installed for the 64bits architecture, matching the MySQL one.

Then, you should not face the previous problem any more, maybe some access denials due to misconfiguration in your RoR’s project “database.yml” file or your MySQL configuration. But the Ruby/MySQL connection should be OK!

Categories: Mac OS X Tips Tags: , , , , ,

Retain your IB objects in Cocoa Touch

I previously published 2 articles on the fact that you had to retain your objects instantied from Interface Builder. I finally discovered the scientific explanation within an AdMob source file:

Note that top level objects in nibs other than MainWindow.xib in Cocoa Touch are autoreleased, not retained like in OS X. Be sure to use [self retain] in -awakeFromNib when part of a custom nib (as in this example).

Extract from the AdMob SDK source files for iOS.

Automatically restart an app having started a call?

This post will be quite simple. Its goal is to answer this question : is there a way to automatically restart an app which has initiated a call, once this call ended? This question is pretty related to my own apps (I hope you already know them! if not, just check them in the AppStore! most are free!), since these are phone shortcuts (naturally just a little advertising picture below…).

My iPhone shortcut apps

So I recently checked to determine if I may make my apps a little more better, by allowing the user to have them restarting after the call ended. And the answer is… yes… but… this involves doing something which is not really nice in my case. In fact you can do this through using a WebKit view, and having a phone URL displayed in it. Having the user clicking this URL will bring a confirmation alert, if the user goes on the call takes place, and once it finished the app is restarted (you can Google this to find more details, I will not since I did not use this trick, doesn’t match my user experience goal!).

And that’s all! There is no other way, so if you don’t rely on a click on a link to start a call, do not hope to have your app restarting. iOS is not doing this… anymore (I remember it did in its first versions… it’s a shame it’s not customizable).

Have fun!

Object instantied by the interface (XIB) file need to be linked to code

Often using Interface Builder to instantiate some custom classes I use as controllers, I often fell on problems when using these instances. Without giving reason or anything, my program often “bugs”, quits, or anything unexpected when using these instances (for example when pushing a button whose action is set to a method of such an instance).

I imagine this is due to the fact these instances are not quite like ones I could create in my code with a [[CustomClass alloc] init] call, but I do not have scientific explanation! My way of getting out of these problems consists on creating an outlet for these instances in a liable instance. This seems to keep my interface-instantiated object alive!

If you have the scientific explanation to this issue, and the clean way to solve it, I’m listening!

PS: this post could be link to this previous one: My Custom object instantiated in my XIB file is released… is that normal?

How to add a search bar to your table view, and integrate it in your section indexes?

Assume you have a table view filled with objects. Assume you have sections for these objects, and you have setup the section indexes. Until this point, nothing which cannot be found in SDK’s documentation.

Assume now you want to add a search bar to your table view. What do you do? To answer this question, we will start from source code available to all of us, implementing the required table view with section indexes, we shall use the TableViewSuite’s 3rd example, I have named “3_SimpleIndexedTableView” (you can download the tutorial package there).

From this base, I will indicate the updates necessary to display a search bar in the first row. I will do this the fast way, so it may not be really nice. And I will not implement everything required to do search and display search results, you will have to look at documentation for this (this will essentially consist in connecting the search bar with required objects and delegates, providing content for search results).

So, what do we do to the TableViewSuite’s 3rd example:

  1. We change the numberOfSectionsInTableView: method in order to return one row more (you guess? …the row for the search bar naturally!):
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // The number of sections is the same as the number of titles in the collation.
    return [[collation sectionTitles] count] + 1; // CHANGE: add one for the search cell
  2. We update (a little) the tableView:numberOfRowsInSection: and tableView:titleForHeaderInSection: methods in order to have it return good number of rows for the good sections:
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) return 1; // CHANGE: return one for the search cell
    // The number of time zones in the section is the count of the array associated with the section in the sections array.
    NSArray *timeZonesInSection = [sectionsArray objectAtIndex:section - 1];
    return [timeZonesInSection count];
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) return nil;
    return [[collation sectionTitles] objectAtIndex:section - 1];

  3. Finally, we update the most important method, I have named tableView:cellForRowAtIndexPath: :
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // CHANGE: add the search cell
    if ([indexPath indexAtPosition:0] == 0) {
    static NSString *CellIdentifier = @"SearchCell";
    UITableViewCell *searchBarCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    [searchBarCell addSubview:[[UISearchBar alloc] initWithFrame:searchBarCell.frame]];
    return searchBarCell;

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    // Get the time zone from the array associated with the section index in the sections array.
    NSArray *timeZonesInSection = [sectionsArray objectAtIndex:indexPath.section - 1];
    // Configure the cell with the time zone's name.
    TimeZoneWrapper *timeZone = [timeZonesInSection objectAtIndex:indexPath.row];
    cell.textLabel.text = timeZone.localeName;
    return cell;
  4. Like I told before, you should then get the searchBarCell connected to the good objects to have it up and working. One last tip, I use the UISearchDisplayController class and connect them both in this way:
    UISearchDisplayController *sdc = [[UISearchDisplayController alloc] initWithSearchBar:sb contentsController:self];
    [sdc setValue:self forKey:@"searchResultsDataSource"];
    [sdc setValue:self forKey:@"searchResultsDelegate"];

Uh oh, I almost forgot to give you the best tip! How to have the “search” tool icon (what’s this tool’s name in English?) in your section indexes? Just follow the guide… (’cause this cannot be found in the doc, but you may find it elsewhere on the net, like I did – however it’s too long ago, I have not kept the source link).

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [[NSArray arrayWithObject:@"{search}"] arrayByAddingObjectsFromArray:[collation sectionIndexTitles]];

If you have a problem, you can’t make it work in your code, do not hesitate to ask… I have tried this code changes and it worked for me, but I may have forget something!

[Edited] As requested, please find the xCode project there.

My Custom object instantiated in my XIB file is released… is that normal?

I wrote a NSObject subclass, which I use as a table controller in a dedicated XIB file. This XIB file instantiates this class, which is correctly used until… it is dismissed due to lack of retains!
I did solve the problem simply by retaining it, but I do not see why I have to… Did any of you face the same problem, or did I do something in my code I do not see?

%d bloggers like this: