On Mar 6, 2014, at 12:21 PM, William Squires wrote:

>  Given an object, and a method within, is there some way to get the name of 
> the class of the object as an NSString?

Well, you could do NSStringFromClass([self class]) but that gives the name of 
the object's dynamic class, which is not necessarily the same as the class in 
which the method is implemented.

>  For that matter, what I want to do is something like this:

> -(void)myMethod
> {
> NSString *myClassName = ???; // What can I put here besides a literal 
> @"MyClass"?
> 
> NSString *fooText = [NSString stringWithFormat:@"<%@> -(void)myMethod", 
> myClassName];
> NSLog(fooText); // Yellow triangle on this line
> }

Are you aware of the __func__ (or __PRETTY_FUNCTION__) compiler-defined 
variable?  It is a C string whose content is "-[MyClass myMethod]".  (In a 
function, it will be the function name rather than the method name.)

So, you could do:

NSLog(@"%s", __func__);


>  Also, when I do this (using a literal NSString constant for myClassName 
> above), Xcode marks the line with NSLog with a yellow triangle, and 
> disclosing it says something about passing an NSString instance as being 
> "unsecure". Can this warning be turned off? It seems silly to do:
> 
> NSLog(@"%@", fooText);
> 
> just to avoid this warning.

It almost certainly can be turned off.  Usually, the warning line from the 
compiler explains which warning option enabled the particular warning and you 
can turn it off by insert "no" in a compiler option.  However: 1) it's a 
valuable warning, and 2) you're already using a format string to construct 
fooText.  Why not just put that format string into the NSLog() call and 
eliminate the fooText temporary variable?

Regards,
Ken


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to