带有 BOOL 标志的应用程序状态

发布于 2024-12-28 06:54:43 字数 334 浏览 1 评论 0原文

我的应用程序中有 5 个状态,我使用 BOOL 标志来标记它们。但这并不简单,因为当我想要更改状态时,我必须编写 5 行来更改所有标志。

你能写一些想法或简单的代码来解决这个问题吗?

code:

//need to choose second state
flag1 = false;
flag2 = true;
flag3 = false;
flag4 = false;
flag5 = false;

另外,这也太糟糕了,因为我一次可以选择 2 个状态。

PS 我发现它更现代、更苹果化。回答如下。

I've got 5 states in my app, and I use BOOL flags to mark them. But it isn't straightforward, because I have to write 5 lines to change all flags when I want to change state.

Can you write some ideas or simple code to solve this problem?

code:

//need to choose second state
flag1 = false;
flag2 = true;
flag3 = false;
flag4 = false;
flag5 = false;

Also, it's to bad because I can choose 2 states one time.

P.S.
I found modern and more Apple-way. Answer below.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦过后 2025-01-04 06:54:43

使用 typedef enum 来使用位掩码定义所有可能的状态。

注意这将为您提供最多 64 种不同的状态(在大多数平台上)。如果您需要更多可能的状态,则此解决方案将不起作用。

处理这个方案需要您完全理解并安全地处理布尔代数。

//define all possible states
typedef enum
{
    stateOne = 1 << 0,     // = 1
    stateTwo = 1 << 1,     // = 2
    stateThree = 1 << 2,   // = 4
    stateFour = 1 << 3,    // = 8  
    stateFive = 1 << 4     // = 16
} FiveStateMask;

//declare a state
FiveStateMask state;

//select single state
state = stateOne;         // = 1

//select a mixture of two states
state = stateTwo | stateFive;     // 16 | 2 = 18

//add a state 
state |= stateOne;                // 18 | 1 = 19

//remove stateTwo from our state (if set)
if ((state & stateTwo) == stateTwo)
{
    state ^= stateTwo;           // 19 ^ 2 = 17
}

//check for a single state (while others might also be selected)
if ((state & stateOne) == stateOne)
{
    //stateOne is selected, do something
}

//check for a combination of states (while others might also be selected)
if ((state & (stateOne | stateTwo)) == stateOne | stateTwo)
{
    //stateOne and stateTwo are selected, do something
}

//the previous check is a lot nicer to read when using a mask (again)
FiveStateMask checkMask = stateOne | stateTwo;
if ((state & checkMask) == checkMask)
{
    //stateOne and stateTwo are selected, do something
}

Use typedef enum to define all possible states using bitmasks.

Note this will give you a maximum of up to 64 different states (on most platforms). If you need more possible states, this solution will not work.

Handling this scheme will require you to fully understand and safely handle boolean algebra.

//define all possible states
typedef enum
{
    stateOne = 1 << 0,     // = 1
    stateTwo = 1 << 1,     // = 2
    stateThree = 1 << 2,   // = 4
    stateFour = 1 << 3,    // = 8  
    stateFive = 1 << 4     // = 16
} FiveStateMask;

//declare a state
FiveStateMask state;

//select single state
state = stateOne;         // = 1

//select a mixture of two states
state = stateTwo | stateFive;     // 16 | 2 = 18

//add a state 
state |= stateOne;                // 18 | 1 = 19

//remove stateTwo from our state (if set)
if ((state & stateTwo) == stateTwo)
{
    state ^= stateTwo;           // 19 ^ 2 = 17
}

//check for a single state (while others might also be selected)
if ((state & stateOne) == stateOne)
{
    //stateOne is selected, do something
}

//check for a combination of states (while others might also be selected)
if ((state & (stateOne | stateTwo)) == stateOne | stateTwo)
{
    //stateOne and stateTwo are selected, do something
}

//the previous check is a lot nicer to read when using a mask (again)
FiveStateMask checkMask = stateOne | stateTwo;
if ((state & checkMask) == checkMask)
{
    //stateOne and stateTwo are selected, do something
}
浪荡不羁 2025-01-04 06:54:43

您始终可以使用字节(无符号字符)大小变量并使用其位
作为标志(每一位充当一个 BOOL 标志)。

设置/清除/切换/检查位的良好说明是 此处

当然你想为此设置一种人类可读的名称
标志,即:

#define flag1 1
#define flag2 2
#define flag3 4
#define flag4 8
#define flag5 16

You can always use a byte (unsigned char) size variable using its' bits
as flags (each bit acts as one BOOL flag).

Good instructions to set/clear/toggle/check a bit is here.

Offcourse you'd want to set kind of human readable names for this
flags, i.e.:

#define flag1 1
#define flag2 2
#define flag3 4
#define flag4 8
#define flag5 16
梦初启 2025-01-04 06:54:43

现在我们有另一种标志选项。它是NS_ENUM

typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
    UITableViewCellStyleDefault,
    UITableViewCellStyleValue1,
    UITableViewCellStyleValue2,
    UITableViewCellStyleSubtitle
};

第一个参数代表类型,第二个参数代表名称。

Nowadays we have got another option for flags. It is NS_ENUM.

typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
    UITableViewCellStyleDefault,
    UITableViewCellStyleValue1,
    UITableViewCellStyleValue2,
    UITableViewCellStyleSubtitle
};

First arg for type and second for name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文