NSMutableString 访问问题
所以我想在我的函数之外访问并显示格式化日期。对于日期格式,我使用 NSDateFormatter
,效果很好。
我的函数 (didFinishUpdatesSuccessively
) 执行一些操作,如果成功,会显示一个 UIAlertView
,其中包括格式化的日期。一切正常..
- (void) didFinishUpdatesSuccessfully {
//--- Create formatted date
NSDate *currDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"dd/MM/YYYY - hh:mm:ss a"];
NSString *dateString = [dateFormatter stringFromDate:currDate]; // dateString contains the current date as a string
[dateFormatter release];
//--- UIAlertView
NSString *title = @"The update has been performed!";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: title
message: dateString
delegate: nil
cancelButtonTitle: [FileUtils appResourceForKey:@"UPDATE_GENERAL_BUTTON_TITLE_OK"]
otherButtonTitles: nil];
[alert show];
[alert release];
//--- create new string
// NSMutableString* lastUpdated = [NSMutableString stringWithFormat:@"%@",dateString];
}
我现在想将 dateString
的值写入全局 NSString
或 NSMutableString
并在代码中的其他位置访问它,例如另一个函数等。
我考虑过创建一个像这样的 NSMutableString : NSMutableString* lastUpdated = [NSMutableString stringWithFormat:@"%@",dateString];
并在其他地方访问 lastUpdated
,但在此函数之外 lastUpdated
是空的...你能帮忙吗?干杯
So I'd like to access and display a formatted date outside my function. For the date format I am using NSDateFormatter
which works fine..
My function (didFinishUpdatesSuccessfully
) performs some action and if successful displays an UIAlertView
which includes the formatted date. All that works fine..
- (void) didFinishUpdatesSuccessfully {
//--- Create formatted date
NSDate *currDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"dd/MM/YYYY - hh:mm:ss a"];
NSString *dateString = [dateFormatter stringFromDate:currDate]; // dateString contains the current date as a string
[dateFormatter release];
//--- UIAlertView
NSString *title = @"The update has been performed!";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: title
message: dateString
delegate: nil
cancelButtonTitle: [FileUtils appResourceForKey:@"UPDATE_GENERAL_BUTTON_TITLE_OK"]
otherButtonTitles: nil];
[alert show];
[alert release];
//--- create new string
// NSMutableString* lastUpdated = [NSMutableString stringWithFormat:@"%@",dateString];
}
I now want to write the value of dateString
into a global NSString
or NSMutableString
and access it somewhere else in the code, e.g. another function etc..
I thought about creating a NSMutableString
like this:NSMutableString* lastUpdated = [NSMutableString stringWithFormat:@"%@",dateString];
and to access lastUpdated
somewhere else, but ouside of this function lastUpdated
is empty... Can you help? Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果这样做,您将声明一个名为
lastUpdated
的局部变量。即使存在另一个同名的全局变量,只要该全局变量在作用域(函数的生命周期)内,该局部变量就会隐藏该全局变量。要实现此功能,您需要在任何函数或方法之外的某个位置声明一个全局
lastUpdated
,可能位于 .m 文件顶部附近:然后您可以从 .m 文件中的任何位置访问该变量。如果您想在其他 .m 文件中访问它,则需要在相应的头 (.h) 文件中添加 extern 声明:
通过该声明,您可以在任何文件中使用
lastUpdated
包括该头文件。需要了解两件事:
这是基本的 C 内容,因此如果它看起来不熟悉,您应该查看 C 的作用域规则。了解全局变量、静态变量、局部变量、实例变量之间的区别(好吧,普通的旧 C 没有这些)和参数。
全局变量是可怕的。不要相信任何告诉你其他情况的人。我提供上面的建议来帮助解决您眼前的问题,但更好的解决方案是弄清楚如何重构您的代码,以便您可以避免对全局变量的需要。 (在我看来,单例也不是答案。仅用于访问全局数据的单例只不过是花哨的全局变量。)
If you do that, you're declaring a local variable named
lastUpdated
. Even if there's another global variable with the same name, this local one will hide the global variable for as long as it's in scope (the life of your function).To make this work, you need to declare a global
lastUpdated
somewhere outside of any function or method, probably near the top of a .m file:You can then access that variable from anywhere in the .m file. If you want to access it in other .m files, you'll want to add an extern declaration in the corresponding header (.h) file:
With that declaration, you can use
lastUpdated
in any file that includes that header file.Two things to know:
This is basic C stuff, so if it seems unfamiliar, you should review scope rules for C. Know the difference between a global variable, a static variable, a local variable, an instance variable (okay, plain old C doesn't have those), and a parameter.
Global variables are horrible. Don't trust anybody who tells you otherwise. I offer the advice above to help solve your immediate problem, but a better solution would be to figure out how to refactor your code so that you can avoid the need for a global variable. (And IMO a singleton isn't the answer, either. Singletons used just to access global data aren't much more than fancy-pants global variables.)
你应该保留字符串。
现在尝试从外部访问。
You should retain the string like.
Now try to access in outside.