整数常量 [英] Integer constants

查看:115
本文介绍了整数常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我声明一个字符串常量,如下:

If I declare a string constant like so:

您应该创建一个头文件

// Constants.h
extern NSString * const MyFirstConstant;
extern NSString * const MySecondConstant;
//etc.

您可以将此文件包含在使用常量的每个文件中,或在预编译的标题中项目。

You can include this file in each file that uses the constants or in the pre-compiled header for the project.

您在.m文件中定义这些常量,如

You define these constants in a .m file like

// Constants.m

NSString * const MyFirstConstant = @"FirstConstant";
NSString * const MySecondConstant = @"SecondConstant";

我如何定义整数常量?

推荐答案

使用NSInteger替换NSString *。

Replace NSString* with NSInteger.

对于任何常量类型,无论是int / float还是NSString或NSInteger这样的原始类型都是如此。

This is true of any constant type, be it a primitive such as int/float, or a class such as NSString or NSInteger.

唯一需要注意的是你需要一个常量还是一个指向常量的指针(如withNSString),以及如何在.m文件中初始化

The only thing to be aware of is whether you desire a constant or a pointer to a constant (such as withNSString), and how it's initialized in the .m file

整数示例:

// constants.h
extern NSInteger const MyIntegerConstant;

// constants.m
NSInteger const MyIntegerConstant = 666;

(注意:为什么NSInteger而不是普通的int,参见这篇文章

(Note: for the reason why NSInteger instead of just regular "int", see this post)

类示例:

// constants.h
extern MyClass* const MyClassConstant;

// constants.m
MyClass* const MyClassConstant= [[MyClass alloc] initWith: paramOne and:paramTwo];

这篇关于整数常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆