Swift learns-anyobject for null judgment

Source: Internet
Author: User

Recently, I have been wondering how to judge the Anyobject type is empty, like the previous OC has nil to judge it, but it can not use this method to judge;

And I need to use the Anyobject type as the return value in many data requests, studied for a long time did not find an effective way of judging, today carefully studied the next its data structure, finally found the method;

First of all, the analysis of its data structure, the following sub-data and no data when the distinction:

1. When there is data:

▿optional<anyobject>

▿some:2 elements

▿[0]: Elements

▿[0]: 2 elements

-. 0:systime

▿[1]: 2 elements

-. 0:contxt

▿[1]: Elements

▿[0]: 2 elements

-. 0:systime

▿[1]: 2 elements

-. 0:contxt {...}

2. When there is no data:

Optional<anyobject>

-Some: <null>

From the above two cases can be seen in the data when it is used in a count of the way to store, a bit like an array, then it should be similar to the array to determine the number of elements to call the Count function to determine whether there are elements;

And there is no value of the situation will find that it gives the <null>, which reminds me of the Nsnull class, but I use this way to judge the time is not good, the compilation can not pass;


Therefore, the final solution is to use it as a container to handle, storage elements in the container, then you can get the number of elements in the container, if the number is 0, then there is no element in the container;


The final judgment is that Anyobject is an empty operation that relies on the count function to anyobject.count<=0 that means Anyobject is empty.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.