Re: Passing variables between .m files

2009-09-10 Thread Graham Cox

Hi Aaron,

You seem to be conflating several unrelated concepts here.

First, a file is not an object or class, and an object or class is not  
a file. A file is just a container for some text which can be  
compiled. A file might contain the definitions for several objects, or  
a single object might be spread across several files.


Taking your request at face value, accessing variables across files. A  
variable accessible from multiple files is a global variable, and  
globals are by and large to be discouraged, so unless you have very  
unusual needs, you don't want to do that.


However, I think what you're really asking is how one object can  
access the properties of another.


So, there's another conflation - variables are not properties and  
properties are not variables, though often properties might be  
implemented in terms of instance variables. For one object to get a  
property value from another, it simply messages it. To do that it  
needs a reference to the other object, and I believe this is the crux  
of your question - how to get a reference to another object.


Object references come from many, many different places, so there's no  
one answer. One way is, if both of your objects are instantiated in a  
nib, for them to declare IBOutlets to the other object, and for them  
to be connected in IB. Then in your code, you can reference the other  
object using the outlet.


I think one reason you didn't find anything on the topic (apart from  
searching for the wrong terms) is that messaging objects is so  
fundamental to the way OO programming works that it's likely to be  
taken for granted. Reviewing some of the very basic Cocoa concepts  
should help you out here.


--Graham



On 10/09/2009, at 7:09 AM, Aaron Robinson wrote:

I have been struggling over this one for a while now, and what  
frustrates me

the most is the feeling that it should be relatively simple to do.

Let's say I have File1.m and File2.m, and I would like to access a  
variable

in File2.m from File1.

My previous programming experience is in Java, so my first  
impression was to

do this (in the File1 implementation file):

File2.outletVariable = varFile1;

I tried reorganizing it to fit Object C in this manner:

[File2.outletVariable setFloatValue:varFile1];

It would give me the error syntax error before . token, which I  
take to

mean it doesn't support identifying variables in this manner.

I have been through dozens of blogs, tutorials, and sample projects  
in the
hopes of finding one that did this kind of thing, but I so far I  
haven't
found blogs or tutorials that go over it, and the sample code that  
did have
something like it was too difficult for me to understand, since it  
usually

involved some other process that I wasn't familiar with.

Some of what I read lead me to believe that the answer possibly lies  
in
created another method and calling it from the other file.  I  
haven't been

able to get this to work for me either, however; the closest I got, it
wouldn't let me pass a float variable through it.  And it seemed  
like that

was more work than necessary to pass variables in this manner.

The reason I can't just put all the variables I want access to in  
the same
file is because I'm developing an Audio Unit with the Cocoa View,  
and using
Interface Builder, and the place I can get the variables are in a  
different

file than the outlets for my textfields and sliders.

I would really appreciate any suggestions.  Other than a few little  
things

like this, I have greatly enjoyed developing via Xcode and Interface
builder.


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Passing variables between .m files

2009-09-10 Thread BareFeet

Hi Aaron,

I have been struggling over this one for a while now, and what  
frustrates me

the most is the feeling that it should be relatively simple to do.

Let's say I have File1.m and File2.m, and I would like to access a  
variable

in File2.m from File1.

My previous programming experience is in Java, so my first  
impression was to

do this (in the File1 implementation file):

File2.outletVariable = varFile1;


I asked a similar question a few days ago, and was helped out by the  
same Graham who answered you. I feel your pain and confusion.


The answer is pretty simple, assuming we're talking about the same  
thing.


I'm assuming you have two classes, depicted in your File1.m and  
File2.m files, and you have an object of each class in your nib file.  
You need to add an outlet in File1.h that you can link to the File2  
object, like this:


// File1.h

@class File2;

@interface File1 : NSObject
{
IBOutlet File2* linkToFile2;
}

Once that is saved in XCode, switch over to Interface Builder, control- 
drag from your File1 object to your File2 object. The popup menu  
should show linkToFile2, select it.


Now you can call any method in File2 from File1. If you want to access  
an instance variable in the File2 object, such as (NSString*)  
myVariable, then you will need to create an accessor method for it in  
File2, such as:


- (NSString*) myVariable
{
return myVariable;
}

Then you can refer to it from File1 via:

[linkToFile2 myVariable];

Hope this helps you as much as Graham helped me. If not, flip back to  
his answers to my question about a week ago.


Tom
BareFeet

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com