bob

bob

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Print
  • Reddit
  • StumbleUpon
  • Technorati
  • TwitThis
  • Wikio

NSMutableArray with weak references

Here’s a category that permits to create an NSMutableArray that has weak references. This is useful when making arrays of delegates and you don’t want to retain/release them (preventing circular references).

@implementation NSMutableArray (WeakReferences)
+ (id)mutableArrayUsingWeakReferences
{
	return [self mutableArrayUsingWeakReferencesWithCapacity:0];
}
 
+ (id)mutableArrayUsingWeakReferencesWithCapacity:(NSUInteger)capacity
{
	CFArrayCallBacks callbacks = {0, NULL, NULL, CFCopyDescription, CFEqual};
	// We create a weak reference array
	return (id)(CFArrayCreateMutable(0, capacity, &callbacks));
}
@end

This is simple but not very well documented and many people still don’t know about it. The workaround was really heavy and prone to errors. Note that the resulting NSMutableArray keeps all functionalities (fast enumeration, description, …). It just doesn’t retain/release its members.

A similar method exists already on the Mac, but is not included in iPhone for reasons I don’t really know/understand.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Print
  • Reddit
  • StumbleUpon
  • Technorati
  • TwitThis
  • Wikio

iPhone 3.0 Debugging made easier

Nice addition there. When an exception is thrown, in the simulator (didn’t try on the device), objc_exception_throw now calls __TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION__ directly.

What this means is that you don’t have a start fiddling with GCC prompts to get to the exact point the exception was thrown. In 2.2.1 and before, the exception was genuinely thrown and caught in the main loop, and you did lost your stack.

This is really useful when sending messages to objects that don’t handle them, since you can get up the stack and look at what happened exactly.

Anyone has seen other major improvements to debugging in the 3.0 beta-world?

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Print
  • Reddit
  • StumbleUpon
  • Technorati
  • TwitThis
  • Wikio

Convert a CGColorRef to another CGColorSpaceRef

It is not documented and officially there is no way to convert a CGColor from a CGColorSpaceRef to another. Therefore, if you have two colors from a different colorspace, you’re kind of stuck. Unless you go ColorTransform, but then you’re on your own (undocumented CoreGraphics exports, which means some Reverse Engineering and assembly to do – I’ll go there for you if I feel like it someday).

The problem is more common than it seems: [UIColor whiteColor] gets you a color in the white colorspace, while [UIColor greenColor] gets you one in… the RGB colorspace. Now if you try to create a CGGradientRef from those two, you’ll have a problem. CGGradientRef does not convert any of those two to its own CGColorSpaceRef.

The only way I’ve found to do that is to use an undocumented method: styleString. It returns a parse-able NSString* that can be used to create a RGB based UIColor.

Here’s some code in an easy-to-use category to you code-lubbers:

 
 
@interface UIColor (colorSpaceConversionCategory)
// Transfer the UIColor to another UIColor that is guaranteed to be in the RGB
// colorspace.
- (UIColor*)rgbColor;
@end
 
@interface UIColor (UIColorUndocumentedCategory)
// Undocumented API. Remove the warning from the code below.
- (id)styleString;
@end
 
@implementation UIColor (colorSpaceConversionCategory)
- (UIColor*)rgbColor
{
  // Call to undocumented method.
  NSString*style = [self styleString];
 
  // Remove the "rgb(" prefix and the ")" suffix.
  style = [[style substringToIndex:style.length - 1] substringFromIndex:4];
  // Split the components.
  NSArray* rgb = [style componentsSeparatedByString:@","]; 
 
  CGFloat red   = [[rgb objectAtIndex:0] floatValue] / 255.0f;
  CGFloat green = [[rgb objectAtIndex:1] floatValue] / 255.0f; 
  CGFloat blue  = [[rgb objectAtIndex:2] floatValue] / 255.0f;
  CGFloat alpha = CGColorGetAlpha([self CGColor]);
 
  return [UIColor colorWithRed:red 
                         green:green 
                          blue:blue
                         alpha:alpha];
}
@end

There you go. This code is ugly as hell but works. Now calling [[UIColor whiteColor] rgbColor] returns a color in the RGB colorspace. Whatever the source colorspace.

This might not work in future versions of the SDK. I’ll post back if it changes.

/poltras

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Print
  • Reddit
  • StumbleUpon
  • Technorati
  • TwitThis
  • Wikio

Microsoft invents the OSX dock

Eight years later. In a blog post, and without even mentioning OSX as an inspiration, Microsoft created what is their equivalent of the dock – namely that the taskbar now uses icons avatar of your apps, offers the option to pin them there so you can click on them when the app is not running, and application feedback embedded in the icon itself, three points that the Dock was superior with and that weren’t there in the Taskbar. Now, give it magnification and some stacks, and you can change the name for “Dock”…

What’s next? Saying all Windows applications are singletons and getting the One-Document-One-Window concept right? Unsure about this, maybe in Windows 8…

I don’t think Mac OSX has everything right the first time, but most of the concepts in the Apple’s operating system are right the first time, very intuitive and just makes sense. They polished some over time, and look where they are – a solid consistent and intuitive operating system, overall. But I don’t consider myself a blinded Apple devotee.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Print
  • Reddit
  • StumbleUpon
  • Technorati
  • TwitThis
  • Wikio

Cornerstone Vs Versions

Versions beta is off. It is time for me to chose a SVN client for work.
Read the rest of this entry »

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Print
  • Reddit
  • StumbleUpon
  • Technorati
  • TwitThis
  • Wikio

Adding pictures to the Simulator

There is a limitation with Quartz when dealing with pictures. This problem is undetectable from the simulator, but its solution should be testable in it.

Read the rest of this entry »

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Print
  • Reddit
  • StumbleUpon
  • Technorati
  • TwitThis
  • Wikio

You Are Elle

I like codinghorror for two main reasons:

  • his insights are insightful, and
  • his code is horrific. In the problematic sense of the term

Seriously, reading this here makes me wanna puke. Bad solution for the wrong problem.

Read the rest of this entry »

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Print
  • Reddit
  • StumbleUpon
  • Technorati
  • TwitThis
  • Wikio

Undocumented UIImage resizing

Last week, I was trying to resize an image in the iPhone SDK. Those who don’t know anything about Objective-C and are not interested should stop reading right… here.

Read the rest of this entry »

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Print
  • Reddit
  • StumbleUpon
  • Technorati
  • TwitThis
  • Wikio

First steps with the google droids

I don’t know if it’s Android itself, or just the Mac implementation of their emulator, but I have a hard (read real hard) times with the Android SDK, and precisely their emulator. Seriously, a lot of stuff just doesn’t work at all.

To make matter worse, being in Canada, we won’t ever see a G1 and the next device with Android on it will take forever to get out of prototype.

Anyone has had similar issues? Like trying to open the messaging application just crash when adding a target? Or most development tools throw exceptions (lovely exception browser btw… what’s wrong with a console?). Any developper with device hands-on experience?

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Print
  • Reddit
  • StumbleUpon
  • Technorati
  • TwitThis
  • Wikio
←Older