Movatterモバイル変換


[0]ホーム

URL:


Next:, Previous:, Up:GNU Objective-C Features   [Contents][Index]


9.5 Constant String Objects

GNU Objective-C provides constant string objects that are generateddirectly by the compiler. You declare a constant string object byprefixing a C constant string with the character ‘@’:

  id myString = @"this is a constant string object";

The constant string objects are by default instances of theNXConstantString class which is provided by the GNU Objective-Cruntime. To get the definition of this class you must include theobjc/NXConstStr.h header file.

User defined libraries may want to implement their own constant stringclass. To be able to support them, the GNU Objective-C compiler providesa new command line options-fconstant-string-class=class-name.The provided class should adhere to a strict structure, the sameasNXConstantString’s structure:

@interface MyConstantStringClass{  Class isa;  char *c_string;  unsigned int len;}@end

NXConstantString inherits fromObject; user classlibraries may choose to inherit the customized constant string classfrom a different class thanObject. There is no requirement inthe methods the constant string class has to implement, but the finalivar layout of the class must be the compatible with the givenstructure.

When the compiler creates the statically allocated constant stringobject, thec_string field will be filled by the compiler withthe string; thelength field will be filled by the compiler withthe string length; theisa pointer will be filled withNULL by the compiler, and it will later be fixed up automaticallyat runtime by the GNU Objective-C runtime library to point to the classwhich was set by the-fconstant-string-class option when theobject file is loaded (if you wonder how it works behind the scenes, thename of the class to use, and the list of static objects to fixup, arestored by the compiler in the object file in a place where the GNUruntime library will find them at runtime).

As a result, when a file is compiled with the-fconstant-string-class option, all the constant string objectswill be instances of the class specified as argument to this option. Itis possible to have multiple compilation units referring to differentconstant string classes, neither the compiler nor the linker impose anyrestrictions in doing this.


Next:compatibility_alias, Previous:Garbage Collection, Up:GNU Objective-C Features   [Contents][Index]


[8]ページ先頭

©2009-2025 Movatter.jp