如何NSDictionary使用数组计数创建多个变量?
这基本上就是我提出的,但我不确定如何使用Objective-C语法来完成这项工作.doesntContainAnother是一个NSArray.我希望字典的名称使用当前值loopInt.
int *loopInt = 0;
while (doesntContainAnother.count <= loopInt) {
NSMutableDictionary *[NSString stringWithFormat:@"loopDictionary%i", loopInt] = [[[NSMutableDictionary alloc] init] autorelease];
[NSString stringWithFormat:@"loopDictionary%i", loopInt] = [NSDictionary dictionaryWithObject:[array1 objectAtIndex:loopInt]
forKey:[array2 objectAtIndex:loopInt]];
loopInt = loopInt + 1;
}
Run Code Online (Sandbox Code Playgroud) 假设我的代码中定义了很多变量,并带有这样的名称
public javax.swing.JPanel BenderPanel1;
public javax.swing.JPanel BenderPanel2;
public javax.swing.JPanel BenderPanel3;
etc...
Run Code Online (Sandbox Code Playgroud)
所以他们的一般类型是这样的:BenderPanel"NUMBER".
我想访问其中一些并设置其可见性,.setVisible(false);但我想访问的那些面板的数量是在运行时用户定义的.
是否有任何可能的方法通过库将数字附加到每个变量的末尾,以便在for循环中访问它,如下所示:
for (int i=1; i<=UserInput; i++)
{
BenderPanel"i".setVisible(false); // Watch this "i" right there.
}
Run Code Online (Sandbox Code Playgroud)
无需要将它们添加在ArrayList第一和具有明显的方式做到这一点?
我有以下简单的功能,但其ggplot命令不起作用.从命令行给出时,该命令可以正常工作:
> testfn <- function(gdf, first, second){
library(ggplot2)
print(ggplot(gdf, aes(first, second)) + geom_point())
}
>
> testfn(mydataf, vnum1, vnum2)
Error in eval(expr, envir, enclos) : object 'second' not found
>
> ggplot(mydataf, aes(vnum1, vnum2)) + geom_point()
> (plots graph without any error)
Run Code Online (Sandbox Code Playgroud)
我试着用aes_string而不是aes; 并且还使用x=first, y=second.事情有所改善,有一点被绘制出来!X和Y轴显示与该点相关的数字作为标签.仅绘制第一行.问题出在哪儿.谢谢你的帮助.
我听说可以使用 Unicode 变量名-fextended-identifiersGCC 中的标志来使用 Unicode 变量名。所以我用C++编写了一个测试程序,但它无法编译。
#include <iostream>\n#include <string>\n\n#define \xc2\xac !\n#define \xe2\x89\xa0 !=\n#define \xc2\xab <<\n#define \xc2\xbb >>\n\n/* uniq: remove duplicate lines from stdin */\nint main() {\n std::string s;\n std::string t = "";\n while (cin \xc2\xbb s) {\n if (s \xe2\x89\xa0 t)\n cout \xc2\xab s;\n t = s;\n }\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n我收到这些错误:
\n#include <iostream>\n#include <string>\n\n#define \xc2\xac !\n#define \xe2\x89\xa0 !=\n#define \xc2\xab <<\n#define \xc2\xbb >>\n\n/* uniq: remove duplicate lines from stdin */\nint main() {\n std::string s;\n std::string t …Run Code Online (Sandbox Code Playgroud) 我需要从变量列表中找到最大值.但是,max()返回变量的内容而不是变量名.有没有办法获取名称而不是内容?
快速示例代码:
jan <- 0
feb <- 0
mar <- 0
#for testing purposes - just select a random month and add 10
s1 <- sample(1:3, 1)
if (s1==1) {
jan <- jan + 10
}
if (s1==2) {
feb <- feb + 10
}
if (s1==3) {
mar <- mar + 10
}
final <- max(jan, feb, mar)
final
Run Code Online (Sandbox Code Playgroud)
结果将始终为10.这没有帮助...有没有办法让返回月/变量名称?(即"jan"而不是"10")
谢谢!
提前谢谢,任何帮助的人.:)
对于那些经验丰富的人来说,这似乎是一个简单的答案,但我已经浏览了互联网以及一些参考书,并没有找到这个问题的直接答案,所以希望它也可能对其他人有所帮助.
我目前正在从Actionscript过渡到Typescript,并且对vanilla Javascript有相当多的经验,所以简单地用Javascript术语,如果我想动态引用变量,我可以简单地使用这样的东西:
var myTextA = "Hello!";
var myTextB = "Goodbye!";
var textList = ["A", "B"];
console.log("I want to say " + this["myText" + textList[0]]);
Run Code Online (Sandbox Code Playgroud)
结果当然是:"我想说你好!"
在Typescript中,对于类中的私有变量,这似乎不可能,并导致以下TSC错误:
"Index signature of object type implicitly has an 'any' type."
Run Code Online (Sandbox Code Playgroud)
至于我可以收集的,typescript希望我在动态构造中以某种方式声明变量类型,但是我找不到任何关于如何执行此操作的明确参考.
为了我自己的目的,把它放在上下文中,我正在开发一个项目,我需要循环一系列配对变量,这些变量都有相同的开头,但结尾略有不同,所以简单地将变量本身放在一个数组中就不是一个选项(或者无论如何都是一个混乱的解决方案).
例如:
var myVar1a, myVar1b, myVar2a, myVar2b etc...
Run Code Online (Sandbox Code Playgroud)
所以在循环中,我想要参考每个的a和b:
console.log(this["myVar" + (i+1) + "a");
console.log(this["myVar" + (i+1) + "b");
Run Code Online (Sandbox Code Playgroud)
任何帮助深表感谢!!
我试图弄清楚如何从对象本身获取 powershell 变量的名称。
我这样做是因为我正在对通过引用传递到函数中的对象进行更改,因此我不知道该对象是什么,并且我正在使用 Set-Variable cmdlet 将该变量更改为只读。
# .__NEEDTOGETVARNAMEASSTRING is a placeholder because I don't know how to do that.
function Set-ToReadOnly{
param([ref]$inputVar)
$varName = $inputVar.__NEEDTOGETVARNAMEASSTRING
Set-Variable -Name $varName -Option ReadOnly
}
$testVar = 'foo'
Set-ToReadOnly $testVar
Run Code Online (Sandbox Code Playgroud)
我浏览了很多类似的问题,但找不到任何具体答案。我想完全在函数内部使用变量——我不想依赖于传递附加信息。
另外,虽然可能有更简单/更好的设置只读的方法,但我长期以来一直想知道如何可靠地从变量中提取变量名称,所以请专注于解决该问题,而不是我在其中的应用这个例子。
variables powershell readonly pass-by-reference variable-names
我在 vim 中编写了一些 Perl 代码,不小心在变量名中输入了一个单引号字符,并注意到它以与普通单引号字符串不同的颜色突出显示它。
我觉得这很奇怪,所以我写了一个小测试程序(如上所示)并尝试运行它以查看 Perl 将如何处理它,但我收到了这个错误:
"my" variable $var::with::apostrophes can't be in a package
这里到底发生了什么?是否存在变量名称中的单引号实际上有效的情况?如果是这样,单引号在这种情况下使用时有什么含义?
#check if the name is valid
function myfunc()
{
#check "${1}"
#echo "valid/invalid"
}
#these should return valid
myfunc "my_number"
myfunc "my_number1"
#these should return ivalid
myfunc "1my_number"
myfunc "1my _number"
myfunc "my number"
myfunc "my_number?"
Run Code Online (Sandbox Code Playgroud)
等变量名只能有字母,数字(但不能在开头),...和java的所有规则一样......
我可以使用任何功能吗?我不想重新发明轮子......
我想我理解堆栈和堆栈之间的主要区别.
在以下程序中,在堆上创建大小为n的Object.在堆栈上创建一个指向这个sofar无名对象的指针p,它占用4个字节(至少在我的系统上).如果我理解得很好,因为引用不使用额外的内存,不会分配更多的内存(除了堆栈上的main()返回的int).
Class Object; // n bytes
int main() {
Object* p = new Object();
Object& r = *p;
// ...
}
Run Code Online (Sandbox Code Playgroud)
但是,内存管理尚不完全清楚:
1)名称p和r存储在哪里?它们都是本地名称,所以我想它们也应该进入堆栈?这不需要额外的内存来存储变量名称和它引用的内存部分之间的绑定吗?
2)指针存储的类型在哪里?指针在堆栈上只占用4个字节,(我认为)是存储内存地址的确切大小.计算机如何知道在该地址可以找到哪种类型?
3)与(2)类似,堆上的Object需要n个字节的存储空间和唯一(直接)引用它的0个字节.这个对象的类型存储在哪里,所以当使用r时,它知道它是哪种类型?
4)我知道编译后的程序也驻留在某个地方的内存中以指导它的执行.这是在堆栈还是堆上,还是这仍然是内存的另一部分?
variable-names ×10
variables ×3
c++ ×2
r ×2
append ×1
function ×1
gcc ×1
ggplot2 ×1
heap ×1
java ×1
javascript ×1
linux ×1
max ×1
memory ×1
objective-c ×1
perl ×1
powershell ×1
readonly ×1
shell ×1
stack ×1
string ×1
typescript ×1
unicode ×1