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

时间:2016-10-14 12:29:13

标签: ios objective-c

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

- (AppDelegate *)service;

现在在ViewController中我已经包含了app 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'

我哪里错了?

1 个答案:

答案 0 :(得分:1)

要注意听到的一些事情,

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

如下所示,

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"