Firebase Swift查询符号 [英] Firebase Swift querying for symbols

查看:58
本文介绍了Firebase Swift查询符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释如何查询Firebase中的特殊字符吗?

Would someone be able to explain how to query Firebase for special characters?

我有一些类似的数据-

posts
  post_1
    description: "This is a post! #thisdoesntwork"
  post_2
    description: "Heres another post! #neitherdoesthis"

如果我迅速运行查询-

let db = Database.database().reference()

db.child("posts").queryOrdered(byChild: "description").queryStarting(atValue: "[a-zA-Z0-9]*").queryEnding(atValue: "#thisdoesntwork").observeSingleEvent(of: .value) { (snapshot) in
   // No results!
 }

一无所获.但是,当我这样省略主题标签时,它会起作用-

Nothing gets returned. However it works when I omit the hashtag like so -

db.child("posts").queryOrdered(byChild: "description").queryStarting(atValue: "[a-zA-Z0-9]*").queryEnding(atValue: "thisdoesntwork").observeSingleEvent(of: .value) { (snapshot) in
   // One post gets returned here
 }

这是因为哈希是我需要以某种方式转义的特殊字符吗?还是我以错误的方式查询它?

Is this because the hash is a special character I need to escape somehow? Or am I querying for it in the wrong way?

先谢谢了.

推荐答案

您认为正在发生的事情并非如此.让我解释一下并提供一个示例:

What you think is happening isn't. Let me explain and provide an example:

您似乎正在尝试执行字符串搜索.甚至是子字符串搜索.

It appears your are trying to perform a string search. Perhaps even a substring search.

Firebase不提供子字符串搜索功能,甚至字符串搜索也不完全是字符串搜索,就像在swift这样的语言中一样.

Firebase does not offer a substring search feature and even a String search isn't exactly a string search as it would be in a language like swift.

所以对于初学者来说这是无效的

So for starters this isn't valid

queryStarting(atValue: "[a-zA-Z0-9]*")

会从字面上搜索以等于[a-zA-Z0-9] *的字符串或字符开头的节点.因此,如果您的节点碰巧如下所示:

that would literally search for a node that starts with the string or characters equal to [a-zA-Z0-9]*. So if your node happened to look like the following:

posts
  post_x
    description: "[a-zA-Z0-9]* This node would be returned"

这将是一个匹配项.

.startWith: a query that starts with the given string
.endWith: a query ending with a string that starts with the given string
        (not the ending part of a string or a substring)

让我根据您的结构提供一个示例结构

Let me provide an example structure based on your structure

posts
  post_1
    description: "This is a post! #thisdoesntwork"
  post_2
    description: "Heres another post! #neitherdoesthis"
  post_3
    description: "a"
  post_4
    description: "n"

和示例查询

    let postsRef = ref.child("posts")
    let queryRef = postsRef.queryOrdered(byChild: "description")
                           .queryStarting(atValue: "This")
                           .queryEnding(atValue: "z")
    queryRef.observeSingleEvent(of: .value) { snapshot in
        print(snapshot)
    }

此查询将返回帖子1、3和4.为什么?

This query will return posts 1, 3, and 4. why?

post_1以字母大写T开头,即ascii 84.

post_1 starts with the letter upper case T, which is ascii 84.

查询将返回所有具有以84(ascii T)开头和以122(ascii z)结尾的ascii值的节点.因此,第3个是a,即ascii 97,第4个a,n,是ascii110.因此,所有这些都将返回.

The query will return ALL nodes that have an ascii value starting with 84 (ascii T) and ending with 122 (ascii z). So post 3 is an a, which is ascii 97 and post 4, an n, is ascii 110. So all of those are returned.

*对于随后的用户,该查询实际上以单词'This'开头,并以单词'z'结尾,但在此示例中进行了简化.

*for those following along, the query actually starts with the word 'This' and ends with the word 'z' but simplifying for this example.

虽然一方面看似有些局限,但实际上却非常强大.

While on one hand that may seem a bit limiting, it's actually quite powerful.

一种用法是当您要查询以特定字符串开头的一系列值时.因此,假设您拥有一家农产品分销公司,并且拥有诸如Apple,Banana和Peanut and Walnut之类的商品.您可以像这样组织数据库

One use is when you want to query for a range of values starting with a specific string. So let say you own a produce distribution company and have items like Apple, Banana and Peanut and Walnut. You can organize your database like this

items
  item_0
   description: fruit_apple
  item_1
   description: fruit_banana
  item_2
   description: nut_peanut
  item_3
   description: nut_walnut

如果您想要所有水果的清单,可以像这样查询

If you wanted a list of all of your fruits, you can query like so

    let queryRef = postsRef.queryOrdered(byChild: "description")
                           .queryStarting(atValue: "fruit_")
                           .queryEnding(atValue: "fruit_")

这称为复合值.

在您的情况下,最重要的答案是您不能直接在字符串内搜索特殊字符 ,但是,您可以搜索以ASCII码范围内的字符开头的字符范围.

In your case the bottom line answer is you cannot search directly for special characters within the string however, you can search for a range of characters that start with character within an ascii code range.

查询从!"开始并以"/"结尾将返回所有以字符开头的字符串:

query starting at "!" and ending at "/" would return all strings starting with characters:

33  !
34  \"
35  #
36  $
37  %
38  &
39  '
40  (
41  )
42  *
43  +
44  ,
45  -
46  .
47  /

这个超长答案并不是真正的解决方案,但可能会帮助您重组Firebase,以便您获取要查询的数据.

This super long answer isn't really a solution but will maybe help with restructuring your Firebase so you can get to the data you want to query on.

这篇关于Firebase Swift查询符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆