-2

I want get something like:

#define weaken(object) ...

----

ClassABCD * abcd = [ClassABCD new];
weaken(abcd);
weakAbcd.tag = 0;

----

I have some code below:

#define weaken(x) __weak typeof(x) weak##x = x

But it only can use "weakabcd", and not "weakAbcd". Any idea why, and how to fix it?

  • Are you trying to create an instance of some class by passing a string? That's why I'm interpreting from the subject line – Chris Dec 30 '14 at 16:20
  • I tried but failed..So I've come here to ask – ge chen Dec 30 '14 at 16:37
  • I don't know whether you can do what you want (a macro that capitalizes the first letter before prefixing it with `weak`), but you could always contemplate a different implementation, the `weakify` macro: http://stackoverflow.com/a/27280374/1271826 – Rob Dec 30 '14 at 16:45

2 Answers2

0

Based on the question title, NSClassFromString() will do it. Based on the code you offer, I'm not so sure the two match.

This is a line from one of my projects where I am creating a UITableViewCell subclass instance based on the string variable prefCellClassName that I define elsewhere. (For completeness, prefCellStyle and prefCellReuseIdentifier are also variables that I define elsewhere.)

        cell = [[NSClassFromString(prefCellClassName) alloc] initWithStyle:prefCellStyle reuseIdentifier:prefCellReuseIdentifier];
Brad Brighton
  • 2,179
  • 1
  • 13
  • 15
0

To make a class from a string:

id someObject = NSClassFromString("MyClassName");

When you do something like this, it's always good to check if your object responds to the method you're trying to pass it like so:

if ([someObject respondsToSelector:@selector(someMethod)])
{
    [someObject someMethod];
}

It's features like this that make objective-c the dynamic language that it is

Chris
  • 7,270
  • 19
  • 66
  • 110