Ios 用目标c中的值存储任何类型的常量
我不熟悉objective c,我来自.NET背景。我想创建一个静态类,它将在.NET中存储任何类型的常量(NSString、NSArray等)。我将创建一个类,并使用“public const”声明常量。如何在目标C中实现同样的目标?我曾考虑过创建一个类,它是NSObject的一个子类,但我不确定,也不想使用#define。如果有任何帮助,我将不胜感激 样本:Ios 用目标c中的值存储任何类型的常量,ios,objective-c,constants,Ios,Objective C,Constants,我不熟悉objective c,我来自.NET背景。我想创建一个静态类,它将在.NET中存储任何类型的常量(NSString、NSArray等)。我将创建一个类,并使用“public const”声明常量。如何在目标C中实现同样的目标?我曾考虑过创建一个类,它是NSObject的一个子类,但我不确定,也不想使用#define。如果有任何帮助,我将不胜感激 样本: public class ApplicationConstants() { public const string global
public class ApplicationConstants()
{
public const string globalString="This is a global string";
public const int sample=1;
}
我想在目标c中做类似的事情。任何指向示例代码的链接都会非常有用。我还想以同样的方式存储阵列看一看。例如,您可以在标题中定义字符串常量,如下所示:
FOUNDATION_EXPORT NSString *const aConstantString;
然后在实现文件中初始化它们:
NSString *const aConstantString = @"Some constant string";
如果您想知道什么是FOUNDATION\u EXPORT
,请在NSObjCRuntime.h
中定义d为extern
查看。例如,您可以在标题中定义字符串常量,如下所示:
FOUNDATION_EXPORT NSString *const aConstantString;
然后在实现文件中初始化它们:
NSString *const aConstantString = @"Some constant string";
如果您想知道什么是FOUNDATION\u EXPORT
,那么在NSObjCRuntime.h
中,它是#将d定义为extern
我想您对更改编码域有些困惑
在.NET中
public const string globalString="This is a global string";
定义一个不可变的常量字符串,这意味着您不能更改由globalString
指向的内存位置的内容。您可以在类级别定义这样的变量
另一方面,Objective-C作为纯C的继承者,并不是完全面向对象的。相反,它支持在模块级别声明不可变变量。这意味着,您可以在标头中定义一个不可变的变量,并且可以从导入该标头的任何模块访问该变量
因此,您可以在模块级别声明常量变量,例如,在标题顶部
extern const NSString* const globalString;
然后需要在实现(.m)文件的顶部对其进行初始化,如
const NSString* const globalString = @"This is a global string";
这里,const NSstring*const
表示变量globalString
指向唯一的内存位置(尾随的const
),它包含一些无法更改的不可变值(@“这是一个全局字符串”)(前导的const
)。因此,它就相当于您的.NET声明
这里要记住的另一件事是,NSString
、NSArray
、NSDictionary
等本质上是不可变的(与NSMutableString
、NSMutableArray
、NSMutableDictionary
等作为其可变对应项)您不需要为它们指定前导的const
关键字,因为它是隐式定义的。因此,可以这样写:
extern NSString* const globalString;
用于声明常量字符串
希望这有助于理解。我认为您对更改编码域有些困惑
在.NET中
public const string globalString="This is a global string";
定义一个不可变的常量字符串,这意味着您不能更改由globalString
指向的内存位置的内容。您可以在类级别定义这样的变量
另一方面,Objective-C作为纯C的继承者,并不是完全面向对象的。相反,它支持在模块级别声明不可变变量。这意味着,您可以在标头中定义一个不可变的变量,并且可以从导入该标头的任何模块访问该变量
因此,您可以在模块级别声明常量变量,例如,在标题顶部
extern const NSString* const globalString;
然后需要在实现(.m)文件的顶部对其进行初始化,如
const NSString* const globalString = @"This is a global string";
这里,const NSstring*const
表示变量globalString
指向唯一的内存位置(尾随的const
),它包含一些无法更改的不可变值(@“这是一个全局字符串”)(前导的const
)。因此,它就相当于您的.NET声明
这里要记住的另一件事是,NSString
、NSArray
、NSDictionary
等本质上是不可变的(与NSMutableString
、NSMutableArray
、NSMutableDictionary
等作为其可变对应项)您不需要为它们指定前导的const
关键字,因为它是隐式定义的。因此,可以这样写:
extern NSString* const globalString;
用于声明常量字符串
希望有助于理解。Objective-C没有类变量。有两种方法可以实现相同的功能:
使用全局变量,其名称指示它们所属的类。例如
标题:
extern NSString *const BCOpinionatedClassWillStartOpiningNotification;
@interface BCOpinionatedClass
+(NSInteger)theBestIntegerEver;
@end
实施:
NSString *const BCOpinionatedClassWillStartOpiningNotification = @"BCOpinionatedClassWillStartOpiningNotification";
@implementation OpinionatedClass
+(NSInteger)theBestIntegerEver
{
return 1;
}
@end
2创建一个返回值的类方法。例如:
标题:
extern NSString *const BCOpinionatedClassWillStartOpiningNotification;
@interface BCOpinionatedClass
+(NSInteger)theBestIntegerEver;
@end
实施:
NSString *const BCOpinionatedClassWillStartOpiningNotification = @"BCOpinionatedClassWillStartOpiningNotification";
@implementation OpinionatedClass
+(NSInteger)theBestIntegerEver
{
return 1;
}
@end
Objective-C没有类变量。有两种方法可以实现相同的功能:
使用全局变量,其名称指示它们所属的类。例如
标题:
extern NSString *const BCOpinionatedClassWillStartOpiningNotification;
@interface BCOpinionatedClass
+(NSInteger)theBestIntegerEver;
@end
实施:
NSString *const BCOpinionatedClassWillStartOpiningNotification = @"BCOpinionatedClassWillStartOpiningNotification";
@implementation OpinionatedClass
+(NSInteger)theBestIntegerEver
{
return 1;
}
@end
2创建一个返回值的类方法。例如:
标题:
extern NSString *const BCOpinionatedClassWillStartOpiningNotification;
@interface BCOpinionatedClass
+(NSInteger)theBestIntegerEver;
@end
实施:
NSString *const BCOpinionatedClassWillStartOpiningNotification = @"BCOpinionatedClassWillStartOpiningNotification";
@implementation OpinionatedClass
+(NSInteger)theBestIntegerEver
{
return 1;
}
@end
您不应该创建这个对象(实际上也不应该在C#中创建这个对象,但我们现在正在讨论ObjC)。对于“一个程序的所有常量”的类来说,没有面向对象的意义。正确地放置这些值的位置是在需要它们的类中。因此,例如,如果为NSNotification
名称创建字符串常量,则应将其定义放入发布该通知的类中
通常,您根本不会将这些类型的常量放在类中。您可以使用前缀命名来指示它们与什么相关。因此,例如,UIApplication.h
头文件中定义了UIApplication.h
idbecomeactivityfication
字符串常量,因此:
UIKIT_EXTERN NSString *const UIApplicationDidBecomeActiveNotification;
UIKIT_EXTERN
是一个可移植性宏,解析为EXTERN