|
From: | Ivan Vučica |
Subject: | Re: Fast enumeration |
Date: | Fri, 4 Feb 2011 18:37:27 +0100 |
On 4. vel. 2011., at 16:31, Jens Ayton wrote:
If someone has a clear answer, this is something that would interest me as well.
With regards to the runtime support, I believe you can test at -- runtime. Attempting to enumerate over an NSString throws this exception under Cocoa runtime: 2011-02-04 18:18:58.003 test[2804:903] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x7fff710f2170' which leads me to believe you could test for countByEnumeratingWithState:objects:count: selector. However, this does not resolve the issue of testing this kind of stuff at build time. I have figured out a way you might be able to resolve this. A "configure" step in the build process. (If you did not using autotools, you might write a manual "configure" script which would perform this test.) Test program would simply iterate over an empty NSArray using for(x in y) syntax. If either compilation or running of the program were to fail, then the fast iteration is not supported. Here's a simple configure script I threw together under Cocoa environment. For GNUstep, I'd probably go with outputting a GNUmakefile as well. instead of just calling the compiler directly. #!/bin/bash cat << _END > /tmp/__configure_test.m #include <Foundation/Foundation.h> int main (int argc, char**argv) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSArray *testArray = [[NSArray alloc] init]; for(id i in testArray) { // nothing } [pool release]; return 0; } _END echo "#ifndef CONFIG_H_INCLUDED" > config.h echo "#define CONFIG_H_INCLUDED" >> config.h echo "==> Testing fast iteration..." if gcc -x objective-c /tmp/__configure_test.m -framework Foundation -o /tmp/__configure_test && /tmp/__configure_test; then echo "#define HAVE_FAST_ITERATION 1" >> config.h echo "====> Pass" else echo "#define HAVE_FAST_ITERATION 0" >> config.h echo "====> Fail" fi echo "#endif" >> config.h I did something similar for testing whether or not NSViewController exists (it didn't in older GNUstep releases, such as the one in Debian). If anyone has a better way to test for fast iteration, I'm all ears. ;-) |
smime.p7s
Description: S/MIME cryptographic signature
[Prev in Thread] | Current Thread | [Next in Thread] |