0

why does this code not work for referencing a const from a class?

Background: I want to be able to reference a constant value from a class in a class variable type approach, as this is where it makes sense to source. Trying find the best way to effectively have the class offer up an exposed constant. I tried the below but it doesn't seem to work, I get "ERROR: property 'titleLablePrefix' not found on object of type 'DetailedAppointCell'"

@interface DetailedAppointCell : UITableViewCell {
}
  extern NSString * const titleLablePrefix;
@end

#import "DetailedAppointCell.h"
@implementation DetailedAppointCell
  NSString * const titleLablePrefix = @"TITLE: ";
@end

// usage from another class which imports
NSString *str = DetailedAppointCell.titleLablePrefix;  // ERROR: property 'titleLablePrefix' not found on object of type 'DetailedAppointCell'
Greg
  • 34,042
  • 79
  • 253
  • 454

2 Answers2

2

You can use directly as NSString *str = titleLablePrefix; if your external linkages are proper.

Tatvamasi
  • 2,537
  • 19
  • 14
  • what do you mean by "if your external linkages are proper". I just tried what you suggested and interestingly it work for one such variable but not another - the one it didn't work for I got: Undefined symbols for architecture i386: / ld: symbol(s) not found for architecture i386 / collect2: ld returned 1 exit status". – Greg Jun 16 '11 at 05:37
1

Objective C doesn't support class variables/constants, but it supports class methods. You can use the following solution:

@interface DetailedAppointCell : UITableViewCell {
}
+ (NSString*)titleLablePrefix;
@end

#import "DetailedAppointCell.h"
@implementation DetailedAppointCell
+ (NSString*)titleLablePrefix {
  return @"TITLE: ";
}
@end

// usage from another class which imports
NSString *str = [DetailedAppointCell titleLablePrefix];

p.s. Dot syntax is used for instance properties. You can learn more about Objective C here: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocObjectsClasses.html

Andrew Kovzel
  • 601
  • 5
  • 7