1

I'm working with CocosBuilder 2.1 and Cocos2d-iPhone 2.0. I've gotten CocosBuilder to compile, and I'm having a weird problem when using their one-text-label example in my project.

Here's the code in question, from CCBReader.m line 823:

Class class = NSClassFromString(className);
if (!class)
{
    NSLog(@"CCBReader: Could not create class of type %@",className);
    return NULL;
}

This fails with the text "Could not create class of type CCLabelTTF". But if I change the code like this:

Class class = NSClassFromString(className);
if (!class)
{
    CCLabelTTF* tempLabel = [[CCLabelTTF alloc] init];
    [tempLabel release];
    NSLog(@"CCBReader: Could not create class of type %@",className);
    return NULL;
}

It works. I don't see anyone else having problems with CocosBuilder in this spot, so what's going on?

The weird thing is that this change can only be affecting it at compiler level, because the added code is inside the error segment, right?

Almo
  • 15,538
  • 13
  • 67
  • 95
  • className is an NSString*. – Almo May 10 '13 at 13:19
  • No - what are the *contents*? – Undo May 10 '13 at 13:20
  • You can't do that. NSStrings can only be created as pointers. – Almo May 10 '13 at 13:20
  • 2
    Well, if the output I get is "Could not create class of type CCLabelTTF", then the contents must be "CCLabelTTF", right? Or am I missing something... – Almo May 10 '13 at 13:21
  • Look at my profile. You will see that I have a very high accept ratio. I am still working on this problem. One answer is causing other problems, and the other doesn't work because the class name is unknown and dynamically loaded from a file. I will accept an answer once the problem is solved. – Almo May 10 '13 at 13:28

2 Answers2

3

because you did not use CCLabelTTF at all in you project, so the runtime did not load the class for you.

it works after you did the hack because your project now do use the CCLabelTTF class so the runtime will load it.

to solve this problem, add -ObjC to your linker flag, check details in following links

http://developer.apple.com/library/mac/#qa/qa1490/_index.html https://stackoverflow.com/a/2615407/642626

Community
  • 1
  • 1
Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
  • This is causing a load of other linker errors... working on it. – Almo May 10 '13 at 13:30
  • looks like some other libraries are not included in the build. I may just need to include the OpenAL library or something. – Almo May 10 '13 at 13:31
  • Example: "_alGetListenerf", referenced from: -[CDSoundEngine masterGain] in libCocosDenshion.a(CocosDenshion.o) – Almo May 10 '13 at 13:33
  • Ok, just needed to include some frameworks in the main project. I'm linking to Cocos2d as a static lib, so the -ObjC flag requires all the refereneced frameworks. Thanks. – Almo May 10 '13 at 13:37
0

from the apple documentation:

The class object named by aClassName, or nil if no class by that name is currently loaded. If aClassName is nil, returns nil.

Either you variable "className" is nil, or class was not loaded in runtime before this call. Try to force load this class with this:

[CCLabelTTF class];

anywhere in the code.

For future: try searching your question before creating new one.

Community
  • 1
  • 1
art-divin
  • 1,635
  • 1
  • 20
  • 28
  • I did search, and I found the link you posted. It did not answer my particular question. – Almo May 10 '13 at 13:29
  • I cannot do [CCLabelTTF class] because the className comes from a file and I don't know which class it will be. – Almo May 10 '13 at 13:29
  • Then specify this information in your question, the other answer might be helpful then – art-divin May 10 '13 at 13:31
  • If I could just put CCLabelTTF, why would I have made it a string, className? – Almo May 10 '13 at 13:34
  • because, there are some situations in which you have to use NSString instead of Class – art-divin May 10 '13 at 13:37
  • @art-divin: `NSString *` and `Class` are two different types that refer to two different things. Moreover, there's very little you can do with a class name (string) itself; for it to be useful, you have to resolve it to a class, at which point you have a `Class`. – Peter Hosey May 10 '13 at 20:48
  • `[CCLabelTTF class]` doesn't force the class to be loaded; dyld loads everything from each executable it loads. The problem is that if you leave it out, the linker may not even link that class into the executable, so the class *really does not exist* in the final build product. (I'm not sure whether this affects applications themselves or only static libraries, but if the class was meant to be in a lib and isn't, it'll be missing from the app, too.) The solution, as xlc stated in their answer, is the `-ObjC` flag, which tells the linker to include everything, even if it seemingly isn't used. – Peter Hosey May 10 '13 at 20:52