Search code examples
iosnslog

Value transfer issue


Is there any explanation why the NSLog always displays null when running this code?

ExtratoDeNotasSideMenuViewController *extratoDeNotasSideMenuViewController =   [[ExtratoDeNotasSideMenuViewController alloc] init];
extratoDeNotasSideMenuViewController.userImageView.image = [UIImage imageNamed:@"Icone_SideBar.png"];
NSLog(@"%@", extratoDeNotasSideMenuViewController.userImageView.image);

2014-10-28 13:40:50.594 E-proinfo[913:51418] (null)


Solution

  • Potential causes:

    • Your ExtratoDeNotasSideMenuViewController init returned nil because there was an error during initialization.

      NSLog(@"%@", extratoDeNotasSideMenuViewController); // Does this display '(null)'?
      
    • Your userImageView isn't initialized yet because you don't initialize it in ExtratoDeNotasSideMenuViewController init.

      NSLog(@"%@", extratoDeNotasSideMenuViewController.userImageView); // Does this display '(null)'?
      
    • Your image is nil because you don't have an image named Icone_SideBar.png.

    You should either add NSLog statements or add a breakpoint and use po to inspect your objects.

    Edit: If you want to set something on your view controller before your view is created (or as a persistent thing between view creations if you expect your view controller to destroy its view and then recreate it at some point), you should use a property.

    Consider the following:

    @interface ExtratoDeNotasSideMenuViewController
    @property (strong) UIImage *myUserImage;
    ...
    @end
    
    @implementation ExtratoDeNotasSideMenuViewController
    ...
    - (void)loadView {
      [super loadView];
      ...
      [self.userImageView setImage:self.myUserImage];
      ...
    }
    ...
    @end
    
    ...
      ExtratoDeNotasSideMenuViewController *extratoDeNotasSideMenuViewController =   [[ExtratoDeNotasSideMenuViewController alloc] init];
      extratoDeNotasSideMenuViewController.myUserImage = [UIImage imageNamed:@"Icone_SideBar.png"];