ios - 无法识别的选择器发送到 ViewController 的实例

标签 ios objective-c

我在Appdelegate.m中创建了一个方法

- (AppDelegate *)service;

现在在 ViewController 中我包含了应用委托(delegate):

#import "AppDelegate.h"

在 ViewController.h 中,我创建了带有“弱”引用的属性:

@property (nonatomic, weak) GDataServiceGooglePhotos *service;

在实现文件ViewController.m中:

_service = [(AppDelegate *)[[UIApplication sharedApplication] delegate] service];

但它显示错误,

-[AppDelegate service]: unrecognized selector sent to instance 0x7a284bc0
2016-10-14 14:21:34.216 PicassaClient[2474:134418] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AppDelegate service]: unrecognized selector sent to instance 0x7a284bc0'

我哪里错了?

最佳答案

有几件事需要注意,

首先,你正在调用一个方法,但你没有定义,这就是它崩溃的原因。

像下面这样改变,

Appdelegate.h

添加这个

- (GDataServiceGooglePhotos *)service; //it returns the type `GDataServiceGooglePhotos`

Appdelegate.m

- (GDataServiceGooglePhotos *)service 
  {
   //your code 


   //finally
   return myGDataServiceGooglePhotos; // it is the instance of type GDataServiceGooglePhotos
  }

在 ViewController.h 中,与您所做的相同

@property (nonatomic, weak) GDataServiceGooglePhotos *service;

在 ViewController.m 中:

_service = [(AppDelegate *)[[UIApplication sharedApplication] delegate] service]; //hear u are calling the method named "service" which is returning  an instance of type "GDataServiceGooglePhotos"

关于ios - 无法识别的选择器发送到 ViewController 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40043181/

相关文章:

ios - 如何避免核心数据中的重复记录 - ios

ios - 在单元格和水平滚动之间滑动冲突

ios - 在专属网络中是否可以使用对等Wi-Fi?

ios - 缺少 148x110 iMessage 图标

objective-c - Objective-C:尝试在UIScrollView中实现自己的拖动,遇到了很多问题

objective-c - 在自定义 View 上显示 NSString

iphone - UILabel 不更新

ios - 我怎样才能加快 becomeFirstResponder 的速度?

ios - 以编程方式更改n个按钮的按钮文本颜色,而无需更改其他按钮的文本颜色

iphone - xcode,nslog 奇怪的问题