views:

177

answers:

2

I'm trying to come up with a way to make it easy to switch out our "mock" data services and our live ones. Basically, we'll have live servers with real web services, but for whatever reason, a developer may want to load data from static files (file urls).

I figured I would solve this problem by creating categories that override the methods that fetch the data, thus leaving original code untouched (it has no concept of the "mock" data). I don't want to litter my code with #ifdef.

I can put an #ifdef at the very beginning of each file that has categories in it, and I can set a custom flag in the configuration settings, but I'd rather just have a way to include or exclude the files depending on the configuration. Is that possible? How do you solve this problem?

A: 

I would recommend creating two targets one of which has the mock categories included and another one which does not.

When you want to test, just build the target containing the mock categories. Everything else can remain identical.

TechZen
Can you base on target on another? I worry that people will forget to add new files to both (or multiple) targets. Also, we are already using targets for something else (iPad vs iPhone) -- ideas?
Sean Clark Hess
I think cdespinosa's suggestion to customize the build scripts is the only way to make it 'automatic' in some sense. If you use a naming convention for the files a script should be able to include or exclude them. However, you will at some point in your process always have to have manual step that indicates to Xcode what files go in which target. The linker can't read your mind. ;-)
TechZen
+4  A: 

See http://lists.apple.com/archives/xcode-users/2009/Jun/msg00153.html

The trick is to define EXCLUDED_SOURCE_FILE_NAMES in the configuration you want to exclude the files from, and set the value of that custom build setting to a list of the file names (or a pattern that matches those, and only those, file names).

cdespinosa
Bingo. Make sure to document this carefully, as it's something that's not at all obvious to someone who isn't familiar with XCode at that level of detail. And note that INCLUDED_SOURCE_FILE_NAMES overrides EXCLUDED_SOURCE_FILE_NAMES.
Stephen Canon