Empty string
In iOS applications, if you request data from the network and return data in json or xml format, you often encounter empty strings. Generally, the interface is written in Java and other languages. If it is Android, because the source language is Java, you only need to determine whether it is equal to null, but various forms of items will appear in iOS, such as null, (null), <null>.
If you use it simply
Copy the codeThe code is as follows:
string!=nil;
Unable to determine the empty string of angle brackets
Complete judgment method
Copy the codeThe code is as follows:
-(BOOL)isNull:(id)object
{
// Determine whether it is an empty string
if ([object isEqual:[NSNull null]]) {
return NO;
}
else if ([object isKindOfClass:[NSNull class]])
{
return NO;
}
else if (object==nil){
return NO;
}
return YES;
}
Sending messages to empty strings will cause various crashes, which makes people speechless. Similarly, converting strings
Copy the codeThe code is as follows:
-(NSString*)convertNull:(id)object{
// Convert empty string
if ([object isEqual:[NSNull null]]) {
return @" ";
}
else if ([object isKindOfClass:[NSNull class]])
{
return @" ";
}
else if (object==nil){
return @"None";
}
return object;
}
Page value transfer and custom copy
When doing some network-related problems, sometimes there are many values. Customize each class and want to pass the values of the entire part of this class to another interface. This involves copying. In a custom class, you must implement the NSCopying protocol and write the copy method - (id)copyWithZone:(NSZone *)zone, so that this class can be copied with = assignable value like the NSString class.
Customize a TypesItem class, inherited from NSObject, contains three variables (multiple can be added in custom)
Copy the codeThe code is as follows:
#import <Foundation/>
@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
In the file, in addition to synthesize the three variables
Copy the codeThe code is as follows:
@synthesize type_id,type_memo,type_name;
Implement the NSCopying protocol method
Copy the codeThe code is as follows:
- (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 the value between pages, assuming that the value of TypeItem in A->B, A must be passed to B
Write code in B.h file
Copy the codeThe code is as follows:
@property(nonatomic,copy) TypesItem *selectedItem;
In the file
Copy the codeThe code is as follows:
@synthesize selectedItem;
Add code before jumping to B
Copy the codeThe code is as follows:
BViewController *BVC = [[[BViewController alloc] initWithNibName:@"BViewController" bundle:nil] autorelease];
// item is TypeItem type and is not empty
= item;
[ pushViewController:BVC animated:YES];
PS: When passing values between pages, the BVC here must be consistent with the BVC push past, otherwise the selectedItem value in the interface push to B must be null.