Objective-c handles empty strings and page-pass values and custom copies _ios

Source: Internet
Author: User

Empty string
in iOS applications, if you request data from the network, return JSON or XML format data, often encounter empty string, the general interface is written in Java and other languages, if it is Android, because the source language is Java, just judge whether equals null can, But in iOS there are various forms of each, such as NULL, (NULL),<null>.
If you simply use

Copy Code code as follows:

String!=nil;

Unable to determine empty string for Chunchan bracket

Complete Judgment method

Copy Code code as follows:

-(BOOL) IsNull: (ID) object
{
To determine if 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 a message to an empty string can be a different kind of crash, very silent, and the same. converting strings
Copy Code code 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 pass values and custom copies
When doing some network-related problems, sometimes the value is more, custom a class, want to pass the entire part of the class value to another interface, which involves copying the problem, the custom class must implement the Nscopying protocol, write the method of copying-(ID) Copywithzone: ( Nszone *) zone, so that this class will be the same as the NSString class, you can use = Assignment copy.
Customizing a Typesitem class, inheriting from NSObject, containing three variables (customizable to add multiple)

TypesItem.h

Copy Code code as follows:

#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 file, in addition to synthesize these three variables

Copy Code code as follows:

@synthesize Type_id,type_memo,type_name;


Also implement the Nscopying protocol method
Copy Code code 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;
}


Values are passed between pages, assuming that the value of the Typeitem in the A->b,a is passed to B

Write code in the. h file in B

Copy Code code as follows:

@property (nonatomic,copy) Typesitem *selecteditem;

In the B.M file
Copy Code code as follows:

@synthesize SelectedItem;

Add code before jumping to B in a.m
Copy Code code as follows:

Bviewcontroller *BVC = [[[Bviewcontroller alloc] initwithnibname:@ "Bviewcontroller" Bundle:nil] autorelease];

Item is a typeitem type and is not empty

Bvc.selecteditem = Item;

[Self.navigationcontroller PUSHVIEWCONTROLLER:BVC Animated:yes];

PS: When the values are passed between pages, the BVC in the Bvc.selecteditem must be in line with the push-past BVC, otherwise the SelectedItem value in the push to B interface must be null.

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.