Re: Passing variables between .m files
Re: Passing variables between .m files
- Subject: Re: Passing variables between .m files
- From: Graham Cox <email@hidden>
- Date: Thu, 10 Sep 2009 16:59:43 +1000
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 (email@hidden)
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:
This email sent to email@hidden