1

I'm just learning Objective-C and have a what I'm sure is a pretty basic question here. This is a function I've created that will simple hold variables from some user input.

- (void)standardDatabaseWithName:(NSString*)name 
                            host:(NSString*)host 
                        username:(NSString*)username 
                        password:(NSString*)password
                        database:(NSString*)database
                            port:(NSInteger*)port {

  NSString *myName = name;
  NSString *myHost = host;
  NSString *myUsername = username;
  NSString *myPassword = password;
  NSString *myDatabase = database;
  NSInteger *myPort = port;
}

Below is a seperate function where I want to create a new var with that information and the from there use it was what I need.

- (void)insertStandardConnection {
    NSString name = [NewDbModalView standardDatabaseWithName:myName];
    NSString host = [NewDbModalView standardDatabaseWithName:myHost]; 
}

So this attempt didn't work for me. Any advice here guys? At this point I've been left scratching my head.

4 Answers 4

2

I would suggest creating an object to hold all of that information and just pass the object around

Sign up to request clarification or add additional context in comments.

Comments

0

Every time you call the method, you have to provide all of the variables:

[NewDbModalView standardDatabaseWithName:myName host:myHost username:aUsername password:aPassword database:aDatabase port:aPort];

So, when you call [NewDbModalView standardDatabaseWithName:myName], that method doesn’t exist.

2 Comments

Hey, thanks that makes sense. So the reason I was hoping my method would work is because I need to, at certain point, only target one of these var. So say I've done what you suggesting and only need to work with the username for example. How can I target that?
You can create methods to do each of those. So if you only need to change the password, make a -setPassword: method.
0

The variables you declare in standardDatabaseWithName:... are local to that method. You need to store (copies of) that data inside the object. Although, looking at your code, I wonder if you're trying to set default values? In that case, you might want static global variables to take the values, and then you'd need -(void)standardDatabaseWithName:... to become +(void)setStandardDatabaseName:(NSString *)name ....

Comments

0

In your .h file you should declare properties

    @interface standardDatabaseWithNameObject : NSObject{
    NSString *myName;
    NSString *myHost;
    NSString *myUsername ;
    NSString *myPassword;
    NSString *myDatabase;
    NSInteger *myPort;
    }

    @property (nonatomic, retain) NSString *myName;
    @property (nonatomic, retain) NSString *myHost;
    @property (nonatomic, retain) NSString *myUsername ;
    @property (nonatomic, retain) NSString *myPassword;
    @property (nonatomic, retain) NSString *myDatabase;
    @property (nonatomic, retain) NSInteger *myPort;
etc...

Then in your implementation you need to synthesize the properties and they will be available for use:

@synthesize myName, myHost, myUsername, myPassword, myDatabase, myPort;


- (void)standardDatabaseWithName:(NSString*)name 
                            host:(NSString*)host 
                        username:(NSString*)username 
                        password:(NSString*)password
                        database:(NSString*)database
                            port:(NSInteger*)port {

  myName = name;
  myHost = host;
  myUsername = username;
  myPassword = password;
  myDatabase = database;
  myPort = port;
}

- (void)insertStandardConnection {
    NSString name = myName;
    NSString host = myHost; 
}

- (void) dealloc
{
[myName release];
[myHost release];
[myUsername release];
[myPassword release];
[myDatabase release];
[myPort release];

}

Good Luck

3 Comments

Hey, thanks a lot. You've made that pretty crystal clear. So when passing that amount or info around. It's probably always cleaner to create a separate object, correct?
All of those need to be self.myName, etc, or the above code won't be following the retain/release rules...
Shane, depending on where you need the data, you should store it on that particular level. If you only need it within a function, then declare it in the function. If you need it in the object, then declare it as a property. If you need to globally, then declare it globally. -- Also bbum is right, you want to deallocate your variables at some point as to not create a memory leak.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.