开发者

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

开发者 https://www.devze.com 2023-03-13 19:22 出处:网络
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

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 typ开发者_JAVA百科e 'DetailedAppointCell'


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号