December 2011
1 post
1 tag
Getting the IMP of the current method
Once in a very great while, you come across a weird situation where you want to know the IMP of the current method. I’ll just say right now that if you think you need this, you probably don’t. That’s how rare this situation is.
Regardless, it’s an interesting question. How do you get the function pointer for the currently-executing method?
A few years ago, Matt...
July 2011
1 post
1 tag
Dumbfounded
I was just shown an NSPredicate that dumbfounded me for several reasons:
It’s using five nested SUBQUERY expressions. In other words, a SUBQUERY within a SUBQUERY within a SUBQUERY within a SUBQUERY within a SUBQUERY.
It works
It’s reasonably fast
Core Data correctly handles it on a SQLite store
It’s the only way the author could accomplish what he needed in a...
June 2011
1 post
2 tags
Parsing Mathematical Expressions
A while ago, I was looking for a library to parse NSString objects as mathematical expressions. I wanted to be able to take a string like @"2+3*4" and get back 14. I found two easy ways to do this: GCMathParser and NSPredicate (of course!).
GCMathParser is a library by Graham Cox for parsing strings as equations. It uses a parser generated by BISON as the primary engine, with a...
April 2011
1 post
1 tag
Subclassing NSInputStream
Subclassing NSInputStream is a problem that has been plaguing Cocoa developers for a long time. Googling “subclass nsinputstream” reveals that people have been having issues with this for six or seven years. While the documentation seems to indicate that it should be fairly straightforward, actually trying to do so reveals several hairy spots.
For example, if you try to use a...
March 2011
1 post
1 tag
How to learn Cocoa
This is something any experienced Cocoa developer has been asked at least half a dozen times:
“What’s the best way to learn Objective-C/Cocoa?”
I have two answers to this question:
Be curious
Don’t be satisfied
Be Curious
Curiosity is the heart of learning anything. If you can cultivate a deep curiosity about the Objective-C language and the Cocoa frameworks, then...
February 2011
2 posts
1 tag
How I do my singletons
This is something that comes up again and again. How do you make a true singleton in Objective-C? I was just asked this the other night, so I decided to write up my version of an Objective-C singleton.
For starters, there’s an example in the documentation on how you could make a singleton. It’s a pretty bizarre implementation.
StackOverflow.com has a nice long discussion on what...
heapify asked: Why do Strings in Objective-C have to be preceded with an @?
January 2011
4 posts
mahmoudhossam-deactivated201107 asked: Since this is an Obj-c blog,I have a question for you
why would they make another C with OOP,what's wrong with C++?
why would they make another C with OOP,what's wrong with C++?
cbarrett asked: I've really been enjoying your posts, but you don't have any personal information on your site, as far as I could find. Who are you?
1 tag
Using custom functions with NSExpression
There are a bunch of built-in functions in the NSExpression class that allow you to do some pretty neat stuff. They are: average:, sum:, count:, min:, max:, median:, mode:, stddev:, add:to:, from:subtract:, multiply:by:, divide:by:, modulus:by:, sqrt:, log:, ln:, raise:toPower:, exp:, ceiling:, abs:, trunc:, random, random:, now, floor:, uppercase:, lowercase:, bitwiseAnd:with:, bitwiseOr:with:,...
What the heck is SUBQUERY?
One of the lesser known bits of NSPredicate is the SUBQUERY() function. The documentation for a subquery expression explains a little bit about what’s going on, but it takes a while to understand when it’s really useful.
Let’s break it down.
The Syntax
A subquery expression takes 3 arguments:
A collection
A variable name
A predicate
The Collection
The collection can...
November 2010
8 posts
1 tag
Creating an advanced NSPredicateEditorRowTemplate
In a recent post I covered how to make a simple NSPredicateEditorRowTemplate. In this post we’re going to go a bit deeper.
The Setup
In our last example, we did some simple date comparisons, where we were comparing an NSTimeInterval against a property that represented a duration. For example, if we had an array of Song objects, then we could use that row template to construct a...
2 tags
Mathematically evaluating NSStrings
I’ve recently posted the code to a project I’ve written called DDMathParser. It’s a way whereby you can take an NSString and evaluate it as a mathematical expression. For example, you can do:
NSLog(@"%@", [@"1 + 3 * 4 - 5" numberByEvaluatingString]); //logs "8"
There are several other ways to evaluate strings, including methods that allow you to use variables (1 + $a, and...
1 tag
Creating a simple NSPredicateEditorRowTemplate
NSPredicateEditor is a great class, provided you figure out how it works. There’s a fair amount of documentation as to what’s going on, but if you ever want to do something beyond what’s possible through configuration in Interface Builder, you’re probably going to spend several hours playing with things until you get it right.
Hopefully, I can help cut down that...
2 tags
Abusing NSPredicate
NSPredicate is a nifty class. It’s intended use is to be an objected-oriented way of expressing a truth statement. We see this being used with things like -[NSArray filteredArrayUsingPredicate:], -[NSFetchRequest setPredicate:], and more. NSPredicate, however, can offer us a lot more.
There are 2 things that make NSPredicate so insanely awesome:
Its string parser
The evaluation power...
1 tag
Defining custom key path operators
(This was prompted by a recent question on StackOverflow.)
Key-value coding is one of my more favorite things about Cocoa (up there with NSPredicate). Basically, it allows you to access properties of objects by name (ie, as a string) rather than invoking a method (er… sending a message) directly.
Along with accessing properties, you can (with some limitations) retrieve calculated values...
1 tag
Mike Ash: Creating Classes at Runtime →
Another great article by Mike Ash that’s an expansion on my earlier article about dynamic subclassing.
2 tags
Followup to Localizing NSPredicateEditor
To expand a little bit on my previous post, here’s a little bit more information:
If you ever forget the format to put in the NSLocalizedString() macros, you can do the following:
predicateEditor = ... //an NSPredicateEditor
NSData * stringsData = [predicateEditor _generateFormattingDictionaryStringsFile];
NSString * strings = [[NSString alloc] initWithData:stringsData...
2 tags
Localizing NSPredicateEditor
Many people who know me personally (at least as a developer), know something interesting about me: I love NSPredicate. I love it so much that I stood up and blathered for an hour on how awesome it is. I won’t go into that here.
One of the things that make NSPredicate superbly awesome is the NSPredicateEditor. NSPredicateEditor, in a nutshell, is a way to visually build an NSPredicate,...
October 2010
3 posts
1 tag
Method Swizzling
Recently I wrote about how to dynamically subclass objects in Objective-C, and when that might be useful.
While dynamic subclassing can be really useful, there is one major gotcha that can make it not very effective, and that was hinted at the end of my post: Key-Value Observing.
When you add an observer to an object, the KVO mechanism will dynamically subclass that object to implement the...
1 tag
Dynamic Subclassing
If you’ve seen my github.com page or my portfolio link, you’re probably aware of a project of mine called CHLayoutManager. CHLayoutManager, for those of you not in-the-know, is a way to define the layout of a user interface on the Mac via constraints (as opposed to autoresizing masks). For example, if you have two buttons “A” and “B”, you can say “I want...
1 tag
Drawing a gradient-filled rounded rect with a drop...
This is something I’ve been fiddling with for the past few hours trying to get to work right. I tried doing stuff with CoreGraphics, drawing paths, drawing images, and so on, but it never quite worked correctly. Then I stumbled upon this post over at cimgf.com, which helped me figure it out.
In a custom UIView subclass, I have this:
- (void) awakeFromNib {
//turning off bounds...
April 2009
1 post
1 tag
Aspect Oriented Programming
Recently in a CS class of mine, we briefly discussed Aspect Oriented Programming. The idea intrigued me, and knowing some of the capabilities of the Objective-C runtime, I decided to play around with it and see what I could do.
I first needed a good aspect that I could work on. After a bit of thought, I decided that NSCoding was a good candidate. Usually, Cocoa developers implement NSCoding...