5

Screenshots

I'm having quite a hard time setting up a category on a class I made. From what I've read, Objective-C allows you to create a category on any class, not just closed-source ones. (It honestly wouldn't make sense any other way.)

Of course I can add the category messages to the actual class file, but I want to keep them separate (as the category is an uncommonly special use of a class that can be used very generally). I want to share the class, but keep the category private... anyway.

I've stripped down the category to just show the issue at hand. I (currently) get four errors on the first category message. The number of errors I receive on that line is directly proportional to how many times it is references, but it is not an even rise. Does anyone know what could be causing this?

11
  • What you've written looks correct to me. I've done similar things in my code. My guess is that maybe this is some other issue that's confusing the compiler. Does "ByteCollection.h" have a proper @end in it, for example? Or is there anything else that could be wrong with either "ByteCollection.h" or anything it includes? Commented Jun 13, 2012 at 20:47
  • Not that I can see; Xcode doesn't point out anything and I implement the entire interface. What exactly dictates a 'proper @end'? Commented Jun 13, 2012 at 20:52
  • 3
    Another possibility is that the compiler isn't finding "ByteCollection.h" when you import it. If that file's in a different folder (in the filesystem, not in Xcode's yellow-folder "groups"), you may need to set up search paths in the project settings so it can be found. Commented Jun 13, 2012 at 20:52
  • Also, I'm pretty sure it's nothing to do with the cause of your problem, but by convention ObjC category names are capitalized. Commented Jun 13, 2012 at 20:54
  • Capitalization: fixed - thanks ;) Also, all source files are in the same folder (flat). But, I am wondering if that could be the issue. I've previously had problems with Xcode simply not importing files that I ask it to import. Does anyone know if there is a surefire way to determine this? Commented Jun 13, 2012 at 21:04

1 Answer 1

6

Your Resources.h file, which is imported by ByteCollection.h, imports ByteCollection+words.h. So when ByteCollection+words.h imports ByteCollection.h, this results in a circular dependency†. The simplest way to break a circular dependency is to move one of the imports to the implementation file rather than the header. It looks like this should be possible with Resources.h.

† You might be wondering, why is it a problem if you have a circular dependency? Well, the #import directive literally just textually includes the file you specify, just like if you copy-pasted. It also intelligently doesn't include a file twice, because that would create duplicate code. But this means that when File A says "I want File B to go before me" and File B says "I want File A to go before me," one of them is going to be disappointed, and that leads to errors like the one you're getting here.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, it does import ByteCollection+Words.h, but I've had issues with circular inclusion before and will never scratch my head for that mistake again! XD There are no circular inclusion issues. I never even import Resources.h anywhere in my source files. (Used to, but not anymore.) I even commented out the specific import for good measure with no effect. Thanks though :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.