Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6367067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:30:07+00:00 2026-05-25T00:30:07+00:00

I am a Java programmer, learning Objective-C and I have a problem with implementation

  • 0

I am a Java programmer, learning Objective-C and I have a problem with implementation of variables, similar to static final class variables in Java. In class PolygonShape, I would like to have NSDictionary with polygon types, which can be accessed from within and outside of the class. I already tried the following:

PolygonShape.h:

...
extern NSDictionary *polygonTypes;

@interface PolygonShape
...

PolygonShape.m:

...    
NSDictionary *polygonTypes = nil;

@implementation PolygonShape

- (id)init {
    self = [super init];
    if (self) {
        if(!polygonTypes) {
            polygonTypes = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"triangle", [NSNumber numberWithInt: 3], @"quadrilateral", [NSNumber numberWithInt: 4],
                            @"pentagon", [NSNumber numberWithInt: 5], @"hexagon", [NSNumber numberWithInt: 6],
                            @"heptagon", [NSNumber numberWithInt: 7], @"octagon", [NSNumber numberWithInt: 8],  
                            @"enneagon", [NSNumber numberWithInt: 9], @"decagon", [NSNumber numberWithInt: 10],
                            @"hendecagon", [NSNumber numberWithInt: 11], @"dodecagon", [NSNumber numberWithInt: 12], nil];
        }
    }
...

But this is not good enough, because if I want to access polygon types from elsewhere (e.g. main.m) without initializing instance of PolygonShape, variable polygonTypes is nil. So I used static function which works fine:

PolygonShape.m:

static NSDictionary *polygonTypes = nil;

@implementation PolygonShape

...

+ (NSDictionary *) polygonTypesDicionary {
    if(!polygonTypes) {
        polygonTypes = [NSDictionary dictionaryWithObjectsAndKeys:
                        @"triangle", [NSNumber numberWithInt: 3], @"quadrilateral", [NSNumber numberWithInt: 4],
                        @"pentagon", [NSNumber numberWithInt: 5], @"hexagon", [NSNumber numberWithInt: 6],
                        @"heptagon", [NSNumber numberWithInt: 7], @"octagon", [NSNumber numberWithInt: 8],  
                        @"enneagon", [NSNumber numberWithInt: 9], @"decagon", [NSNumber numberWithInt: 10],
                        @"hendecagon", [NSNumber numberWithInt: 11], @"dodecagon", [NSNumber numberWithInt: 12], nil];
    }
    return polygonTypes;
}

Now this is ok, but I wonder, what is the best way to do this and is it possible to use extern for NSDictionary without having to initialize it in a class method? (and I know about singelton classes but I would really like to have constant array of polygon types inside PolygonShape class).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:30:08+00:00Added an answer on May 25, 2026 at 12:30 am

    I am a Java programmer, […]

    There’s your problem right there.

    Objective-C is a quite different language from Java in a lot of ways, and a lot of idioms may be quite foreign to you.

    As an example; why do you want, or need, to know what kind of polygon shapes the Polygon class can handle?

    If you are trying to create a polygon with a certain number of corners, but can’t, then yes, you need to know.

    Apart from that? Not very useful info, at least as far as I can see.

    In fact, why would a polygon shape even need to know what other shapes exist?

    Shed for a moment the very idea of static, and reconsider your problem. Enlightenment is sure to follow.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a Java programmer, and I like my compiler, static analysis tools and unit
As the question states, i am a C#/Java programmer who is interested in (re)learning
Possible Duplicate: Should I learn C before learning C++? As a professional (Java) programmer
As a java programmer I'm quite comfortable with using JAXB and similar, for example
I am a pretty experienced Ruby, Objective C, and Java programmer and I was
I am a Java programmer, and I am learning Ruby... But I don't get
I've recently started learning Python (long time Java programmer here) and currently in the
I'm a good programmer, but I have zero network experience. Basically, I'd like to
Hey everyone. I am an experienced java programmer and am just learning C++. Now
I'm a novice programmer with basic Java experience, and currently learning Python. I've stumbled

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.