views:

264

answers:

3

Greetings - I am trying to set and store values in memory... and be able to share them between classes. I've tried two different methods with not too much luck -- one with 'dot' syntax and one with [].

Generally: In class1 I have something like this:

#import <Foundation/Foundation.h>
@class Class1;

@interface Class1 : NSObject {
    NSString *isTest;
}

@property(nonatomic, retain) NSString *isTest;

- (void)startTest;

@end

The .m file looks like this:

#import "Class1.h"

@implementation Class1
@synthesize isTest;

- (void)startTest { 
    self.isTest=@"yes this is a test";
}

So All I'm trying to do is set up a method that sets a value (isTest) when the method(startTest) is called.

Then in another class, I am trying to execute and access some of the values from Class1:

Here is the .h

#import <UIKit/UIKit.h>
#import "Class1.h"

@interface _AppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
    UIWindow *window;
    UIView *view1;
    Class1 *Class1Local;
}

@property (nonatomic, retain) Class1 *Class1Local;

@end

and then the .m

#import "_AppDelegate.h"

@implementation _AppDelegate

@synthesize window,view1;
@synthesize Class1Local;

- (void)applicationDidFinishLaunching:(UIApplication *)application {


    // one time we want to kick off the startTest event
    // this sets the isTest value
    [Class1Local startTest];

    NSString *dantest;
    dantest=[solarDan isRegistered];

    NSLog(dantest);

}

So I'm sure this is very simple..... just not for me. Any ideas on why I can't get to this value? I don't get any errors... just an empty value.

I started this a different way by using the form of id Class1Local; Class1Local = [Class1 alloc]; [Class1Local startTest]; dantest = [Class1Local isTest];

...but not much luck there either.

I'm missing one key concept -- how do I set it and then access it from another spot. I thought that the @Register in the home class did the setters and getters, and ultimately put the value into memory. I just can't seem to get my hands on a value.

I also checked for other occurrences of the same variable in case I am stopming on the value... no joy.

Any suggestions and teachings would be much appreciated for this old aspnet/sql gone objective-c guy.

thanks! Dan Ribar

Dan Ribar

+2  A: 

Okay, you forgot to create an object to put in the field Class1Local.

self.Class1Local = [[[Class1 alloc] init] autorelease];

If you call alloc, you have to call init. And if you call alloc, you have a reference which has to be released somewhere.

I'm utterly confused about what you're actually trying to do. My guess is you have a field in one class that you want to access from another class, but I don't know what some of your code is supposed to do (where does solarDan and isRegistered come from?). Here's a short example (my Objective-C is a little old-school but it still works).

@interface TastyCookie : NSObject {
    NSString *_cookieType;
}
- (void)setCookieType:(NSString *)cookieType;
- (NSString *)cookieType;
@end

@implementation TastyCookie
- (id)init {
    if (![super init])
        return nil;
    [self setCookieType:@"Chocolate Chip"];
    return self;
}
- (void)setCookieType:(NSString *)cookieType {
    [_cookieType release];
    _cookieType = [cookieType retain];
}
- (NSString *)cookieType {
    return _cookieType;
}
@end

void my_function(void) {
    TastyCookie *c = [[[TastyCookie alloc] init] autorelease];
    NSLog(@"Cookie: %@", [c cookieType]);
    [c setCookieType:@"Snickerdoodle"];
    NSLog(@"Cookie: %@", [c cookietype]);
}

The question is pretty basic, so you might want to check out some Objective-C tutorials and the like.

Dietrich Epp
Your setCookieType: method may prematurely deallocate _cookieType and then crash if the input value and _cookieType varibales are identical (retain sent to deallocated object). It might be better to autorelease _cookieType, or to check for equality beforehand.
dreamlax
A: 

My first guess is that Class1Local is nil, which will probably mean the objective-c runtime will give you an empty string for the call to isRegistererd (since it will return some default value for messages sent to nil).

Stick in an NSLog(@"%@", Class1Local); to make sure it's not nil and go from there.

If it is nil, you need to initialize it with something like

Class1Local = [[Class1 alloc] init];

And a [Class1 release] in your app delegate's dealloc method would be a good idea to make sure it is released at some point.

Mike Weller
+2  A: 

I'm guessing you chopped out a lot of code before you posted your example here, which created some confusing typos. For instance:


dantest=[solarDan isRegistered];

solarDon? What's that. In any case, in the code you supplied, you're calling an instance method on a variable (Class1Local) that's not initialized yet. You need:


Class1Local = [[Class1 alloc] init];

to create an instance first. Then startTest should work on it.

In addition, when using a NSString property, every example of code I've seen uses


@property (nonatomic, copy) NSString *var;

to make a copy of the string, rather than trying to bump the retain count on the string.

BigSprocket