Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-25-2009, 10:58 AM   #1 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 21
neigaard is on a distinguished road
Default Objective-C static public variable?

Im an old Java developer, staring on Objective-C. In Java I often declare some static variables to be used to test on later, like this:

Code:
public class Foo {
  public static int TYPE_CIRCLE = 0;
  public static int TYPE_SQUARE = 1;

  public void draw(int type) {
    if(type == TYPE_CIRCLE) {
      ...
    }
  }
}
And then I can use this from another class like this:

Code:
public class Bar {
  Foo f = new Foo();
  f.draw(Foo.TYPE_CIRCLE);
}
Can I do something like this in Objective-C, or what do you guys do to archive something like this?

Thank you
Søren
neigaard is offline   Reply With Quote
Old 03-16-2009, 12:30 PM   #2 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 1
wombleme is on a distinguished road
Default from the apple dev docs

the apple dev docs has got some useful info on static variables:

When you define a new class of objects, you can decide what instance
variables they should have. Every instance of the class will have its
own copy of all the variables you declare; each object controls its own
data.

However, you can't prescribe variables for the class object; there are
no "class variable" counterparts to instance variables. Only internal
data structures, initialized from the class definition, are provided for
the class. The class object also has no access to the instance variables
of any instances; it can't initialize, read, or alter them.

Therefore, for all the instances of a class to share data, an external
variable of some sort is required. Some classes declare static variables
and provide class methods to manage them. (Declaring a variable static
in the same file as the class definition limits its scope to just the
class-and to just the part of the class that's implemented in the file.
Unlike instance variables, static variables can't be inherited by
subclasses.)

Static variables help give the class object more functionality than just
that of a "factory" producing instances; it can approach being a
complete and versatile object in its own right. A class object can be
used to coordinate the instances it creates, dispense instances from
lists of objects already created, or manage other processes essential to
the application. In the case when you need only one object of a
particular class, you can put all the object's state into static
variables and use only class methods. This saves the step of allocating
and initializing an instance.


Quote:
Originally Posted by neigaard View Post
Im an old Java developer, staring on Objective-C. In Java I often declare some static variables to be used to test on later, like this:

Code:
public class Foo {
  public static int TYPE_CIRCLE = 0;
  public static int TYPE_SQUARE = 1;

  public void draw(int type) {
    if(type == TYPE_CIRCLE) {
      ...
    }
  }
}
And then I can use this from another class like this:

Code:
public class Bar {
  Foo f = new Foo();
  f.draw(Foo.TYPE_CIRCLE);
}
Can I do something like this in Objective-C, or what do you guys do to archive something like this?

Thank you
Søren
wombleme is offline   Reply With Quote
Old 03-21-2009, 11:20 PM   #3 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 1
isabel82 is on a distinguished road
Default

If you peruse the docs, you'll see that that's not quite the style of Objective-C. Java and Flex nicely tuck their constants into appropriate classes, by Objective-C uses C-style #define statements or typedef enums and...long, long variable names!

I wonder what the longest in the whole API is.

But they're things like "UIViewAnimationTransitionFlipFromRight" and "kAudioSessionUnsupportedPropertyError". Yes, of course it would be much easier to autocomplete AudioSession.Error_UnsupportedProperty or something, but where's the fun in that ...

Isa
isabel82 is offline   Reply With Quote
Old 03-24-2009, 09:04 PM   #4 (permalink)
I like teaching
 
kwigbo's Avatar
 
Join Date: Sep 2008
Posts: 94
kwigbo is on a distinguished road
Default

This is how I found it easiest to accomplish what you are talking about.

In the .h
extern NSString *const MY_CONSTANT;

In the .m
NSString *const MY_CONSTANT = @"MyConstantValue";

Usage
#import "filewithconstant.h"
NSLog(@"MY_CONSTANT = %@", MY_CONSTANT);

Quote:
Originally Posted by neigaard View Post
Im an old Java developer, staring on Objective-C. In Java I often declare some static variables to be used to test on later
__________________
kwigbo.com - iPhone dev blog with code snippets and more.
----------------------------------------------------------------------------
iReviewFail.com Funny reviews from the iOS App Store
kwigbo is offline   Reply With Quote
Old 03-29-2010, 07:59 PM   #5 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 3
swright is on a distinguished road
Default Possible Solution?

I'm in the same boat. I'm coming from .NET/C# and need a static instance of a class which provides cached access to remote data in my iPhone application.

I decided, for my application, it made sense to use a public property of my application delegate. This way I could still access my instance from anywhere within my application, and I could dispose of it when the application exited.

Code:
MyAppDelegate * app = (MyAppDelegate * )[[UIApplication sharedApplication] delegate];

// use app.myProperty here
Maybe this won't work or doesn't apply for you, but maybe it will help someone? It was sort of an "oh... duh" moment for me.
swright is offline   Reply With Quote
Old 03-29-2010, 11:17 PM   #6 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 12
innokria is on a distinguished road
Default this is how i did

extern int variable;

Put this declaration *outside* of any Objective-C block like @interface...@end. You can place it before the @interface or after the @end. Either way is just fine.

Then, define the variable in any .m file (it doesn't matter which one, but only place it in one file):

int variable;


then you can acces this variable globally
__________________
--------->> NIGHTANGALE <<----------
game link -->
http://itunes.apple.com/us/app/night...357004082?mt=8


video link-->
http://www.youtube.com/watch?v=c9zVshn_PhM
innokria is offline   Reply With Quote
Old 03-30-2010, 02:30 AM   #7 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 3
swright is on a distinguished road
Question

Quote:
Originally Posted by innokria View Post
extern int variable;

Put this declaration *outside* of any Objective-C block like @interface...@end. You can place it before the @interface or after the @end. Either way is just fine.

Then, define the variable in any .m file (it doesn't matter which one, but only place it in one file):

int variable;


then you can acces this variable globally
Out of curiosity, how then would you know when to dispose of the object? Or is it pretty much fine to let the memory be reclaimed when the application terminates?

Also, aren't you forced into crazy naming schemes, and all sorts of other issues, like not gaining the benefits of getter/setter methods? (It's my understanding you can't have a property out in global space)

Sorry if the comments seem dumb.. I've been kindly pandered to by .NET

Last edited by swright; 03-30-2010 at 02:39 AM.
swright is offline   Reply With Quote
Old 08-20-2010, 02:09 PM   #8 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 1
svenyonson is on a distinguished road
Default

I would try this. The static is defined in the class method, not the class interface, and so it should follow c syntax rules.

Code:
+ (MySingletonClass*) instance {
   static MySingletonClass* theInstance = nil;

   if ( theInstance == nil ) { 
      theInstance = [[MySingletonClass alloc] init];
   }

   return theInstance; 
}

// and...  

-(void) dealloc { 
   [[MySingletonClass instance] release];
}

Last edited by svenyonson; 08-20-2010 at 02:12 PM.
svenyonson is offline   Reply With Quote
Old 01-04-2011, 06:05 AM   #9 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 24
alpertayfun is on a distinguished road
Default

In the .h
extern NSString *const MY_CONSTANT;

In the .m
NSString *const MY_CONSTANT = @"MyConstantValue";

Usage
#import "filewithconstant.h"
NSLog(@"MY_CONSTANT = %@", MY_CONSTANT);

This is error :

"_MY_CONSTANT", referenced from:
_MY_CONSTANT$non_lazy_ptr in shares.o
(maybe you meant: _MY_CONSTANT$non_lazy_ptr)
ld: symbol(s) not found
collect2: ld returned 1 exit status

There is not working. How can i using global header with two uiview file ?
alpertayfun is offline   Reply With Quote
Old 01-20-2011, 12:20 PM   #10 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 3
morrismountain is on a distinguished road
Default

The concepts of the “static” variables are different in Objective-C compared to Java. Consider the following common uses of ‘static’ in Java:

public static final int MY_CONSTANT = 0; // Constant
public static String MyVar = “Foo”; // Class Variable

In all cases above, the use of keyword "static" is consistently used to refer to a variable that is brought to class and not instance scope. In the case of a static variable, meaning that multiple instances of the same class all share the state of the variable, edit the variable and it changes all instances of this class.
morrismountain is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting 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



» Advertisements
» Online Users: 453
16 members and 437 guests
Domele, Duncan C, Feldspar, karatebasker, MacBook MH, Objective Zero, patapple, Paul Slocum, peterwilli, pipposanta, PixelInteractive, Punkjumper, rubyeim54, SLIC, taylor202, Today's Posts
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,694
Threads: 94,137
Posts: 402,950
Top Poster: BrianSlick (7,990)
Welcome to our newest member, peterwilli
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:02 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0