Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-18-2010, 11:35 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 4
Default Singleton Trouble - Compiler Warning and App Crash

I was playing around with the concept of Singleton's so I can have some shared variables that I can use throughout the code. Being a noob to all of this I have run into the following compiler warning and crash of the program. I am guessing I have missed something critical in accessing MyShare but I'm not seeing it.

When I compile I get a warning...

MyShare *slideryear = [MyShare sharedStuff]; <- Compiler Warning here "Warning "MyShare" may not respond to +sharedStuff

This is where the program crashes and then in the Console I see

*** +[MyShare sharedStuff]: unrecognized selector sent to class 0x5118
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[MyShare sharedStuff]: unrecognized selector sent to class 0x5118'

Here's the code snippets. I based this off of some DerekNeely.com samples and some Apple pages. It has to be something really obvious that I have missed the concept. Thanks for any help in advance.


Code:
*************
MyShared.h
*************

#import <Foundation/Foundation.h>

@interface MyShare : NSObject {
 NSString *whatyearisit;	
}

@property (nonatomic, retain) NSString *whatyearisit;
+ (MyShare *)sharedManager;
@end

*************
MyShared.m
*************
#import "MyShared.h"

static MyShare *sharedStuff = nil;

@implementation MyShare
@synthesize whatyearisit;

#pragma mark -
#pragma mark Singleton Methods

+ (MyShare *)sharedManager {	
	if(sharedStuff == nil) {
		sharedStuff = [[super allocWithZone:NULL] init];
	}
	return sharedStuff;
}
+ (id)allocWithZone:(NSZone *)zone {
	return [[self sharedManager] retain];

// other general Singleton stuff not listed since that is not relevant  
...
...

*************
MySlideController.m
*************

#import "MySlideController.h"
#import "MyShared.h"

implementation MySlideController

- (IBAction)changeyearresults:(id)sender {
	 
	//setup the shared variable... and make sure it works
	MyShare *slideryear = [MyShare sharedStuff];   <- Compiler Warning here "Warning "MyShare" may not respond to +sharedStuff and then crash on execution.
	
	slideryear.whatyearisit = @"Not Determined Yet";
	...
	...
} @end
PhoneKoke is offline   Reply With Quote
Old 03-18-2010, 11:41 AM   #2 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,178
Default

Quote:
Originally Posted by PhoneKoke View Post
I was playing around with the concept of Singleton's so I can have some shared variables that I can use throughout the code. Being a noob to all of this I have run into the following compiler warning and crash of the program. I am guessing I have missed something critical in accessing MyShare but I'm not seeing it.

When I compile I get a warning...

MyShare *slideryear = [MyShare sharedStuff]; <- Compiler Warning here "Warning "MyShare" may not respond to +sharedStuff

This is where the program crashes and then in the Console I see

*** +[MyShare sharedStuff]: unrecognized selector sent to class 0x5118
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[MyShare sharedStuff]: unrecognized selector sent to class 0x5118'

Here's the code snippets. I based this off of some DerekNeely.com samples and some Apple pages. It has to be something really obvious that I have missed the concept. Thanks for any help in advance.


Code:
*************
MyShared.h
*************

#import <Foundation/Foundation.h>

@interface MyShare : NSObject {
 NSString *whatyearisit;	
}

@property (nonatomic, retain) NSString *whatyearisit;
+ (MyShare *)sharedManager;
@end

*************
MyShared.m
*************
#import "MyShared.h"

static MyShare *sharedStuff = nil;

@implementation MyShare
@synthesize whatyearisit;

#pragma mark -
#pragma mark Singleton Methods

+ (MyShare *)sharedManager {	
	if(sharedStuff == nil) {
		sharedStuff = [[super allocWithZone:NULL] init];
	}
	return sharedStuff;
}
+ (id)allocWithZone:(NSZone *)zone {
	return [[self sharedManager] retain];

// other general Singleton stuff not listed since that is not relevant  
...
...

*************
MySlideController.m
*************

#import "MySlideController.h"
#import "MyShared.h"

implementation MySlideController

- (IBAction)changeyearresults:(id)sender {
	 
	//setup the shared variable... and make sure it works
	MyShare *slideryear = [MyShare sharedStuff];   <- Compiler Warning here "Warning "MyShare" may not respond to +sharedStuff and then crash on execution.
	
	slideryear.whatyearisit = @"Not Determined Yet";
	...
	...
} @end
Looks like you should be calling [MyShare sharedManager], rather than [MyShare sharedStuff], no?
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Old 03-18-2010, 11:56 AM   #3 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 4
Default

Ahh.. egg on face.. Thank You. That will teach me to cut and paste examples that appear to have bugs.
PhoneKoke is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 269
20 members and 249 guests
ADY, AragornSG, Bertrand21, Dani77, Dattee, Duncan C, fkmtc, HDshot, HemiMG, iDifferent, JasonR, macquitzon216, mer10, prchn4christ, Rudy, sacha1996, sneaky, spiderguy84, Sunny46, theone8one
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,231
Posts: 380,768
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:12 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0