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

时间:2012-03-06 07:38:24

标签: iphone ios cocoa-touch automatic-ref-counting

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

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

如果无法在局部变量或工具提示突出显示方法中看到该ivar的地址,那么Xcode似乎已经全部消失了......

思想?

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

似乎简单地将对象分配给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

老数据:

在我的viewDidLoad中我有:

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

但是稍后当我访问该数组时,数组有一个地址,但我添加到它的所有对象都消失了,当我向该对象发送计数消息(NSMutableArray)时,它会在控制台中抛出:< / p>

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

界面中的我的属性:

@property(strong) NSMutableArray* collectionOfImageViews;

我的@implmentation后面有@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设置和获取..以及分配和初始化...我以前做了很多次但似乎我的ivars没有活着......我是新来的ARC。

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

最后 - [UIImage count]:无法识别的选择器发送到实例0x6b7ab40  意味着你试图使一个UIImage对象执行一个不存在的count方法,你可能希望数组上的数不是里面的对象。所以问题出在你调用这个方法的地方,你似乎很难在引用对象或者ivar时,为了避免这种情况,在合成上更改@synthesize collectionOfImageViews;到@synthesize collectionOfImageViews = collectionOfImageViews;

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

相关问题