草庐IT

objective-c - 无法更改/替换 NSDictionary 键值 IOS

coder 2024-01-21 原文

我正在从 Dropbox 下载文件修订状态,基本上我将 Dropbox 中下载的修订号与本地 plist 中的修订号进行比较。

比较它们后,我想用 Dropbox 的修订号更改本地修订号。但它不起作用,我快要失去理智了。

我放置了一些标志和 NSlogs,它似乎替换了值,但在我调用相同的函数或再次启动应用程序后,我看到该值没有被替换。它一遍又一遍地给出相同的输出

    NSString* revisionLocal = [dicInner objectForKey:@"revision"];
    NSString* statusLocal = [dicInner objectForKey:@"status"];

    NSLog(@"revision  value before %@",revisionLocal);
    NSLog(@"status value before %@",statusLocal);

   //If revision has changed on dropbox, flag it as outdated on the local revision
                        if(![revisionLocal isEqualToString: dropBoxRevision] ){
                            [dicInner setValue:@"outdated" forKey:@"status"];
                            //But the revision is the latest
                            [dicInner setValue:dropBoxRevision forKey:@"revision"];
                            //[dicInner setValue:@"outdated" forKey:@"revision"];

                            NSLog(@"revision value %@",[dicInner objectForKey:@"revision"]);
                            NSLog(@"status value %@",[dicInner objectForKey:@"status"]);

所以这给了我输出:

revision  value before 4309efbbb7
status value before updated
revision value 4409efbbb7
status value outdated

完整代码为:

- (void)restClient:(DBRestClient *)client loadedMetadata:(DBMetadata *)metadata {

    //get the local revision
    NSDictionary * localRevisionDic = [FileUtils readPlistIntoDictionary:@"revision.plist"];

    NSString *fileRevString = [NSString alloc];
    //get the revision from Dropbox
    //NSString * dropboxRevision;
    if (metadata.isDirectory) { 
        NSLog(@"Folder '%@' contains:", metadata.path);
        for (DBMetadata *file in metadata.contents) {

            NSLog(@"\t%@", file.filename);
            //NSLog(@"\t%@", file.lastModifiedDate);
            NSLog(@"\t%@", file.rev ); 

            //Assign dropbox revision for this file
            //dropboxRevision = file.rev;
            //if no local revision.plist, entry will be added. Hence init here
            if (localRevisionDic==nil){
                localRevisionDic = [[NSMutableDictionary alloc]init];

            }
            //Otherwise go through each from dropbox and campare with local

            //From Dropbox
            NSString * dropBoxFileName = file.filename;
            NSString * dropBoxRevision = file.rev;

            fileRevString = file.rev;

            //if no local revision.plist entry is added for all other files
            //with status need_downloaded, and no revision
            if ([localRevisionDic count]==0){

                //Creating revision dictionary entry for agenda.plist
                NSDictionary * localRevisionDicDic = [[NSMutableDictionary alloc]init];
                //when agenda.plist revision entry is added update the revision while leaving status as "new" before downloading
                //will be updated accordingly if download fails
                [localRevisionDicDic setValue:@"new" forKey:@"status"];
                //Status is new but the revision is the latest
                [localRevisionDicDic setValue:dropBoxRevision forKey:@"revision"];

                [localRevisionDic setValue:localRevisionDicDic forKey:dropBoxFileName];

            }else{//If there is local revision.plist compare and update accordingly
                NSDictionary * dicInner = [localRevisionDic objectForKey:dropBoxFileName];
                //File name Found locally
                if (dicInner!=nil){
                    NSString* revisionLocal = [dicInner objectForKey:@"revision"];
                    NSString* statusLocal = [dicInner objectForKey:@"status"];

                    NSLog(@"revision  value before %@",revisionLocal);
                    NSLog(@"status value before %@",statusLocal);

                    //If revision has changed on dropbox, flag it as outdated on the local revision
                    if(![revisionLocal isEqualToString: dropBoxRevision] ){
                        [dicInner setValue:@"outdated" forKey:@"status"];
                        //But the revision is the latest
                        [dicInner setValue:dropBoxRevision forKey:@"revision"];
                        //[dicInner setValue:@"outdated" forKey:@"revision"];

                        NSLog(@"revision value %@",[dicInner objectForKey:@"revision"]);
                        NSLog(@"status value %@",[dicInner objectForKey:@"status"]);
                    }
                    }else{//File name not found locally newly added on dropbox

                        NSDictionary * localRevisionDicDic = [[NSMutableDictionary alloc]init];
                        //when agenda.plist revision entry is added update the revision while leaving status as "new" before downloading
                        //will be updated accordingly if download fails
                        [localRevisionDicDic setValue:@"new" forKey:@"status"];
                        //But the revision is the latest
                        [localRevisionDicDic setValue:dropBoxRevision forKey:@"revision"];

                        [localRevisionDic setValue:localRevisionDicDic forKey:dropBoxFileName];
                }
            }
        }
    }
    //At this point agendaRevisionDicTemp contains all the files in dropbox entered/updated.
    [[self agenda] setRevision:localRevisionDic];

    //*****The following block is needed to determine is new agenda is needed or not.
    BOOL newAgendaNeeded = false;

    NSMutableDictionary * agendaRevisionDicLocal = [localRevisionDic objectForKey:@"agenda.plist"];

    //NSString * localRevision = [agendaRevisionDicLocal objectForKey:@"revision"]; //what is this value?

    NSString * localStatus = [agendaRevisionDicLocal objectForKey:@"status"];
    NSLog(@"Local Status= %@",agendaRevisionDicLocal);


    if ([localStatus isEqualToString:@"new"] ||[localStatus isEqualToString:@"outdated"]){
        newAgendaNeeded = true;
        //when agenda.plist is added update the revision while leaving status as "new" before downloading
        //will be updated accordingly if download fails
        NSDictionary * agendaDic = [[[self agenda]revision] objectForKey:@"agenda.plist"];
        [agendaDic setValue:@"updated" forKey:@"status"];
        NSLog(@"agendaDic where update %@",agendaDic);

    }
    //*****The above block is needed to determine is new agenda is needed or not.

    //If new agenda is needed download
    if (newAgendaNeeded){
        //Download agenda.plist
        NSString *documentsDirectory = FileUtils.getDocumentsDirectory;
        [[self restClient] loadFile:@"/agenda.plist" intoPath: [ NSString stringWithFormat:@"%@/%@",documentsDirectory,@"agenda.plist"] ];

    } else{//Else display the scene

        [self populateSceneFromAgenda];

    }

    [restOfView reloadData];
    // Add at start of requestFinished AND requestFailed
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}

知道如何替换该值吗?

最佳答案

您不尊重 NSDictionary 对象的不变性。您将 localRevisionDic 声明为 NSDictionary*,但稍后可以为其分配 NSMutableDictionary 的值。 localRevisionDicDic 声明为 NSDictionary* 但初始化为 NSMutableDictionary 的值。以下两个赋值应该在编译时被警告。在行

[localRevisionDic setValue:localRevisionDicDic forKey:dropBoxFileName];

谁能确定 localRevisionDic 是可变的还是不可变的?
然后,您再次将 dicInner 声明为 NSDictionary*,但稍后尝试设置 Value 两次。首先制作字典的可变副本。

关于objective-c - 无法更改/替换 NSDictionary 键值 IOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12695027/

有关objective-c - 无法更改/替换 NSDictionary 键值 IOS的更多相关文章

  1. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  2. ruby-on-rails - Ruby on Rails 迁移,将表更改为 MyISAM - 2

    如何正确创建Rails迁移,以便将表更改为MySQL中的MyISAM?目前是InnoDB。运行原始执行语句会更改表,但它不会更新db/schema.rb,因此当在测试环境中重新创建表时,它会返回到InnoDB并且我的全文搜索失败。我如何着手更改/添加迁移,以便将现有表修改为MyISAM并更新schema.rb,以便我的数据库和相应的测试数据库得到相应更新? 最佳答案 我没有找到执行此操作的好方法。您可以像有人建议的那样更改您的schema.rb,然后运行:rakedb:schema:load,但是,这将覆盖您的数据。我的做法是(假设

  3. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  4. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  5. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  6. ruby 正则表达式 - 如何替换字符串中匹配项的第 n 个实例 - 2

    在我的应用程序中,我需要能够找到所有数字子字符串,然后扫描每个子字符串,找到第一个匹配范围(例如5到15之间)的子字符串,并将该实例替换为另一个字符串“X”。我的测试字符串s="1foo100bar10gee1"我的初始模式是1个或多个数字的任何字符串,例如,re=Regexp.new(/\d+/)matches=s.scan(re)给出["1","100","10","1"]如果我想用“X”替换第N个匹配项,并且只替换第N个匹配项,我该怎么做?例如,如果我想替换第三个匹配项“10”(匹配项[2]),我不能只说s[matches[2]]="X"因为它做了两次替换“1fooX0barXg

  7. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  8. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  9. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  10. ruby-on-rails - 项目升级后 Pow 不会更改 ruby​​ 版本 - 2

    我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby​​版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby​​版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘

随机推荐