Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective-C在运行时将参数修改为方法_Objective C_Cocoa_Cocoa Touch - Fatal编程技术网

Objective-C在运行时将参数修改为方法

Objective-C在运行时将参数修改为方法,objective-c,cocoa,cocoa-touch,Objective C,Cocoa,Cocoa Touch,我想在运行时更改参数的值,并想知道这在Obj-C中是如何工作的 我有一个循环,“n”的值是0,每个循环增加1。当n移动时,如何将传递的参数增加1 UIViewSubclass *uiViewSubclass = [[UIViewSubclass alloc] initWithValue:([value integerValue]) andPlacement:kPlacement0; 下次通过

我想在运行时更改参数的值,并想知道这在Obj-C中是如何工作的

我有一个循环,“n”的值是0,每个循环增加1。当n移动时,如何将传递的参数增加1

UIViewSubclass *uiViewSubclass = [[UIViewSubclass alloc] initWithValue:([value integerValue]) 
                                                   andPlacement:kPlacement0;
下次通过循环,我希望第二个参数读作:和placement:kPlacement1; 然后:和位置:kPlacement2;而且

我是否将kPlacement设置为字符串,并通过附加字符串:[[NSNumber numberWithInt:n]stringValue]


什么是Obj-C/Cocoa方法?

您不能在运行时修改源代码或生成变量引用。Objective-C不是那么有活力

如果
kPlacement0
kPlacementMax
的值是连续的,您可以使用
for
循环直接通过它们:

for (MyPlacement placement = kPlacement0; placement += kPlacementIncrement; placement <= kPlacementMax) {
    UIViewSubclass *instanceOfUIViewSubclass = [[UIViewSubclass alloc] initWithValue:([value integerValue])
                                                                        andPlacement:placement];
    //Do something with instanceOfUIViewSubclass.
    [instanceOfUIViewSubclass release];
}

您可能会想出比
kPlacement0
等更多的描述性名称。当您想按数字引用它们时,请这样做;当你想用名字来引用它们时,给它们起个好名字。

更明确地说:当你发现自己想要一堆名为
suchhandsuch1
suchhandsuch2
等的变量时,你可能想用数组。
enum { numPlacements = <#Insert the number of placement constants here#> };
MyPlacement placements[numPlacements] = {
    kPlacement0,
    kPlacement1,
    kPlacement2,
    ⋮
}

for (unsigned i = 0U; i < numPlacements; ++i) {
    UIViewSubclass *instanceOfUIViewSubclass = [[UIViewSubclass alloc] initWithValue:([value integerValue])
                                                                        andPlacement:placements[i]];
    //Do something with instanceOfUIViewSubclass.
    [instanceOfUIViewSubclass release];
}