首页 \ 问答 \ NSMutableArray会覆盖这些值(NSMutableArray overwrites the values)

NSMutableArray会覆盖这些值(NSMutableArray overwrites the values)

也许我是盲目的,但我无法弄清楚我做错了什么。

在2次运行之后(数据库中只有2个值),我得到2个不同的值,就像它应该的那样。 然后我把它写入NSMutableArray。

但是只有第二个值两次。 它不应该添加到数组的末尾吗? 我做错了什么?

- (NSMutableArray *)getItemsFromDatabaseWithName:(NSString *)databaseName fromTable:(NSString *)tableName andConstraint:(NSString *)constraint
{
    NSString *absolutePath = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:databaseName];
    NSLog(@"%@", absolutePath);

    //Datenbank öffnen --- "**" bedeuten "&" verwenden
    sqlite3_open([absolutePath UTF8String], &_database);

    //check if there is a constraint and if not take 2nd statement
    if (![constraint isEqualToString:@""])
    {
        _statement = [NSString stringWithFormat:@"select * from %@ where %@",tableName, constraint];
    }
    else
    {
        _statement = [NSString stringWithFormat:@"select * from %@",tableName];
    }

    const char *charStatement = [_statement cStringUsingEncoding:NSUTF8StringEncoding];
    sqlite3_stmt *results;
    //new array to return values
    _mutableItemArray = [NSMutableArray new];
    //new ItemModel
    ItemModel *tmpItem = [ItemModel new];

    if (sqlite3_prepare_v2(_database, charStatement, -1, &results, NULL)== SQLITE_OK)
    {
        while (sqlite3_step(results) == SQLITE_ROW)
        {

            _charItemName =  (char *)sqlite3_column_text(results, 1);
            [tmpItem setItemName:[NSString stringWithUTF8String:_charItemName]];

            _charItemDescription = (char *)sqlite3_column_text(results, 2);
            [tmpItem setItemDescription:[NSString stringWithUTF8String:_charItemDescription]];

            _charItemYear = (char *)sqlite3_column_text(results, 3);
            [tmpItem setItemYear:[_dateFormat dateFromString:[NSString stringWithUTF8String:_charItemYear]]];

            _charItemRecommendedBy = (char *)sqlite3_column_text(results, 4);
            [tmpItem setItemRecommendedBy:[NSString stringWithUTF8String:_charItemRecommendedBy]];

            _charItemImage = (char *)sqlite3_column_text(results, 5);
            [tmpItem setItemImage:[NSString stringWithUTF8String:_charItemImage]];


            [_mutableItemArray addObject:tmpItem];

#warning here I get the 2 items correct
            NSLog(@"ItemName: %@",[tmpItem getItemName]);
            NSLog(@"ItemName: %@",[tmpItem getItemDescription]);
        }
    }
    sqlite3_close(_database);

#warning here I get 2 times the same item ???
    NSLog(@"ItemName: %@",[_mutableItemArray objectAtIndex:0]);
    NSLog(@"ItemName: %@",[_mutableItemArray objectAtIndex:1]);

    return _mutableItemArray;
}

perhaps I'm blind but I cant figure out what I am doing wrong.

after the 2 runs (there are only 2 values in the database) I get 2 different values like it should be. Then I write it into the NSMutableArray.

But there is only the 2nd value twice. Shouldnt it add to the end of the array? What do I do wrong?

- (NSMutableArray *)getItemsFromDatabaseWithName:(NSString *)databaseName fromTable:(NSString *)tableName andConstraint:(NSString *)constraint
{
    NSString *absolutePath = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:databaseName];
    NSLog(@"%@", absolutePath);

    //Datenbank öffnen --- "**" bedeuten "&" verwenden
    sqlite3_open([absolutePath UTF8String], &_database);

    //check if there is a constraint and if not take 2nd statement
    if (![constraint isEqualToString:@""])
    {
        _statement = [NSString stringWithFormat:@"select * from %@ where %@",tableName, constraint];
    }
    else
    {
        _statement = [NSString stringWithFormat:@"select * from %@",tableName];
    }

    const char *charStatement = [_statement cStringUsingEncoding:NSUTF8StringEncoding];
    sqlite3_stmt *results;
    //new array to return values
    _mutableItemArray = [NSMutableArray new];
    //new ItemModel
    ItemModel *tmpItem = [ItemModel new];

    if (sqlite3_prepare_v2(_database, charStatement, -1, &results, NULL)== SQLITE_OK)
    {
        while (sqlite3_step(results) == SQLITE_ROW)
        {

            _charItemName =  (char *)sqlite3_column_text(results, 1);
            [tmpItem setItemName:[NSString stringWithUTF8String:_charItemName]];

            _charItemDescription = (char *)sqlite3_column_text(results, 2);
            [tmpItem setItemDescription:[NSString stringWithUTF8String:_charItemDescription]];

            _charItemYear = (char *)sqlite3_column_text(results, 3);
            [tmpItem setItemYear:[_dateFormat dateFromString:[NSString stringWithUTF8String:_charItemYear]]];

            _charItemRecommendedBy = (char *)sqlite3_column_text(results, 4);
            [tmpItem setItemRecommendedBy:[NSString stringWithUTF8String:_charItemRecommendedBy]];

            _charItemImage = (char *)sqlite3_column_text(results, 5);
            [tmpItem setItemImage:[NSString stringWithUTF8String:_charItemImage]];


            [_mutableItemArray addObject:tmpItem];

#warning here I get the 2 items correct
            NSLog(@"ItemName: %@",[tmpItem getItemName]);
            NSLog(@"ItemName: %@",[tmpItem getItemDescription]);
        }
    }
    sqlite3_close(_database);

#warning here I get 2 times the same item ???
    NSLog(@"ItemName: %@",[_mutableItemArray objectAtIndex:0]);
    NSLog(@"ItemName: %@",[_mutableItemArray objectAtIndex:1]);

    return _mutableItemArray;
}

原文:https://stackoverflow.com/questions/22422318
更新时间:2021-02-28 16:02

最满意答案

看起来你的短语被索引到一个Text字段中,在很多东西中,它按空格分割。 这对于全文搜索非常有用,但不适用于分面。

你可以有一个重复的字段,类型字符串(而不是文本),不分割。 您仍然可以使用原始字段进行搜索,但可以使用新字符串字段进行分割。


Looks like your phrase is being indexed into a Text field which, among many things, splits by whitespace. This is very good for full text search but not for faceting.

You can have a duplicate field for this, of type string (and not Text), which is not splitted. You can still use the original field for searching but the new string field for faceting.

相关问答

更多
  • 你有没有启用group.facet = true,如下所述。 你需要至少3.3 have you enabled group.facet=true as explained here. You need at least 3.3
  • Apache solr在其facet配置页面中包含的字段应该只是选择类型字段或分类术语或类似字段的字段 。 如果您的字段只是常规的“文本”或“整数”或“链接”字段,则它不会出现在该构面配置页面中,因为这些类型的字段不能用作构面。 如果您需要将字段用作构面,请确保将其类型设置为可从列表中选择的类型。 那有意义吗? 这是你的问题吗? The fields Apache solr includes in its facets configuration page should be only fields tha ...
  • Solr需要在Java EE应用程序服务器中运行。 您可以使用Jetty或Tomcat 。 Nginx将通过AJP或simliar充当代理,将所有RESTless请求转发给Solr。 我没有使用我的ajp与nginx,但我已经读过这个 。 基本上,您将同时运行所有Java EE应用程序服务器,Rails服务器,nginx,passenger和ajp代理。 您还可以设置代理通行证,这里有一个教程 。 探索不同的选项,看看哪一个是你的赌注。 Solr needs to run in a Java EE appl ...
  • 第一:你得到一个关于未定义字段的错误似乎很奇怪。 您可能不应让最终用户指定使用的字段而不验证字段,而不是要允许过滤或分面的字段。 第二:使用一个字段生成构面,使用一个场进行过滤。 没有什么可以说你必须使用相同的字段来呈现小平面并在fq使用。 您可以使用包含KeywordTokenizer和的字段进行过滤,然后使用将用于分面的字段中的内容复制到小写字段中进行过滤。 First: it seems weird that you're getting an error ...
  • 看起来你的短语被索引到一个Text字段中,在很多东西中,它按空格分割。 这对于全文搜索非常有用,但不适用于分面。 你可以有一个重复的字段,类型字符串(而不是文本),不分割。 您仍然可以使用原始字段进行搜索,但可以使用新字符串字段进行分割。 Looks like your phrase is being indexed into a Text field which, among many things, splits by whitespace. This is very good for full tex ...
  • 官方答案是:将其作为独立应用程序运行。 不再支持 从Solr 5.0开始,不再支持在像Tomcat这样的servlet容器中将Solr部署为WAR的支持。 有关如何将Solr安装为独立服务器的信息,请参阅安装Solr 。 关于此举的决定的背景可以在Solr Wiki上找到。 Solr旨在成为服务器而非Java Web应用程序,类似于mysql或Apache Web服务器。 首次创建Solr时,将其设计为Web应用程序是一个方便的选择,以避免编写大量棘手的代码来构建网络层。 如今,这一设计决定已成为限制因素。 ...
  • eDisMax查询解析器默认接受小写运算符。 在solrconfig.xml ,指定该解析器,您还可以明确告诉它接受小写运算符: edismax true
  • 他们今天刚刚宣布您现在可以将群集自定义应用于正在运行的群集 。 请参阅Solr群集自定义页面。 仍不建议手动在正在运行的集群上安装内容(而不是使用集群自定义脚本)。 首先,当需要关闭一个节点进行维护时,新节点将在没有安装自定义软件的情况下重新启动。 其次,群集自定义脚本与Azure通信,支持在打开支持案例时对群集所做的更改。 They just announced today that you can now apply cluster customizations to a running cluster ...
  • 嗯好吃 首先,对于所有这些问题,使用Solr分析工具是你的朋友。 其次,请记住,如果查询和术语是字符相同的100%字符,则Solr仅匹配。 对于以下过滤器
  • 大多数拼写检查器和建议器不直接在索引之外工作,而是构建并行结构。 在每次提交时都会有一个设置,如果该设置为false,则可以在请求中传递一个标志来触发重建。 对于拼写检查程序,此标志为spellcheck.build或spellcheck.reload 。 因此,请检查您的配置,并尝试显式调用rebuild / reload。 Most of the spell-checkers and suggesters do not work directly off the index but build a pa ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。