Parse.com:如何正确检查字段是否为空? [英] Parse.com: How to properly check if a field is empty?

查看:66
本文介绍了Parse.com:如何正确检查字段是否为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是在iOS/Swift应用的上下文中问这个问题的:

I ask this in the context of a iOS / Swift app:

当使用查询从Parse中检索对象时,我过去通过将空字段检查为nil来检查空字段.如果!= nil ....则执行此操作,等等...

When retrieving with a query an object from Parse, I used to check for empty fields by checking them against nil. if != nil.... then do this etc etc....

显然,这不是执行此操作的正确方法,因为即使Parse上的字段为空,也不被swift视为nil.

Apparently, this is not the proper way to do this, because even if a field is empty on Parse, it is NOT considered nil by swift.

那么要检查什么以确定一个字段是否为空,以及对于各种Parse支持的类型:字符串,数字,数组....?

So what to to check for to determine if a field is empty or not, and for the various Parse supported types: strings, Number, Array.....?

对objective-c的解决方案无法迅速解决,除非值是布尔值,否则您必须将其与nil(这会引起问题)或其他方式进行比较,以查看其是否存在.

there are answers for objective-c that don't work in swift where unless value is a boolean you MUST compare it against nil (which cause issue here) or something else to see if it exists or not.

推荐答案

好的,这是经过一些调查后我发现的:

Ok so here is what I found after some investigation:

在parse Explorer中看起来为空的String字段,实际上包含".只有当字段中专门包含未定义"时,它才为nil,您可以将其与===进行比较,而不能与==进行比较.不知道为什么,如果有人知道......

A String field when it looks empty on parse explorer, in fact contains "". It is only when the field specifically has "undefined" in it that it is nil, which you can compare against with === and not ==. Not sure why, if someone knows....

字符串"以外的其他字段不能包含".它们包含一个值或未定义.

Other fields than String, cannot contain "". They either contain a value or are undefined.

因此,如果您需要知道某个字段是否为nil或不包含任何值,则将其与=="和=== nil进行比较,如果不是字符串,则使用=== nil.

So if you need to know if a field is nil or contains no value, if it's a string compare it against == "" and === nil, and use === nil if it's not a string.

这篇关于Parse.com:如何正确检查字段是否为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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