Re: Class name as NSString and problem with NSLog?
Re: Class name as NSString and problem with NSLog?
- Subject: Re: Class name as NSString and problem with NSLog?
- From: Kyle Sluder <email@hidden>
- Date: Thu, 06 Mar 2014 10:30:07 -0800
> On Mar 6, 2014, at 10:21 AM, William Squires <email@hidden> 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?
> For that matter, what I want to do is something like this:
>
> Class MyClass
> "MyClass.h"
> #import <Foundation/Foundation.h>
>
> @interface MyClass : NSObject
>
> ...
>
> -(void)myMethod;
>
> @end
>
> "MyClass.m"
> #import "MyClass.h"
>
> @implementation MyClass
>
> ...
> -(void)myMethod
> {
> NSString *myClassName = ???; // What can I put here besides a literal @"MyClass"?
NSStringFromClass([self class])
>
> NSString *fooText = [NSString stringWithFormat:@"<%@> -(void)myMethod", myClassName];
> NSLog(fooText); // Yellow triangle on this line
You should learn about __PRETTY_FUNCTION__, which is a preprocessor macro that expands to a C string literal containing the name of the current function/method (including class).
--Kyle Sluder
> }
> ...
> @end
>
> so that when the myMethod message is sent to an object of MyClass, the output should be:
>
> <<timestamp>>: <MyClass> -(void)myMethod
>
> on the output pane when debugging - "<<timestamp>>" just comes from the NSLog call.
>
> 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.
Yes, passing dynamic strings as the first argument to NSLog, etc. has been the cause of many buffer overruns (and security vulnerabilities) in the past. Apple fixed a whole bunch of them a few OS revs back.
--Kyle Sluder
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden