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 <wsqui...@satx.rr.com>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 (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/himself%40sfko.com > > This email sent to hims...@sfko.com _______________________________________________ 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