【问题标题】:iphone app : EXC_BAD_ACCESS, message sent to deallocated instanceiphone 应用程序:EXC_BAD_ACCESS,消息发送到释放的实例
【发布时间】:2014-10-18 16:31:58
【问题描述】:

我是一名新的 iPhone 开发人员。我正在开发一个使用两个表视图的应用程序。 我的代码在 iOS 6 上运行良好,但是当我转到 iOS 7 并尝试启动应用程序时,我遇到了以下错误。 根据网上的一些答案,我启用僵尸。 返回的初始错误消息是 EXC_BAD_ACCESS。当我启用 NSZombieEnabled 时,我收到错误消息 "-[CFString retain]: message sent to deallocated instance"

这是一段代码,抛出错误, 导致错误的原因是,

SystemViewController.h

if(indexPath.section == 0)
{
  //User Info section
  if(indexPath.row == 0)
  {
    cell2.label1.text = @"User";
    cell2.label2.text = [UserInfoBean userName];
  }
  else
  {
   cell2.label1.text = @"SI Host URL";
   cell2.label2.text = [UserInfoBean url];
  }
  cell = cell2;
}

Interface UserInfoBean.h

@implementation UserInfoBean

static NSString *userName;
static NSString *url;
static NSString *presentNode;




+ (NSString *)userName { return userName; }
+ (NSString *)url { return url; }
+ (NSString *)presentNode { return presentNode; }


+ (void)setUserName:(NSString *)newVar { userName = newVar; }
+ (void)setUrl:(NSString *)newVar { url = newVar; }
+ (void)setPresentNode:(NSString *)newVar { presentNode = newVar; }

@end

Thanks in advance!

【问题讨论】:

  • 这里是 UserInfoBean 类,
  • 你的 UserInfoBean 类在哪里?交叉检查您的 UserInfoBean 类属性 geter 和 setter 方法实现(expcly userName、url)。
  • 接口 UserInfoBean.h @interface UserInfoBean : NSObject { } + (NSString *)userName; + (NSString *)url; + (NSString *)presentNode; + (void)setUserName:(NSString *)newVa; + (void)setUrl:(NSString *)newVa; + (void)setPresentNode:(NSString *)newVa;
  • 请使用默认的属性声明。例如:@property (nonatomic, strong) NSString *userName;

标签: ios objective-c iphone


【解决方案1】:

我建议使用属性而不是编写自己的变量访问器方法,如下所示:

@property (nonatomic, retain) NSString *userName; 

编译器负责所有设置/获取这些变量,使用 retain 将执行以下操作:

保留一个对象会创建一个强引用,而一个对象不能 被释放,直到它的所有强引用都被释放。如果两个 对象相互保留,两个对象都不会被释放 因为它们之间的联系是无法断开的

创建属性后,您可以删除 getter/setter 方法并简单地使用 UserInfoBean.userName = @"something" 来设置或使用 NSString *something = UserInfoBean.userName 来获取。

【讨论】:

    猜你喜欢
    • 2023-03-29
    • 1970-01-01
    • 2011-06-14
    • 2012-08-22
    • 1970-01-01
    • 2011-12-17
    • 2016-08-28
    • 2011-06-16
    • 2013-08-30
    相关资源
    最近更新 更多