Search code examples
iosuiviewframenslog

Objective C: how to get the string equivalent, or variable name of a method argument call


I want to make a simple method that logs a view's frame, but also logs the name of the specific view input. Is there an easy way to get the string equivalent of the called view, aside from inputting a string as an argument to the method?

-(void)printViewFrame:(UIView*)view { 

    NSLog(@"%@'s frame is: origin x: %f, origin y: %f, size x: %f, size y: %f", view ,view.frame.origin.x, view.frame.origin.y, view.frame.size.width, view.frame.size.height);

}

[self printViewFrame:myView];

NOTE: for now I am just doing this, which is a little ugly:

-(void)printViewFrame:(UIView*)view name:(NSString*)name {

    NSLog(@"%@'s frame: %@",name, NSStringFromCGRect(view.frame));

}

Solution

  • To get the view's frame you can use NSStringFromCGRect

    NSLog(@"%@",NSStringFromCGRect(view.frame));
    

    But if you want to get the name of the view you will have to pass it through as an NSString. You can only read the memory address of the pointer and not it's real name. The closest you'll get to this is

    NSLog(@"%@",NSStringFromClass([view class]));
    

    which will output

    UIView