Note that there are some explanatory texts on larger screens.

plurals
  1. POObjective-c constant static NSArray
    primarykey
    data
    text
    <p>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:</p> <p>PolygonShape.h:</p> <pre><code>... extern NSDictionary *polygonTypes; @interface PolygonShape ... </code></pre> <p>PolygonShape.m:</p> <pre><code>... 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]; } } ... </code></pre> <p>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:</p> <p>PolygonShape.m:</p> <pre><code>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; } </code></pre> <p>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).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload