views:

702

answers:

7

I just discovered the NSRect helper functions in NSGeometry.h (i.e. NSMidX, NSMaxX, etc...)

These would have made some repetitive coding much easier. I knew about NSMakeRect, NSMouseInRect, NSOffsetRect and many others but somehow missed the functions that aid in recalculating NSRect geometry.

+4  A: 

This is one that I wish I had known about 6 months ago. I was creating our first iPhone application and I wanted to create a simple help file that was based on HTML using the UIWebView Controller.

However I could not figure out how to embed local images that I had stored in the Bundle and I did not want the user to have to have internet access to fetch the images from a server.

Little did I know I could do the following to get images from the Main Bundle

NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSURL *bundleBaseURL = [NSURL fileURLWithPath: bundlePath];

[webView loadHTMLString:htmlContent baseURL: bundleBaseURL];

The Image in your HTML can then call local images directly.

<img src="yourImageFromTheMainBundle.jpg" />

I had no idea I could set the baseURL with the location of the Bundle.

Niels Hansen
+10  A: 

I've found NSStringFrom*() helpful when logging structs like CGRect, CGPoint, etc.

You can find a comprehensive overview at Apple's Foundation Functions Reference.

Jason Medeiros
Oh man... I had recreated several of those. Good answer to a good question!
Felixyz
+2  A: 

Much of the stuff in NSPathUtilities.h. I did know about it two years ago, but when I first found it I wished I’d seen it two years earlier. :-)

At some point I wasted quite a bit of time because I didn’t know about NSCountedSet, and made a mess of my dictionary-based replacement. I know of several cases where people have done the same sort of thing because they didn’t know about NSSet at all. Another good “hidden” collection is CFBinaryHeap, which implements a priority queue, but doesn’t have an NS equivalent.

Ahruman
+3  A: 

One I remember

+ (NSBezierPath *)bezierPathWithRoundedRect:(NSRect)rect xRadius:(CGFloat)xRadius yRadius:(CGFloat)yRadius

Granted figuring out how to draw a rounded rectangle manually is a pretty good exercise. There are others that I am just so used to now.

Colin Wheeler
A: 

This is a shortcut instead of a library call that I missed, but it is in the spirit of the thread.

One shortcut that I use alot is using an inline format statement in NSLog calls.


NSLog(@"x=%@", [someobject className]);

instead of the more verbose


NSLog([NSString stringWithFormat:@"x=%@", [someobject classname]]);
Mark Thalman
That's not a shortcut; that's the only correct way to do it. If the result of stringWithFormat: (or whatever string you pass to NSLog) ends up containing, say, “%@”, then NSLog will try to insert an object there—but you didn't pass one. So it takes a garbage pointer, sends it a message, and boom.
Peter Hosey
+2  A: 

Creating a colour from a pattern image with

[UIColor colorWithPatternImage:[UIImage imageNamed:@"mypattern.png"]];
nduplessis
Mark Thalman
+1  A: 

Helper function to draw three part images with left cap, fill and right cap. Ideal for custom buttons

void NSDrawThreePartImage(NSRect frame,
    NSImage *startCap,
    NSImage *centerFill,
    NSImage *endCap,
    BOOL vertical,
    NSCompositingOperation op,
    CGFloat alphaFraction,
    BOOL flipped
);

Also look for NSDrawNinePartImage

Erik Aigner