Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Using variables from diff implementations

Using variables from diff implementations
Thread Tools
Junior Member
Join Date: Jul 2002
Location: Australia
Status: Offline
Reply With Quote
Aug 28, 2002, 06:38 AM
 
How can i use a variable (NSString) "bam" from a different implementation called "bang"???

I tried something like this:

Code:
NSString *bam = [[NSString alloc] initWithFormat:@"bam - %@", bang.bam];
But no such luck. an anyone help me?
     
Mac Enthusiast
Join Date: Nov 2001
Status: Offline
Reply With Quote
Aug 28, 2002, 06:57 AM
 
What exactly is this "implementation" of bang? An instance, a method, a class? You could declare it as a static variable, an external variable, it all depends on what you want to do.
     
Forum Regular
Join Date: Oct 2001
Location: Sweden
Status: Offline
Reply With Quote
Aug 28, 2002, 07:01 AM
 
I guess you mean from a different object (or instance), not implementation. Anyway, the correct object oriented solution is to write an accessor method and use, in this case, [bang bam] to access the bam variable. The accessor is in most cases trivial, just:
- (<variable type>)bam
{
return bam;
}

Another way of accessing the variable is to use the -> operator. The . operator (that you used in your code) won't work since Cocoa always uses pointers to objects. That said, never ever use this method, always use accessors.

Hope this helps
     
Mac Enthusiast
Join Date: Nov 2001
Status: Offline
Reply With Quote
Aug 28, 2002, 07:04 AM
 
OK, I think what you're asking is that you have an instance of an object called "bang" that you want to access that ivar "bam" from in that case you need...accessor methods!
Add this method to your "Bang" class (class names should be capitalized):
Code:
- (NSString *)bam { return bam; }
Then to access your string, call [bang bam]. As a side note, you don't actually need this. Instead of bang.bam, the correct way to write it is bang->bam, which accesses the bam ivar in the bang object. Don't get into the habit of this though; it's poor style and goes against the abstract OO concepts behind Objective-C.
     
Mac Enthusiast
Join Date: Nov 2001
Status: Offline
Reply With Quote
Aug 28, 2002, 07:05 AM
 
Ahh tobli bet me to it!
     
   
Thread Tools
Forum Links
Forum 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
Top
Privacy Policy
All times are GMT -5. The time now is 01:42 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2