As I understand your problem you have failed to check the result when loading an image, font or something similar and this is causing an error when the bad result is later inserted into a dictionary. What you are after is a quick way, as you have a large codebase, to track down that insertion so you can backtrack and find the source of the problem and add appropriate checking code to the load/whatever.
What you can do is:
Replace NSMutableDictionary
with a simple class, say DebuggingDictionary
, which appears to be (explained below) a derived class and just checks for nil
on insertion and produces the diagnostics you are after; and
Do a find/replace over your code base for [NSMutableDictionary alloc]
and replace with [DebuggingDictionary alloc]
. You can easily change this back once the problem has been fixed.
So how to write DebuggingDictionary
?
Well as NSMutableDictionary
is a class cluster you cannot just derive from it and override setObject:forKey:
, you have provide your own storage for the keys & objects and override six key methods and all (or at least all you use) of the init methods.
Sounds bad but it isn't. First read this answer to a different but related question. In that answer a version of NSMutableArray
is created which checks the type of elements added, you need to check whether the items are nil
. The implementation provides the storage by wrapping a real NSMutableArray
. You can do the equivalent with NSMutableDictionary
, the documentation (NSMutableDictionary
and NSDictionary
) lists the six primitive methods you need to override.
That answer also adds its own initWithClass:
initialisers and blocks the standard ones, you just need to implement the standard dictionary ones - by calling them on the wrapped dictionary.
[Minimal checking in the following code sketches, all typed directly into answer so beware of typos]
So for example initWithCapacity:
becomes something like:
- (instancetype) initWithCapacity:(NSUInteger)numItems
{
realDictionary = [NSMutableDictionary dictionaryWithCapacity:numItems];
return self;
}
and the core insertion method becomes:
- (void)setObject:(id)anObject forKey:(id<NSCopying>)aKey
{
if (anObject == nil)
{
// produce your diagnostics
}
else
realDictionary[aKey] = anObject;
}
Once you've tracked your problem to its source and fixed it there just remove your DebuggingDictionary
and find/replace all occurrences in your code with NSMutableDicitionary
.
HTH