开发者

Pass variables from function to function

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 开发者_JAVA百科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.


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


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.


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 ....


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜