Is there a way to make Objective-C support a multi-variable switch construct?(有没有办法让 Objective-C 支持多变量开关结构?)
问题描述
我想知道:有没有办法让 Objective-C 支持多变量 switch
构造?
I was wondering: is there a way to make Objective-C support a multi-variable switch
construct?
我的意思是,我经常不得不处理解决方案取决于一对变量而不是单个变量的问题.对于单个变量值的长列表,可以使用简单的 switch/case
构造:
I mean, very often I have to deal with problems in which the solution depends on a pair of variables instead of a single one. For a long list of single variable values one can use a simple switch/case
construct:
switch (var) {
case 0: ...
case 1: ...
default: ...
}
但是当你必须处理两个值的组合时,你经常会碰巧做这样的事情:
But when you have to deal with the combination of two values you often happen to do something like this:
switch (var1) {
case 0:
switch (var2) {
case 0: ...
case 1: ...
}
case 1:
switch (var2) {
case 0: ...
case 1: ...
}
...
}
而且它变得越来越复杂......我真正想做的是这样的事情:
And it gets more and more complicated ... What I'd really love to do is something like this:
switch (var1, var2) {
case (0,0) : ...
case (1,0) : ...
case (*,1) : ...
default: ...
}
这将导致更简单和可读的结构.是否有任何解决方案来支持这种结构?还是这个略有不同?
that will result in a more simple and readable construct. Is there any solution to support such a structure? Or a slight variation of this one?
推荐答案
我喜欢 saphrosit 的回答,但我会尽量让它易于理解.
I like the answer from saphrosit but I'll make try to make it easy to understand.
将您的问题的可能结果想象为网格中的正方形,其中网格边缘的一条边表示 var1 的值,另一条边表示 var2 的可能值,那么如果您通过网格你会得到这样的东西
Imagine the possible outcomes of your problem as squares in a grid where one edge of the edge of the grid represents values of var1 and the other edge represents the possible values of var2, then if you incrementialy counted through the squares of the of the grid you would it get something like this
|| var1 |
|| 0 | 1 | 2 | ... | j | ... | n-1 |
======++=====================================================================|
0 || 0 | 1 | 2 | ... | j | ... | n-1 |
---||---------+-----------+-----------+-----+-----------+-----+-----------|
1 || n | n+1 | n+2 | ... | n+j | ... | n+(n-1) |
---||---------+-----------+-----------+-----+-----------+-----+-----------|
2 || 2*n | 2*n+1 | 2*n+2 | ... | 2*n+j | ... | 2*n+(n-1) |
v ---||---------+-----------+-----------+-----+-----------+-----+-----------|
a || . | . | . | | . | | . |
r ---||---------+-----------+-----------+-----+-----------+-----+-----------|
2 i || i*n | i*n+1 | i*n+2 | ... | i*n+j | ... | i*n+(n-1) |
---||---------+-----------+-----------+-----+-----------+-----+-----------|
|| . | . | . | | . | | . |
----||---------+-----------+-----------+-----+-----------+-----+-----------|
m-1 || (m-1)*n | (m-1)*n+1 | (m-1)*n+2 | ... | (m-1)*n+j | ... | mn-1 | <-- (m-1)*n+(n-1) = m*n-n + (n-1) = mn-1
------||---------+-----------+-----------+-----+-----------+-----+-----------|
这将被称为行主矩阵,因为您从跨行开始计数.还有一个列主矩阵,您首先开始倒计时,而不是倒计时.这就是矩阵在 C BLAS 库中的存储方式,因此对许多人来说应该非常相似.
This would is called a row major matrix since you start by counting accross the rows. There is also a column major matrix where you start counting down first instead of across. This is how matrixes are stored in the C BLAS library so it should be very fimilar to many people.
在您的情况下,您正在寻找的结果可以被解决为 var3 = var2*n + var1
您可以在代码中将其布局为
In your case the outcome you're looking for can be addressed as var3 = var2*n + var1
you could lay this out in code as
#define N 10 // Maximum size of var1
int main() {
int var1 = 1;
int var2 = 1;
switch(var1 + var2 * N){
case 1 + 1 * N: printf("One One"); break;
case 2 + 2 * N: printf("Two Two"); break;
default:
printf("Bada Bing");
}
return 0;
}
注意:这里之前的代码不会工作,这工作.
NOTE: the code that was here earlier wouldn't have worked, this works.
这篇关于有没有办法让 Objective-C 支持多变量开关结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:有没有办法让 Objective-C 支持多变量开关结构?
基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01