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: Sixten Otto <email@hidden>
- Date: Thu, 06 Mar 2014 11:29:30 -0700
First off, you don't need to build the string ahead of time; NSLog()
supports vararg formatting:
NSLog(@"<%@> -(void)myMethod", myClassName);
Off the top of my head, I can't recall whether Class objects get formatted
as the class name automatically:
NSLog(@"<%@> -(void)myMethod", [self class]); // works??
But you can certainly convert to a string with the name of the class
explicitly with NSStringFromClass():
NSLog(@"<%@> -(void)myMethod", NSStringFromClass([self class));
On Thu, Mar 6, 2014 at 11: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"?
>
> NSString *fooText = [NSString stringWithFormat:@"<%@> -(void)myMethod",
> myClassName];
> NSLog(fooText); // Yellow triangle on this line
> }
> ...
> @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.
> By using this strategy, if several classes implement the same message, I
> can tell which instance received the message during debugging; this is
> handy when iterating over containers (such as NSArray), and passing the
> same message to multiple objects, or when passing messages to objects of
> subclasses that override the behavior of their super.
> TIA!
>
>
>
> _______________________________________________
>
> 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
_______________________________________________
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