main()外部的变量值不正确

nuk*_*ukl 0 cocoa objective-c

我有这个代码

#import <Foundation/Foundation.h>
int testint;
NSString *teststring;

int Test()
{
    NSLog(@"%d",testint);
    NSLog(@"%@",teststring);
}


int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    testint = 5;
    NSString *teststring = [[NSString alloc] initWithString:@"test string"];
    Test();
    [pool drain];
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在输出我有:

5(null)

为什么Test函数没有看到正确的teststring值?我应该怎么做,在输出中有正确的"测试字符串"?

Dan*_*son 6

您正在使用本地变量遮蔽全局变量.如果意图是使用全局testString,则不应使用"NSString*"重新声明它.