Objective-C在运行时将参数修改为方法
我想在运行时更改参数的值,并想知道这在Obj-C中是如何工作的 我有一个循环,“n”的值是0,每个循环增加1。当n移动时,如何将传递的参数增加1Objective-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; 下次通过
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];
}