0

我正在编写一个 iPhone 应用程序,它试图在用户单击 UITableView 中的元素时创建第二个视图。代码看起来像

  ReplyToViewController *reply = [[ReplyToViewController alloc] initWithNibName:@"ReplyTo" bundle:nil];
 reply.delegate = self;
 Message *message = [resultData objectAtIndex:indexPath.row];
 int dbid = [message.bizid intValue];
 NSLog(@"dbid=%d",dbid);

 reply.currentMessage = message;
 reply.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
 [self presentModalViewController:reply animated:YES];

回复对象被正确创建并且视图是正确的。上面代码段的最后一行调用了一些框架代码,最终调用了ReplyToViewController的viewDidLoad方法。上面代码中回复对象的地址和viewDidLoad中对象的地址不一样。

知道这个新对象来自哪里吗?我该如何调试?我还在 ReplyToViewController 中添加了以下方法的 init 方法,希望它会被调用,并且我可以找到创建这个新对象的人。但它并不止于此方法。

任何帮助将不胜感激。

- (id) init
{ 
 /* first initialize the base class */
    self = [super init]; 
 return self;
}

// Following gets called from the 1st code segment.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) 
 {
        // Custom initialization
    }

    return self;
}

- (void)viewDidLoad 
{
    [super viewDidLoad];
    NSLog(currentMessage.text]; // THIS returns nil and the program fails later in the code.
}
4

2 回答 2

0

我确定这无关紧要,但我认为:

NSLog(currentMessage.text];

应该是这样的:

NSLog(currentMessage.text);

此外,我发现分析 (Cmd+Shift+A) 我的代码总是有助于追踪潜在的内存泄漏并防止过度的内存分配。

于 2010-06-09T15:35:32.513 回答
0

最可能的解释是报告错误。

通常,当人们看到对象的不同地址时,这是因为他们在日志语句中使用了错误的格式描述符。

一个常见的错误是:

NSLog(@"Object address=%i", myObject); // any numerical formatter %d,%f...

...产生一个随机数。你真的想要:

NSLog(@"Object address=%%qX",&myObject);

...以十六进制转储地址。

另一个错误是:

NSLog(@"Object address=%%qX",&[myObject description]); 

...返回每次更改的描述字符串的地址。

还有其他人,但你明白了。

如果您使用日志语句,请检查调试器中的地址以确认它是不同的对象。

不相关,但我会摆脱类的初始化方法,因为它们除了调用 super 之外什么都不做。如果您不自定义,您不妨让编译器默认为超级。

于 2010-06-09T15:48:59.330 回答