using arc, my ivars are null after init

2019-09-06 18:56发布

问题:

MOST NEW TESTING: I placed a NSLog(@"%p", self.myArray); after the array assignment and I am seeing a address actually logged.....!?!?!

2012-03-06 01:33:52.618 ArrayTest[9883:f803] 0xae0f160

Seems that Xcode is all wacked out if it cant see the addess of that ivar in either local variables or with the tool tip highlight method...

Thoughts?

NEWEST TESTING: I created a brand new project.

It seems that simple assigning of objects to ivars is not working at all. If I look at the address of myArray after the assignment of the newly created array it has a null address.

output of nslog

2012-03-06 01:30:37.283 ArrayTest[9848:f803] (
)
(lldb) 

//
//  ViewController.h
//  ArrayTest
//
//  Created by Ben J Brown on 3/6/12.
//  Copyright (c) 2012StudioBflat. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    NSMutableArray *myArray;

}

@property (strong) NSMutableArray *myArray;

@end


//
//  ViewController.m
//  ArrayTest
//
//  Created by Ben J Brown on 3/6/12.
//  Copyright (c) 2012 StudioBflat. All rights reserved.
//

#import "ViewController.h"

@implementation ViewController

@synthesize myArray;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSMutableArray *array = [NSMutableArray arrayWithCapacity:16];
    self.myArray = array;

NSLog(@"%@",self.myArray); }

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}

@end

OLDER DATA:

In my viewDidLoad I have:

NSLog(@"%@",self.collectionOfImageViews);
    self.collectionOfImageViews = [[NSMutableArray alloc] initWithCapacity:NUMBER_OF_COLUMNS*NUMBER_OF_ROWS];
     NSLog(@"%@",self.collectionOfImageViews);

However later on when I access that array the array has an address but all the objects that I added to it are gone, and when I send a count message to that object(the NSMutableArray) it throws this in the console:

-[UIImage count]: unrecognized selector sent to instance 0x6b7ab40

My properties in my interface:

@property(strong) NSMutableArray* collectionOfImageViews;

and I have @synthesize collectionOfImageViews; right after my @implmentation... what am I missing here?

Here is where I make the collection:

  NSLog(@"%@",self.collectionOfImageViews);
    self.collectionOfImageViews = [[NSMutableArray alloc] initWithCapacity:NUMBER_OF_COLUMNS*NUMBER_OF_ROWS];
     NSLog(@"%@",self.collectionOfImageViews);

looking at the array it has a null address right after this action....

Concerning that earlier weird error where I had it consoling out that it was a UIImage not responding to count... I fixed that kinda by changing the order of the ivar declarations in the interface:

#import <UIKit/UIKit.h>
#import "TiledImageView.h"


@interface ViewController : UIViewController <TiledImageViewDelegation>
{

    NSMutableArray *collectionOfImageViews;
    UIImage *sourceImage;
    UIImageView *currentTappedView;
}



@property(strong) NSMutableArray* collectionOfImageViews;
@property(strong) UIImage* sourceImage;
@property(strong) UIImageView* currentTappedView;

@end

As for where I fill the mutable array later here is that code:

iView = [[TiledImageView alloc] initWithImage:[UIImage imageWithCGImage:tempImage]];
                iView.userInteractionEnabled = YES;
                iView.delegate = self;
                iView.contentMode = UIViewContentModeScaleAspectFit;
                [iView setTag:index];
                [iView setPosX:column];
                [iView setPosY:row];

            [collectionOfImageViews addObject:iView];

I'm pretty darnd confused because this is simple ivar setting and getting.. and alloc and initialization... something I have done many times before but it seems my ivars are not staying alive... I'm new to ARC.

回答1:

This seems to be an LLDB bug. Use GDB for debugging instead.

edit: It obviously is a bug within lldb, i have filed a bug report back in march (it was marked as a duplicate of another bug which was later marked as closed) - the issue has been resolved in newer Xcode versions.



回答2:

The fact that u get () means that there is nothing wrong with the array. You can test it easily by adding any other object like an NSNumber and then logging the array again, you should see your object there. About the memory address, If you are using the debugger you should set the break point AFTER the instruction where the array is allocated, then you will be able to see the contents of the array. Otherwise if your break point is not there who knows what you will be seeing.

Finally the -[UIImage count]: unrecognized selector sent to instance 0x6b7ab40 Means you are trying to make an UIImage object perform a count method, which doesn't exist, You probably want the count on the array not the object inside. So the problem is in where you are calling this method, you seem to have a hard time when it comes to referencing either the object or the ivar, to avoid this, on the synthesize change the @synthesize collectionOfImageViews; to @synthesize collectionOfImageViews = collectionOfImageViews;

Also try changing the compiler like others have suggested.