533Getting Mouse Position in NSWindow

- (void)mouseDown:(NSEvent *)theEvent {
 NSPoint mousePoint = [theEvent locationInWindow];
 //NSPoint localPoint = [self convertPoint:mousePoint fromView:nil];
 CALayer *hitLayer = [[self layer]  hitTest:mousePoint];
 NSLog(@"MainView, hitLayer: %@", hitLayer.name);
 NSLog(@"mousePoint %f %f", mousePoint.x, mousePoint.y);
}

519A bit about Frameworks and Static Libraries on the iPhone

Frameworks are a way to share code and other resources between application. As sharing code between applications is not really possible on the iPhone (as is dynamic loading of libraries) it does not really make much sense to use Frameworks on the iPhone - except maybe for the nice handling of reusable code.

Where with Frameworks the specific files are accessable after the Framework is added to a project, working with Static Libraries is a bit more of an hussle. You not only need to add the library, but also need to make sure to set the right Header Search Paths, which can become troublesome when working with SVN'ed projects.

And because the architecture differs on the iPhone (ARM) and the Simulator (i386), having precompiled libraries is not the best solution. But adding a depended library project to your projects kind of negates the advantages of having reusable code by adding unnecessary complications.

Recommendation? Just add the desired files to your project. And if you really mind, don't copy, but only include by reference.

503UILabel and performSelectorOnMainThread

I've been struggling with that for a while... Why wouldn't my UILabel display it's updated text? The weird thing was, that I could update the text, as long as I called it from the same class.

But as I passed a reference to the UILabel to another class, and tried to set the text from there, it wouldn't show. Stranger still, I could see in the logs, that the value itself was updated, it only did not show on the screen.

I also tried to send a notification back to the class where I made the label, but again, no luck. Very strange, a directly called method would updated the label without problems, one called by the notification would not show the update.

What was I missing?

My first instinct was to check out [myLabel setNeedsDisplay];

I kind of worked. But only if I called from - for example a Button. It then updated the values of the UILabel. That's was a step forward, but not really satisfying; after all I'd like have updates on the UILabel whenever they arrived. And again, calling if from the notification also did not do the trick.

Then I found out, that it order for UILabels to visually update, the update command has to be called on the main loop! Yeah! And how to perfom and action on the main loop?

Yes, with performSelectorOnMainThread:

So, the solution it an anti-climax and here it is:


[myLabel performSelectorOnMainThread:@selector(setText:)withObject:myText waitUntilDone:NO];
//

492Target Conditionals

TargetConditionals.h

#include ;

// Gives you TARGET_IPHONE_SIMULATOR and TARGET_OS_IPHONE target conditionals.
// Set hello to "Hello, "!
#if TARGET_IPHONE_SIMULATOR
   NSString *hello = @"Hello, iPhone Simulator!";
#else
   NSString *hello = @"Hello, iPhone device!";
#endif

//Determining whether you’re compiling for the iPhone OS
#if TARGET_OS_IPHONE
   #import 
#else
   #import 
#endif

Conditionalizing Compilation and Linking from the Apple Dev Library.

I came across a simple and short IPHONE target conditional at some framework, but that did not seemed to do the trick. I am probably missing something there.

-

Update 1. tconf might be a step into the right directions.

Just out of curiosity, here a incomplete list of available target conditionals:

TARGET_IPHONE_SIMULATOR
TARGET_CARBON
TARGET_OS_IPHONE
TARGET_CPU_PPC 
TARGET_CPU_PPC64
TARGET_CPU_68K
TARGET_API_MAC_OSX
PRAGMA_ALIGN_SUPPORTED
TARGET_OS_UNIX
TARGET_OS_EMBEDDED
TARGET_OS_MAC
TARGET_OS_WIN32
TARGET_OS_UNIX
TARGET_OS_EMBEDDED
TARGET_RT_MAC_CFM
TARGET_RT_MAC_MACHO
...

Update 2. And that's the motherload. Nice of Apple to open-source that.

490‘Initial interface orientation’ setting in Info.plisy

The Informatin Property List of iPhone Applications has the optional setting of Initial interface orientation. Is a NSString type, I was wondering, which values were supported, bzw required. Luckily, auto-complete is your friend.

For completeness' sake, here are the accepted values:

Portrait (top home button) Portrait (bottom home button) Landscape (left home button) Landscape (right home button)

484Printing Selectors in NSLog

Using NSStringFromSelector to convert the selectos into a NSString, and the print the object:

NSLog(@"%@", NSStringFromSelector(selector) );

Or print it directly with good, old-fashioned C:

NSLog(@"%s", selector,);

457Singleton Classes and Shared Resources in Objective-C

Resource.h

#import 

@interface Resource : NSObject {
    float myValue;
}

@property float scale;

+ (id)shared;

@end

 

Resource.m

#import "Resource.h"

@implementation Resource

@synthesize scale;

- (id)init {
    scale = 1.0f    // my default value
}

+ (id)shared {
    static Resource *sharedResource = nil;
    if (!sharedResource) {
        sharedResource = [[self alloc] init];
    }
    return sharedResource;
}
@end

An now it can be used like the following:

Resource *r = [Resource shared];
NSLog(@"Resource Test: %f", [r myValue]);
// Resource Test: 1.000000

[r setMyValue:2.0];
NSLog(@"Resource Test: %f", [r myValue]);
//# Resource Test: 2.000000

Resource *s = [Resource shared];
[s setMyValue:2.111];
NSLog(@"Resource Test: %f", [s myValue]);
// Resource Test: 2.111000

NSLog(@"Resource Test: %f", [r myValue]);
//Resource Test: 2.111000

Note that the values stays constant, even if another instantionation occurs. I guess that's what Singleton classes are all about.

Adapted from Scott Stevenson's fantastic introduction at Cocoa Dev Central.

And more tutorials from Scott Stevenson... Cocoa Dev Central: Learn C for Cocoa Cocoa Dev Central: Learn Objective C

452Padding with Zeros (or other characters)

int x = 11;

[NSString stringWithFormat:@"%03i", x];
// @"011"

[NSString stringWithFormat:@"%+5i", x];
// @"+++11"

[NSString stringWithFormat:@"%+05i", x];
// @"+0011"

And not really like that. 381. I'll still have to learn a lot of C.

447Stripping out NSLog() from the Release Build

Add the the *_Prefix.pch file of your projects. Strips out NSLogs, when optimized (=Release Build)

Very nice solution, thanks to Marek Bell

// strip out NSLog on optimize
#ifndef __OPTIMIZE__
#    define NSLog(...) NSLog(__VA_ARGS__)
#else
#    define NSLog(...) {}
#endif

445Creating a Folder

Check whether a folder exists, otherwise create it.

NSFileManager *fm = [NSFileManager defaultManager];

if (![fm fileExistsAtPath:myPath isDirectory:NULL]) {
        [fm createDirectoryAtPath:myPath withIntermediateDirectories:NO attributes:nil error:nil];
}