Bra*_*ble 1 global-variables objective-c actionscript-3
在Actionscript中,您可以拥有如下全局变量:
var number : Number = 15;
Run Code Online (Sandbox Code Playgroud)
然后在方法/函数中使用它.你如何在Objective-c中做到这一点,是否可能?
请记住,Objective-C的是C严格的超集,所以你可以在常规C.首先声明全局变量你会以同样的方式在任何的功能以外的一些文件中声明它们,然后使用C extern关键字在其他文件中拉这些变量在.
如果您想要使用的不仅仅是C变量,而是使用Objective-C对象,您可以将它们存储在应用程序的委托中.只需像往常一样将它们设置在那里,然后在需要访问变量时:
// Assuming your app delegate is of class YourAppDelegate and
// has an NSString* variable called globalString:
YourAppDelegate *appDelegate =
(YourAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *someGlobalString = [appDelegate globalString];Run Code Online (Sandbox Code Playgroud)
您可能还发现在app委托中声明变量static是有益的.