0

クラスから const を参照するためにこのコードが機能しないのはなぜですか?

背景: クラス変数型アプローチでクラスから定数値を参照できるようにしたいと考えています。クラスに公開された定数を効果的に提供させる最善の方法を見つけようとしています。以下を試しましたが、うまくいかないようです。

@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'
4

2 に答える 2

2

NSString *str = titleLablePrefix; 外部リンケージが適切であるかのように直接使用できます。

于 2011-06-16T05:28:33.093 に答える
1

Objective C はクラス変数/定数をサポートしていませんが、クラス メソッドをサポートしています。次の解決策を使用できます。

@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];

ps ドット構文は、インスタンス プロパティに使用されます。Objective C の詳細については、http: //developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocObjectsClasses.htmlをご覧ください。

于 2011-06-16T06:16:35.917 に答える