【问题标题】:App crashes when setting a NSObject inside another NSObject在另一个 NSObject 中设置 NSObject 时应用程序崩溃
【发布时间】:2014-10-02 19:54:17
【问题描述】:

我有一个名为friend 的NSObject 和另一个名为currentUser 的NSObject。 currentUser 里面有一个朋友类型的属性(friendToSendMimo)。但是每当我尝试为其设置另一个朋友对象时......应用程序崩溃并出现错误:'NSInvalidArgumentException',原因:'-[currentUser setFriendToSendMimo:]: unrecognized selector sent to instance 0x15d29810'

我的问题是:我怎样才能拥有来自 NSObject 的属性?

这是 currentUser 标头:

#import "friend.h"
@interface currentUser : NSObject<NSCoding>

@property (nonatomic,strong)NSString *token;
@property (nonatomic,strong)NSString *email;
@property (nonatomic,strong)NSString *name;
@property (nonatomic,strong)UIImage *userImg;
@property (nonatomic,strong)NSString *username;
@property (nonatomic,strong)friend *friendToSendMimo; //is there another way of setting this NSObject?

代码崩溃如下:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //thisUser was set as currentUser
    thisUser.friendToSendMimo = [thisUser.friendsList objectAtIndex:indexPath.row];

}

【问题讨论】:

  • 不是你的直接问题,而是为了让其他人阅读你的代码,你应该以大写字母开头你的类名。
  • 根据您使用的 Xcode 版本,您可能需要在 @implementation 中包含 @synthesize friendToSendMimo; 以获取 currentUser

标签: ios objective-c nsobject


【解决方案1】:

您的异常原因是“无法识别的选择器”。这通常意味着您要求它执行它不知道的方法。您是否尝试过在访问之前分配和初始化friendToSendMimo 属性?

【讨论】:

  • 在初始化和综合变量之后......它工作了!谢谢!
  • 很高兴它有帮助。更重要的是,您是否理解为什么在尝试访问它之前必须先分配和初始化它?
猜你喜欢
  • 2017-06-06
  • 1970-01-01
  • 2013-10-19
  • 1970-01-01
  • 1970-01-01
  • 2016-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多