Re: Creating shared classes
Re: Creating shared classes
- Subject: Re: Creating shared classes
- From: Michael Stuurman <email@hidden>
- Date: Wed, 18 Jul 2001 09:20:42 +0200
seems you didn't do send an alloc message (memory allocation) to the
class, so the object will not exist.
On Wednesday, July 18, 2001, at 08:52 AM, Graham Wihlidal wrote:
I have a problem. I am trying to learn how to call class functions
from different classes. I originally was just using one large class
for everything just to get the real basics, and am not expanding to
better code structure, etc...
I have placed the code from my test below and am trying to call
setUsername from the UserSession class and then print out the value
using Username from that same class. All called from main.m It seems
to compile with no warnings or errors, but the message is never
received by the class (the NSLog I placed in the setUsername function
is never called)
Any ideas?
TIA,
Graham
-- Code follows:
/// main.m
#import <Cocoa/Cocoa.h>
#import "UserSession.h"
int main(int argc, const char *argv[])
{
[[UserSession sharedUserSession] setUsername:@"Lord Storm"];
NSLog([[UserSession sharedUserSession] Username]); // Always prints
out blank text
return NSApplicationMain(argc, argv);
}
/// UserSession.h
#import <Cocoa/Cocoa.h>
@interface UserSession : NSObject {
NSMutableString* sessionUsername;
}
+ (UserSession*)sharedUserSession;
- (NSMutableString*)Username;
- (void)setUsername:(NSString*)newUsername;
@end
/// UserSession.m
#import "UserSession.h"
static UserSession* mainUserSession;
@implementation UserSession
- (id)init {
if (self=[super init]) {
mainUserSession=self;
}
return self;
}
+ (UserSession*)sharedUserSession {
return mainUserSession;
}
- (NSMutableString*)Username {
return sessionUsername;
}
- (void)setUsername:(NSString*)newUsername {
NSLog(@"Got goodness");
[sessionUsername setString:newUsername];
}
@end
_______________________________________________
cocoa-dev mailing list
email@hidden
http://www.lists.apple.com/mailman/listinfo/cocoa-dev