prace magisterskie prace magisterskie - 4 months ago 51
Objective-C Question

Unrecognized selector sent to instance of ViewController

I have created a method in Appdelegate.m

- (AppDelegate *)service;

Now in ViewController I have included app delegate:

#import "AppDelegate.h"

In ViewController.h I have created property with "weak" reference:

@property (nonatomic, weak) GDataServiceGooglePhotos *service;

And in implementation file ViewController.m:

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

But it's showing the error,

-[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'

Where I am wrong?


couple of things to be note hear,

first of all u are calling a method but u are not defined, that is the reason it is crashing.

change like below,

in Appdelegate.h

add this

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

in Appdelegate.m

- (GDataServiceGooglePhotos *)service 
   //your code 

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

In ViewController.h , same as u are doing

@property (nonatomic, weak) GDataServiceGooglePhotos *service;

in ViewController.m:

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