How to use a NSDictionary and how to make ARC happy



I’ve read the errata which says:

»(page 578) The use of the CountAndSet struct to return a pair of values from a method will fail with ARC. Change it to return a dictionary instead. (which is what it really should have done in the first place. mea culpa)«


Could you please elaborate how you’d do that exactly? IIRC you can’t just put a NSUInteger into a NSDictionary (how about NSNumber?).

Also, ARC isn’t really happy with:

@property (assign) IBOutlet NSWindow *window; @property (assign) IBOutlet NSTextView *wordsView; @property (assign) IBOutlet NSTextField *countLabel; @property (assign) IBOutlet NSTextField *uniqueLabel; @property (assign) IBOutlet NSButton *countButton; @property (assign) IBOutlet NSProgressIndicator *spinner;

Ditching assign seems to make ARC happy though (what’s the impact going from weak to strong btw?)



I’ve updated the sample code to support ARC, including this example. You can get it from

I ended up making a method that returns an int and the counted set by reference:

  • (void) getWordCount: (NSUInteger *) count
    andFrequencies: (NSCountedSet **) frequencies
    forString: (NSString *) string {

And ditched the struct altogether.

But yes, to use a dictionary, you’d wrap the NSUInteger into an NSNumber and put it in there.

For this example, going from weak to strong is OK. If your deployment target is 10.7 or beyond, you can use weak references for everything except the text view. By default desktop OS X has weak references to views in the nib, as opposed to iOS which has strong references (at least until the latest Xcode versions, where it flips the other way :neutral_face: ). When you have setters/getters, the nib loading machinery will vector through those, so you can get whatever memory management semantics you want. Just remember to release references to stuff in the nib (or nil them under ARC) if you’re going to be wanting to free the stuff loaded from the nib.



Thanks for updating the sample code markd2!

Now all we need is a working link :slight_smile:

$ wget --2012-07-22 11:44:52-- Resolving Connecting to||:80... connected. HTTP request sent, awaiting response... 404 Not Found 2012-07-22 11:44:52 ERROR 404: Not Found.


D’OH!! Fixed. (one of those days :slight_smile: )