【问题标题】:Append Data To NSDictionary将数据附加到 NSDictionary
【发布时间】:2012-06-22 09:14:28
【问题描述】:

我的应用属于 SplitView 类型,我使用 UITableView 显示网站名称。在didSelectRowAtIndexPath 方法中,我使用以下代码行::

dateString = [formatter stringFromDate:[NSDate date]];
dict = [[NSDictionary alloc] init];
dict = [NSDictionary dictionaryWithObjectsAndKeys: urlString, dateString, nil];

[history addObject:dict];

NSLog(@"dateString: %@", dateString); //will let you know if it's nil or not
NSLog(@"urlString : %@", [dict objectForKey:dateString]);

NSString *docDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSLog(@"docDir is yoyo :: %@", docDir);
NSString *spaceFilePath = [docDir stringByAppendingPathComponent:@"space.txt"];
[history writeToFile:spaceFilePath atomically: TRUE];  

我正在尝试将访问过的网站的网址与时间戳一起添加到NSDictionary“dict”中。我已经在viewDidLoad 方法中初始化了格式化程序。 History 是一个存储字典列表的数组。

我在viewDidLoad 方法中使用了以下代码行:

formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

我想将访问的新站点的名称附加到已经存在的文件space.txt中。

但是,仅显示一个名称(最后访问的站点的 url)。如何将新访问的站点名称附加到我已经存在的文件 space.txt 中?我无法解决这个问题。

感谢和问候。

【问题讨论】:

  • 添加 dict 后,打印历史记录并验证值?正如@Rajesh 建议的那样检查您的历史记录是否为零?

标签: iphone objective-c ios xcode ipad


【解决方案1】:

您也可以使用您的代码。请交叉检查您是否在 viewDidLoad 中分配了“历史”对象。如果没有,请做出来。

【讨论】:

  • 好吧..大声笑..在 viewDidLoad 中分配历史对象也有效!!多谢 !!这 1 行完成了工作 :: "history = [[NSMutableArray alloc] init];"
  • 但是你能告诉我分配这个内存的一些原因吗?我对下面 AliSoftware 的陈述感到困惑。
  • 如果你没有为一个对象分配内存并且你正在为它添加一些值,这意味着处理器将保存数据临时内存,并且每当范围完成时,它将删除临时数据。这样您就无法在不分配的情况下使用超出范围的这些详细信息。在您的情况下,每次您附加的数据都保存到临时内存中,并且在范围之后它正在删除添加的数据。这样每次它只保存预设分配的数据。
【解决方案2】:

使用 NSMutableDictionary(而不是 NSDictionary)拥有一个可变的字典(= 可以修改)。


附录: 请注意,您的行 dict = [[NSDictionary alloc] init]; 完全没用,因为您在下一行重新影响了变量“dict”。这就像你做了类似的事情:int a = 5;,然后是a=8,当然之前变量中的值被新的值替换,并且之前的值(你的 cas 中的 [[NSDictionary alloc] init])丢失了.

【讨论】:

  • kz .. 我也尝试使用 NSMutableDictionary 并删除了您指出的行。代码仍然不起作用,即仍然只有最后访问的站点被写入文件 space.txt :-/
【解决方案3】:

您创建的对象(“dict”)每次都不同。仅仅意味着您在字典中添加选定的详细信息并编写它。因此您无法看到之前保存的详细信息。检查以下代码以将新访问的站点名称附加到我已经存在的文件 space.txt 中。

dateString = [formatter stringFromDate:[NSDate date]];

NSString *docDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *spaceFilePath = [docDir stringByAppendingPathComponent:@"space.txt"];

if ([[NSFileManager defaultManager] fileExistsAtPath:spaceFilePath]) {
    NSMutableDictionary *appendDict = [[NSMutableDictionary alloc] initWithContentsOfFile:spaceFilePath];
    [appendDict setObject:urlString forKey:dateString];
    [appendDict writeToFile:spaceFilePath atomically: TRUE];  
    [appendDict release];
} else {
    NSMutableDictionary *appendDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:urlString, dateString, nil];
    [appendDict writeToFile:spaceFilePath atomically: TRUE];  
    [appendDict release];
}

我认为这很有用。

【讨论】:

    猜你喜欢
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-16
    • 2017-09-22
    • 2023-04-03
    • 2021-06-06
    相关资源
    最近更新 更多