在objective-c中使用参数作为变量名

时间:2010-01-19 15:31:02

标签: objective-c string function

我是Objective-c的新手并且遇到了问题。是否可以将函数的参数用作某些变量名?

例如,假设我有一堆图像:aPic1,aPic2,bPic1,bPic2,cPic1,cPic2,我想创建一个动作,这样每次单击一个按钮时,视图控制器将隐藏Pic1和根据点击的按钮显示Pic2。

- (void) performAction:(NSMutableString *) text
{
    [text appendString:@"Pic1"].hidden = YES;    //I wanna hide textPic1, which is a UIImageView
    [text appendString:@"Pic2"].hidden = NO;     //I wanna show textPic2, which is also a UIImageView
}

我知道在这种情况下我不应该使用NSString或NSMutableString。任何人都知道如何通过这种功能实现我的目标?感谢。

3 个答案:

答案 0 :(得分:5)

是的,你可以。 (c):)

对于我建议的解决方案,您需要能够通过方法访问(设置/获取)变量(使用属性轻松完成或编写自己的setter和getter)。

以下是一个例子:

- (void)performAction:(NSMutableString *)text {
    [(UIImageView *)[self performSelector:NSSelectorFromString([text stringByAppendingString:@"Pic1"])] setHidden:YES];
    [(UIImageView *)[self performSelector:NSSelectorFromString([text stringByAppendingString:@"Pic2"])] setHidden:NO];
}

至于属性,我实际上只想给你一个示例代码,以便开始使用Objective-C ASAP的这个伟大功能,我将这样做。虽然我不确定深入研究这个主题,因为它可能需要太多的互联网论文,所以这就是为什么在示例代码之后我还会添加一些链接以供进一步阅读。

@interface AClass : NSObject {
    // Here's where you declare variables
    NSObject *objectForInternalUseWeWantToBeRetained;
    id linkToObjectUsuallyNotRetained;
    int nonObjectVariable;
    BOOL aVariableWithARenamedGetter;
}

// And here's where their properties are declared
@property (nonatomic, retain) NSObject *objectForInternalUseWeWantToBeRetained;
@property (nonatomic) id linkToObjectUsuallyNotRetained;
@property (nonatomic, assign) int nonObjectVariable;
@property (nonatomic, assign, getter=renamedVariableGetter) BOOL aVariableWithARenamedGetter;


@end


@implementation AClass

// Here we command the machine to generate getters/setters for these properties automagically
@synthesize objectForInternalUseWeWantToBeRetained, linkToObjectUsuallyNotRetained, nonObjectVariable, aVariableWithARenamedGetter;

// But you can implement any of the getters/setters by yourself to add some additional behaviour
- (NSObject *)objectForInternalUseWeWantToBeRetained {
    // Some additional non-usual stuff here

    // And after that we do the getters job - return the variables value
    return objectForInternalUseWeWantToBeRetained;
}

// And of course we don't forget to release all the objects we retained on dealloc
- (void)dealloc {
    [objectForInternalUseWeWantToBeRetained release];

    [super dealloc];
}


@end

// And here's where their properties are declared
@property (nonatomic, retain) UIImageView *testPic1;
@property (nonatomic, retain) UIImageView *testPic2;


@end

警告:我很快就完成了这个问题。 Here's a nice tutorial at CocoaCast blog - Properties in Objective-C 2.0,我认为这可能是一个很好的起点。顺便说一下,他们提供了大量的学习资料(播客,截屏视频等),因此进一步浏览他们的网站可能会有用。当然,了解Objective-C和Cocoa的主要内容是官方文档,here's where it is about properties

答案 1 :(得分:3)

这是可能的,但是以迂​​回的方式。您可以在课堂上制作ivars @properties,然后通过以下方式访问它们:

id myIvar = [self valueForKey:[text appendString:@"Pic1"]];
[myIvar setHidden:YES];

或者你可以在运行时降低并使用这些方法:

#import <objc/runtime.h>

id myIvarValue = nil;
[text appendString:@"Pic1"];
Ivar theIvar = object_getInstanceVariable(self, [text UTF8String], &myIvarValue];
[myIvarValue setHidden:YES];

答案 2 :(得分:1)

您可以将所有子视图存储在字典中:

NSMutableDictionary *viewsByName = [[NSMutableDictionary alloc] init];

[viewsByName addObject:aPic1 withKey:@"aPic1"];
[viewsByName addObject:aPic2 withKey:@"aPic2"];
[viewsByName addObject:bPic1 withKey:@"bPic1"];
[viewsByName addObject:bPic2 withKey:@"bPic2"];

然后:

- (void) performAction:(NSString *) text
{
    ((UIView *)[viewsByName objectWithKey:[NSString stringWithFormat:@"%sPic1",text]]).hidden = YES;
    ((UIView *)[viewsByName objectWithKey:[NSString stringWithFormat:@"%sPic2",text]]).hidden = NO;
}