[TIP] XCode Header Search Path

September 26, 2009 | Filed Under Tips, XCode | 1 Comment

For many of you, this sounds like a stupid thing. But for those that just use gcc -I from command line, can be a pain find how to do it.

So, the problem is. How can I specify my Include path in XCode (gcc -I./mypath).

XCode Search Header Path

Tap on your project target, and click “Get Info“, tap on “Build” tab and search “Path” as showed in the figure above. Then click on the “Header Search Paths” options and add your favorite include paths for selected target.

Grand Central Dispatch: First Look

September 6, 2009 | Filed Under Apple | No Comments

Mac OS X Grand Central DispatchIn the last years I’ve always used a “parallel task” approach foreach loops that I’ve in the code, not always to speedup but even to clean-up the code. How to do it? Wrapping threads and Thread Pool like in this C# Parallel Forech Code.

Snow Leopard has introduced a new BSD-level infrastructure, with simple and efficient API to do this job. Here a little usage preview.

Block objects are a C-based language feature that you can use in your C, Objective-C, and C++ code. Blocks make it easy to define a self-contained unit of work. Blocks are something like Actions (delegate {}) in C#. Very useful to embed function in loops.

Blocks looks like a “private” function pointer, but you can access to the “parent” vars. (If you’re a Python coder, you’ve exactly the same thing).

/* Blocks in Python...
 * def main():
 *    a = 10
 *    def test(k):
 *        print a, k
 *    test(128)
 */
int main (int argc, const char *argv[]) {
  int a = 12;

  void (^test_block) (int) = ^(int k) {
    printf("A Block: PARENT(%d) ARG(%d)\n", a, k);
  };

  test_block(128);

  return 0;
}

The GCD queue API provides dispatch queues from which threads take tasks to be executed. Because the threads are managed by GCD, Mac OS X can optimize the number of threads based upon available memory, number of currently active CPU cores, and so on. This shifts a great deal of the burden of power and resource management to the operating system itself, freeing your application to focus on the actual work to be accomplished.

#include <dispatch/dispatch.h>
#include <stdlib.h>
#include <stdio.h>

#define ITEM_VMIN       (1)
#define ITEM_VMAX       (200)
#define NR_ITEMS        (100)

static void __fill_item (void *items, size_t n) {
  int *i_items = (int *)items;
  i_items[n] = (ITEM_VMIN + (int)(ITEM_VMAX * ((double)rand() / RAND_MAX)));
}

static void __work_on_item (void *items, size_t n) {
  int *i_items = (int *)items;
  i_items[n] *= 100;  /* Do some Computation on this Item */
}

int main (int argc, const char *argv[]) {
  dispatch_queue_t queue;
  int data[NR_ITEMS];

  /* Get Global Dispatch Queue */
  queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

  /* Initialize data Elements, and run computation on each element */
  dispatch_apply_f(NR_ITEMS, queue, data, __fill_item);
  dispatch_apply_f(NR_ITEMS, queue, data, __work_on_item);

  /* Brief review of the items */
  dispatch_apply(NR_ITEMS, queue, ^(size_t n) {
    printf("Results: Item %lu = %d\n", n, data[n]);
  });

  return 0;
}

Mac OS X 10.6 Snow Leopard

September 5, 2009 | Filed Under Apple | No Comments

It’s finally here.  Mac OS X Snow Leopard, The world’s most advanced operating system. Finely tuned.

Mac OS X 10.6 Snow Leopard
Ok, It’s one week later… but I’ve installed it just right now. Maybe tomorrow morning an usage example of Grand Central Dispatch.

iPhone: Voice Mill

August 30, 2009 | Filed Under iPhone | No Comments

Yesterday I’ve played a bit with AVAudioRecorder, and this is a very small and funny example.
The main Idea is to create something like a wind mill that works with voice instead of wind.

The code below shows you how to record something. Then with the updateMeters() and peakPowerForChannel() you can extract the audio “noise”.

NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSNumber numberWithFloat:44100.0], AVSampleRateKey,
    [NSNumber numberWithInt:kAudioFormatAppleLossless], AVFormatIDKey,
    [NSNumber numberWithInt:1], AVNumberOfChannelsKey,
    [NSNumber numberWithInt:AVAudioQualityLow], AVEncoderAudioQualityKey,
    nil];

NSError *error;
recorder = [[AVAudioRecorder alloc] initWithURL:url
            settings:settings error:&error];
if (recorder) {
    [recorder prepareToRecord];
    recorder.meteringEnabled = YES;
    [recorder record];
    ...
}


The SWF Video is available here Voice Mill Video.
The Source Code is available here Cocoa Voice Mill Source Code.

iPhone: Touch, Drag, Hit!

May 1, 2009 | Filed Under Mac OS X, iPhone | 3 Comments

Yesterday, Fredrik has asked me something like “Drag & Drop with Hit Test” on the iPhone. Here’s a simple example.  There’s a simple UIView class that handles the touch events and a protocol that says that if the “DragMe View” has hit or not the “HitMe View”. Below you can see the result and the Source Code Link.

iPhone Touch Drag Hit

The source code is available here: Touch, Drag, Hit Source Code.

Qt/Cocoa DataStream (Binary Communication)

April 11, 2009 | Filed Under Cocoa | No Comments

In a time of REST protocols… I still love the Binary Communication. I don’t have much ideas this period, I need to find a different job (C# doesn’t fit my code necessity). The Example Source code, contains two Folder. One is the Qt4 TcpServer the second one is the ugly application that you can see below :)

Note: This example doesn’t have anything related to the Qt 4.5 Cocoa Support.

Run the Qt4 Server, and the Cocoa Application (Remember to change the SERVER_HOST in AppController.m) and try to send some binary data. The Qt Server will receive this data it will resend you. Protocol is Very easy 1 byte of Data Type (Int8, Int16, ..) and Data. Remember that Intel arch is Little Endian.

Qt Cocoa DataStream

Note: This is just a “Draft” a “non complete” example only to give the idea on how it works.

The Source Code is available here: Qt/Cocoa DataStream Source Code.

iPhone: Download and Display Gravatar

March 28, 2009 | Filed Under Mac OS X, iPhone | No Comments

Back to the real iPhone for a while… Gravatar, or globally recognized avatar, is an interesting free service. It can be integrated with AddressBook app or other apps that can have a picture related to email address. Here an Objective-C Example to retrive the Gravatar. It’s really simple!

iPhone GravatarThe Source Code is Available Here: iPhone Gravatar Example.

The Apple Worldwide Developers Conference (WWDC) takes place June 8-12, 2009 held at Moscone Center West in San Francisco. http://developer.apple.com/WWDC/.

Cocoa: Sidebar with Badges, Take 2

March 8, 2009 | Filed Under Cocoa | 13 Comments

Cocoa SidebarDo you remember the Cocoa Sidebar? No, Take a Look at this post Cocoa Sidebar with Badges.

Joel asked me to revise the Sidebar, allowing to add/remove and do other operation at any time.

I’ve rewritten all the code of the Sidebar, and I’ve added a couple of new features, like the default action handler and obviously the ability to add, remove items and badges at any time.

All the past features, like Drag & Drop are still present, but there’s something else to do to improve it. If You’ve suggestions, patches or something else post a comment or send me a mail!

The Source Code is Available Here: Cocoa Sidebar 2 Source Code.

Update 10 March 2008 – Source Updated with Fixes.

Next Page »