objective-c - 找出 Objective C 中传递给函数的参数是右值 ( @"something") 还是左值 (foo)

标签 objective-c cocos2d-iphone

在 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/

相关文章:

ios - 在 IOS 中比较两个图像的 RGB

objective-c - CoreData 序列化的 NSDate 转换回真实日期

iphone - 每个 View 上的相同 WebView

iphone - 如何为 iPhone 应用程序创建一个带有兴趣点的旋转地球仪?

iphone - SimpleAudioEngine 需要时间来播放声音

iphone - 缩放整个 View Controller

ios - 当弹出窗口消失时,保持 UITextView 中的文本突出显示

java - 传递包含两个整数的对象或不同的方法?

iphone - TiledMaps 和真实对象

iphone - Cocos2d removeChild 不工作