gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 03:59:56 24 4
gpt4 key购买 nike

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

例如,假设我有一堆图像:aPic1、aPic2、bPic1、bPic2、cPic1、cPic2,我想创建一个 Action ,以便每次单击按钮时, View Controller 都会隐藏 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。任何人都知道如何通过这种功能实现我的目标?谢谢。

最佳答案

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

对于我建议的解决方案,您需要能够通过方法访问(设置/获取)您的变量(使用属性或编写您自己的 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 的这个强大功能,我会这样做的。虽然我不确定是否要深入探讨这个主题,因为它可能需要太多的互联网论文,所以这就是为什么在示例代码之后我还会添加一些链接以供进一步阅读。

@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 ,我认为这可能是一个很好的起点。顺便说一句,他们提供了大量学习 Material (播客、截屏视频等),因此进一步浏览他们的网站可能会有用。当然,了解 Objective-C 和 Cocoa 的主要地方是官方文档,here's where it is about properties .

关于objective-c - 在 objective-c 中使用参数作为变量名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2094587/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com