"Unrecognized Selector" Problem


#1

Hi everyone, I’ve just begun studying iOS programming with this (great) book and have run into a problem I haven’t been able to solve. I’ve searched the forums for a solution, but did not find one. (Sorry if this has already been addressed.)

I’m up to page 85 of the Whereami example and keep getting the following when I run the project:
WhereAmI[440:707] *** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[CLLocation setDelegate:]: unrecognized selector sent to instance 0x1a4960’

I’m running Xcode 4.1 on OS X Lion. Here are the things that I have done:

  1. I’ve implemented the “TemporaryHack” in WhereAmIAppDelegate.m.
  2. I’ve added “@interface WhereAmIAppDelegate : NSObject <UIApplicationDelegate, CLLocationManagerDelegate>” to WhereAmIAppDelegate.h.
  3. I’ve added the CoreLocation.framework to the project and imported it WhereAmIAppDelegate.h.

There are no issues when I build the project. But there seems to be a problem with the line “[locationManager setDelegate:self];”. If needed, I can post my code here, but will refrain from doing that right now as not to make the post overly long. Once again, I’m sorry if this has already been addressed somewhere (or if it’s just a typo or mistake on my part). Thanks in advance for any help.


#2

Hi,

Check that you have CLLocationManager *locationManager; in the .h file and

locationManager = [[CLLocationManager alloc] init];

HTH
Gareth


#3

Hi Gareth,

I’ve double checked and do have CLLocationManager *locationManager; in the .h file, as well as locationManager = [[CLLocation alloc] init]; in the .m file. But thanks for the reply.

I have decided to post my code and debug info below, as it’s quite possible that I’m not posting the right info for someone to help take a look at the problem. (I edited the comments out to try and make the post as lean as possible.)

[size=150]Debug Info[/size]

2011-09-29 17:14:31.290 WhereAmI[1068:707] -[CLLocation setDelegate:]: unrecognized selector sent to instance 0x1a6590 2011-09-29 17:14:31.807 WhereAmI[1068:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CLLocation setDelegate:]: unrecognized selector sent to instance 0x1a6590' *** Call stack at first throw: ( 0 CoreFoundation 0x347f264f __exceptionPreprocess + 114 1 libobjc.A.dylib 0x3196fc5d objc_exception_throw + 24 2 CoreFoundation 0x347f61bf -[NSObject(NSObject) doesNotRecognizeSelector:] + 102 3 CoreFoundation 0x347f5649 ___forwarding___ + 508 4 CoreFoundation 0x3476c180 _CF_forwarding_prep_0 + 48 5 WhereAmI 0x000024cd -[WhereAmIAppDelegate application:didFinishLaunchingWithOptions:] + 124 6 UIKit 0x32e79821 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 772 7 UIKit 0x32e73b65 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 272 8 UIKit 0x32e487d7 -[UIApplication handleEvent:withNewEvent:] + 1114 9 UIKit 0x32e48215 -[UIApplication sendEvent:] + 44 10 UIKit 0x32e47c53 _UIApplicationHandleEvent + 5090 11 GraphicsServices 0x32487e77 PurpleEventCallback + 666 12 CoreFoundation 0x347c9a97 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 26 13 CoreFoundation 0x347cb83f __CFRunLoopDoSource1 + 166 14 CoreFoundation 0x347cc60d __CFRunLoopRun + 520 15 CoreFoundation 0x3475cec3 CFRunLoopRunSpecific + 230 16 CoreFoundation 0x3475cdcb CFRunLoopRunInMode + 58 17 UIKit 0x32e72d49 -[UIApplication _run] + 372 18 UIKit 0x32e70807 UIApplicationMain + 670 19 WhereAmI 0x0000230f main + 82 20 WhereAmI 0x000022b8 start + 40 ) terminate called after throwing an instance of 'NSException'

[size=150]WhereAmIAppDelegate.h[/size]

[code]#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface WhereAmIAppDelegate : NSObject <UIApplicationDelegate, CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@end[/code]

[size=150]WhereAmIAppDelegate.m[/size]

[code]#import “WhereAmIAppDelegate.h”

@implementation CLLocationManager (TemporaryHack)

  • (void)hackLocationFix
    {
    CLLocation *location = [[CLLocation alloc] initWithLatitude:42 longitude:-50];
    [[self delegate] locationManager:self didUpdateToLocation:location fromLocation:nil];
    }

  • (void)startUpdatingLocation
    {
    [self performSelector:@selector(hackLocationFix) withObject:nil afterDelay:0.1];
    }

@end

@implementation WhereAmIAppDelegate

@synthesize window = _window;

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    locationManager = [[CLLocation alloc] init];

    [locationManager setDelegate:self];
    [locationManager setDistanceFilter:kCLDistanceFilterNone];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [locationManager startUpdatingLocation];

    [self.window makeKeyAndVisible];
    return YES;
    }

  • (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {
    NSLog(@"%@", newLocation);
    }

  • (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
    {
    NSLog(@“Could not find location: %@”, error);
    }

  • (void)applicationWillResignActive:(UIApplication *)application {
    }

  • (void)applicationDidEnterBackground:(UIApplication *)application {
    }

  • (void)applicationWillEnterForeground:(UIApplication *)application {
    }

  • (void)applicationDidBecomeActive:(UIApplication *)application {
    }

  • (void)applicationWillTerminate:(UIApplication *)application {
    }

  • (void)dealloc
    {
    [_window release];
    [super dealloc];
    }

@end[/code]

Any help would be appreciated. Thanks, guys.

James


#4

Hi,

In your didFinishLaunchingWithOptions method in the WhereAmIAppDelegate.m file you have

locationManager = [[CLLocation alloc] init];

it should be

locationManager = [[CLLocationManager alloc] init];

Gareth


#5

Hi Gareth,

Thanks! I almost can’t believe it was something so simple. I looked the line many times over but couldn’t see the typo, but now that you’ve pointed it out it seems glaringly obvious.

I can finally make some progress now. Thanks again. :slight_smile: