gpt4 book ai didi

iphone - UITextView alloc 在 iOS4 上崩溃应用程序

转载 作者:行者123 更新时间:2023-12-03 15:57:21 25 4
gpt4 key购买 nike

我对此感到非常困惑。我正常工作的应用程序 [在 iOS<=3.1 上] 在 iOS4 上崩溃了。当我尝试调试它时,我看到它在这一行崩溃了:

编辑:我尝试更改此语句的位置并放置一些日志语句来检查,我可以确认应用程序在这一行崩溃。

这是我的代码的一部分:

    //Various allocations here like

lblForAttachingPhoto = [[UILabel alloc]initWithFrame:CGRectMake(235, 105, 40, 20)];
lblForAttachingPhoto.backgroundColor = [UIColor grayColor];
[lblForAttachingPhoto setText:@"140"];
[lblForAttachingPhoto setTextAlignment:UITextAlignmentCenter];

btnAttachPhoto = [UIButton buttonWithType:UIButtonTypeCustom];
btnAttachPhoto.frame = CGRectMake(275, 105, 20, 20);
btnAttachPhoto.autoresizesSubviews = YES;
btnAttachPhoto.clipsToBounds = YES;
[btnAttachPhoto setImage:[UIImage imageNamed:@"arrow.jpg"] forState:UIControlStateNormal];
[btnAttachPhoto addTarget:self action:@selector(attachPhoto) forControlEvents:UIControlEventTouchUpInside];

viewForAttaching = [[UIView alloc]init];
viewForAttaching.frame = CGRectMake(5, -340, 310, 140);


for(NSInteger e=0;e<[_appDelegate.genericArray count];e++)
{
[genericArray addObject:[_appDelegate.genericArray objectAtIndex:e]];
}

for(NSInteger f=0; f< [_appDelegate.category_type count]; f++)
{
[category_type addObject:[_appDelegate.category_type objectAtIndex:f]];
}

txtVw = [[UITextView alloc]init]; //txtVw is a textview

但是,尽管 textview 分配语句的声明的位置,它使应用程序崩溃。

控制台输出:
          Program received signal:  “EXC_BAD_ACCESS”.
warning: check_safe_call: could not restore current frame

warning: Unable to restore previously selected frame.

是否可以?我也在实现 UITextViewDelegate 协议(protocol)。

有人可以帮忙吗?

提前谢谢。

最佳答案

崩溃实际上不太可能是由于 UITextView 初始化造成的。我实际上怀疑 for() 循环中它上面的行。您的命名和内存管理非常不一致,这在 ObjC 中很危险。

首先,为您的变量提供一致的名称。 category_type听起来像一个枚举,但它似乎是一个 NSMutableArray .这种命名会在 ObjC 中杀死你;您必须跟踪事物是什么,因为编译器通常不会捕获类型不匹配。你应该有一个像 categoryTypes 这样的名字.

接下来,您不应该直接访问您的 ivars。始终使用访问器。总是。 (可能在dealloc 中除外;那里存在争议。)这是避免EXC_BAD_ACCESS 的#1 方法。你显然没有正确地保留一些东西。访问器可以防止这种情况。

你真的要附加 _appDelegate.genericArraygenericArray ?或者你真的想制作genericArray _appDelegate.genericArray 的副本?或者你只是想要genericArray指向_appDelegate.genericArray ?如果你想要一个副本,只需复制它:

self.genericArray = [NSMutableArray arrayWithArray:self.appDelegate.genericArray];

我知道所有这些听起来像是“公正的风格”,但一致的风格对于稳定的 ObjC 绝对至关重要。您会看到其他情况下出现的烦人的崩溃。

您还应该点击“Cmd-Shift-A”并让静态分析器为您查找错误。

关于iphone - UITextView alloc 在 iOS4 上崩溃应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3684603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com