简体   繁体   English

NSMutableString访问问题

[英]NSMutableString access problem

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.. 对于日期格式,我正在使用NSDateFormatter ,它工作正常。

My function ( didFinishUpdatesSuccessfully ) performs some action and if successful displays an UIAlertView which includes the formatted date. 我的函数( didFinishUpdatesSuccessfully )执行一些操作,如果成功,则显示包含格式化日期的UIAlertView 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, eg another function etc.. 我现在想将dateString的值写入全局NSStringNSMutableString并在代码中的其他位置(例如,另一个函数等)访问它。

I thought about creating a NSMutableString like this: NSMutableString* lastUpdated = [NSMutableString stringWithFormat:@"%@",dateString]; 我考虑过创建这样的NSMutableStringNSMutableString* lastUpdated = [NSMutableString stringWithFormat:@"%@",dateString]; and to access lastUpdated somewhere else, but ouside of this function lastUpdated is empty... Can you help? 并在其他地方访问lastUpdated ,但是该函数lastUpdated为空...您能帮上忙吗? Cheers 干杯

NSMutableString* lastUpdated = [NSMutableString stringWithFormat:@"%@",dateString];

If you do that, you're declaring a local variable named lastUpdated . 如果这样做,您将声明一个名为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: 为了使这项工作有效,您需要在任何函数或方法之外的某个位置(可能在.m文件的顶部附近)声明一个全局lastUpdated

NSMutableString *lastUpdated;

You can then access that variable from anywhere in the .m file. 然后,您可以从.m文件中的任何位置访问该变量。 If you want to access it in other .m files, you'll want to add an extern declaration in the corresponding header (.h) file: 如果要在其他.m文件中访问它,则需要在相应的标头(.h)文件中添加extern声明:

extern NSMutableString *lastUpdated;

With that declaration, you can use lastUpdated in any file that includes that header file. 使用该声明,您可以在包含该头文件的任何文件中使用lastUpdated

Two things to know: 有两件事要知道:

  1. 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. 这是C的基本知识,因此,如果您不熟悉C,则应查看C的作用域规则。了解全局变量,静态变量,局部变量,实例变量之间的区别(好吧,普通的旧C没有这些变量) )和一个参数。

  2. 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.) (而且,IMO也无法解决这个问题。仅用于访问全局数据的单例只不过是花哨的全局变量而已。)

You should retain the string like. 您应该像这样保留字符串。

NSMutableString* lastUpdated;
lastUpdated = [[NSMutableString stringWithFormat:@"%@",dateString] retain];

Now try to access in outside. 现在尝试在外部访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM