Objective C handles empty strings and page passes and custom copies

  • 2020-05-14 04:54:49
  • OfStack

An empty string
In the application of ios, if you request data from the network and return data in json or xml format, you will often encounter empty strings. The 1-like interface is written in java and other languages. If you use android, since the source language is java, you only need to judge whether it is equal to null. < null > .
If you just use


string!=nil;

Unable to determine the empty string of Angle brackets

Complete judgment method


-(BOOL)isNull:(id)object
{
    // Determines if the string is empty
    if ([object isEqual:[NSNull null]]) {
        return NO;
    }
    else if ([object isKindOfClass:[NSNull class]])
    {
        return NO;
    }
    else if (object==nil){
        return NO;
    }
    return YES;
}

Sending a message to an empty string causes all sorts of crashes, leaving you speechless, and similarly converts strings

-(NSString*)convertNull:(id)object{
 
    // Convert an empty string
 
    if ([object isEqual:[NSNull null]]) {
        return @" ";
    }
    else if ([object isKindOfClass:[NSNull class]])
    {
        return @" ";
    }
    else if (object==nil){
        return @" There is no ";
    }
    return object;
    
}

Page pass values and custom copies
Do the 1 some problems related to network, sometimes value is more, the custom 1 class, want to put the entire value of the part of the class to the other one interface, this copy is involved, a custom class 1 set to achieve NSCopying agreement, write the copy method - (id) copyWithZone: (zone NSZone *), so this class would like NSString class 1 sample, can use = copy assignment.
1 custom TypesItem class, inherited from NSObject, contains 3 variables (can be customized to add more)

TypesItem.h


#import <Foundation/Foundation.h>
 
@interface TypesItem : NSObject<NSCopying>
{
    NSString *type_id;
    NSString *type_memo;
    NSString *type_name;
}
@property (nonatomic,copy) NSString *type_id;
@property (nonatomic,copy) NSString *type_memo;
@property (nonatomic,copy) NSString *type_name;
 
 
@end

TypesItem.m, in addition to the three variables synthesize


@synthesize type_id,type_memo,type_name;

Also implement the NSCopying protocol method

- (id)copyWithZone:(NSZone *)zone - (id)copyWithZone:(NSZone *)zone
{
    TypesItem *newItem = [[TypesItem allocWithZone:zone] init];
    
    newItem.type_name = self.type_name;
    newItem.type_id = self.type_id;
    newItem.type_memo = self.type_memo;
    return newItem;
}

Pass values between pages, say A- > The value of TypeItem in B,A is passed to B

In B, the.h file is the code


@property(nonatomic,copy) TypesItem *selectedItem;

In the B.m file

@synthesize selectedItem;

Add the code before jumping to B in A.m

BViewController *BVC = [[[BViewController alloc] initWithNibName:@"BViewController" bundle:nil] autorelease];
  
    // item for TypeItem Type and is not null
  
    BVC.selectedItem = item;
    
    [self.navigationController pushViewController:BVC animated:YES];

PS: when a value is passed between pages, BVC1 in BVC.selectedItem must be 1 in push's past BVC, otherwise the selectedItem value in push to B must be null.


Related articles: