使用arc,我的ivar在init之后为null



最新测试:我放置了一个NSLog(@"%p", self.myArray);在数组分配之后,我看到一个地址实际上被记录了。。。。。!?!?!

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

如果Xcode在局部变量中或使用工具提示高亮显示方法都看不到ivar的加数,那么它似乎就完全疯了。。。

想法?

最新测试:我创建了一个全新的项目。

似乎简单地将对象分配给ivars根本不起作用。如果我在分配新创建的数组后查看myArray的地址,它的地址为空。

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

旧数据:

在我的视图DidLoad中,我有:

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

然而,稍后当我访问该数组时,该数组有一个地址,但我添加到其中的所有对象都不见了,当我向该对象(NSMutableArray)发送计数消息时,它会在控制台中抛出以下消息:

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

我的界面中的属性:

@property(strong) NSMutableArray* collectionOfImageViews;

在我的@implementation之后我就有了@synthesize collectionOfImageViews;。。。我在这里错过了什么?

这是我收集的地方:

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

看看这个数组,它在这个操作之后有一个空地址。。。。

关于之前的那个奇怪错误,我安慰说这是一个UIImage没有响应计数。。。我通过更改接口中ivar声明的顺序来修复这种问题:

#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

至于我稍后填充可变数组的位置,这里是代码:

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];

我非常困惑,因为这是一个简单的ivar设置和获取。。以及分配和初始化。。。我以前做过很多次,但我的ivar似乎活不下去了。。。我是ARC的新手。

这似乎是一个LLDB错误。请改用GDB进行调试。

编辑:这显然是lldb中的一个错误,我在三月份提交了一份错误报告(它被标记为另一个错误的副本,后来被标记为关闭)-这个问题在较新的Xcode版本中已经解决。

u get()这一事实意味着数组没有任何问题。您可以通过添加任何其他对象(如NSNumber)来轻松测试它,然后再次记录阵列,您应该可以在那里看到您的对象。关于内存地址,如果你正在使用调试器,你应该在分配数组的指令之后设置断点,这样你就可以看到数组的内容。否则,如果你的断点不在,谁知道你会看到什么。

最后,-[UIImage count]:无法识别的选择器被发送到实例0x6b7ab40意味着你试图让UIImage对象执行一个计数方法,但该方法不存在。你可能想要数组上的计数,而不是里面的对象。因此,问题在于你在哪里调用这个方法,当涉及到引用对象或ivar时,你似乎很难做到这一点,为了避免这种情况,在synthetic上更改@synthetic collectionOfImageViews;to@synthesis collectionOfImageViews=collectionOfImageViews;

也可以像其他人建议的那样尝试更改编译器。

最新更新