Вопрос

I have created an NSObject subclass without any implementation and named it "WebView". In my code I need to be able to dynamicly instantiate objects of this class using NSClassFromString.

While it seams like a trivial work, I am having problems with this specific class name "WebView". It seams like NSClassFromString() is failing for this specific class name, but with lldb I can print both class1 and class2 descriptions and for both it says "WebView".

Anyone else experiencing same issue? Is there some list of forbidden class name like it is for instance variables etc?

@interface WebView : NSObject
@end

@implementation WebView
@end

@implementation ViewController
- viewDidLoad {
    Class class1 = [WebView class];
    Class class2 = NSClassFromString(NSStringFromClass(class1));

    NSAssert(class1 == class2, @"Classes not equal"); // FAIL
}
@end

And another example:

[class1 new]; // OK
[class2 new]; // EXC_BAD_ACCESS (code=2, address=0x0)
Это было полезно?

Решение

enter image description here

When I try to allocate the WebView. It looks into apple's internal framework as shown in image.

Другие советы

You should always use a three letter prefix for your classes to avoid conflicts like this. Apple reserves all two letter prefixes for itself.

When creating your project in Xcode, it will even ask you what prefix you would like to use and it will create new classes using that prefix for you.

WebView is the instance apple sdk class. When i used UIWebView i was seen webView class how ancestor UIWebView. So, do not use class with name WebView, this name use the apple.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top