IOS-based method for determining whether a string is null

Source: Internet
Author: User

IOS-based method for determining whether a string is null

-(BOOL) isBlankString :( NSString *) string {

If (string = nil | string = NULL ){

Return YES;

}

If ([string isKindOfClass: [NSNull class]) {

Return YES;

}

If ([[string stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet] length] = 0 ){

Return YES;

}

Return NO;

}

NSNull is a special class, which is the same as nil and also represents a null value. But there is a difference between the two. NSNull does not accept NSArray's methods. It has only one class method:

+(NSNull *)null;

Therefore, sending NSArray-specific method or attribute access will cause program crash. A major part of the null value judgment is to ensure the stability of the program when the background interface data returns some abnormal data.

For example, in some cases, the background may return a null value for a field for various reasons. In this case, we get an object such as [NSNull null]. For example, the backend may have a value for a certain data, but 0 is returned. In this case, an empty group is returned. what we get is @ [], an empty group.

Therefore, to determine whether the array is null, You need to determine all situations. There are many writing methods, such as writing:

if (array != nil && ![array isKindOfClass:[NSNull class]] && array.count != 0)


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.