在 Objective C 中,有没有办法确定传递给函数的参数是右值还是左值? 例如,
NSString* foo = @"FOO";
[self foobar:@"FOO"]; ...1
[self foobar:foo]; ...2
其中 foobar 是一个函数:
-(void)foobar:(NSString*)str{
//do something
}
我需要能够在函数 foobar 中找出它是由 (1) 还是 (2) 调用的。我想避免更改函数签名。
谢谢!
最佳答案
常量和非常量 NSString
对象确实有不同的类型,如下所示,但是我不确定这是区分需要翻译的字符串和不需要翻译的字符串的特别好的方法t。
您可能应该使用 Apple 自己的 Internationalization Support ,但就我个人而言,我将使用 Delicious Library 作者(之一)记录的方法,here .
#import <Foundation/Foundation.h>
void foo(NSString *s) {
NSLog(@"Type of '%@' is %@", s, NSStringFromClass([s class]));
}
int main(int argc, const char **argv) {
@autoreleasepool {
NSString *s1 = [NSString stringWithFormat:@"A %@ string", @"formatted"];
NSString *s2 = @"A constant string";
foo(s1);
foo(s2);
}
return 0;
}
2013-09-17 16:34:05.667 test[9866:707] Type of 'A formatted string' is __NSCFString
2013-09-17 16:34:05.668 test[9866:707] Type of 'A constant string' is __NSCFConstantString
关于objective-c - 找出 Objective C 中传递给函数的参数是右值 ( @"something") 还是左值 (foo),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18853049/