How to set a BOOL value that can be used in other files of obj-c
I'm trying to set a BOOL value that needs to be checked within another file where I do the XML parching.
Its like this in filename.m:
if (internetConnectionStatus == NotReachable) {
//SET A BOOL VALUE TO FALSE
} else {
//SET A BOOL VALUE TO TRUE
}
And in XMLParserfile.m I need to check whether the BOOL value set in filename.m is TRUE or FALSE
if (BOOLVALUEORSOMETHING ==开发者_StackOverflow中文版 TRUE) {
//DO THIS
} else {
//DO THAT
}
This could be a stupid question, but what is the best way to do this.
Use it as a property.
@property (nonatomic, assign) BOOL number;
Something like this should work. I'm not sure your entire goal, so it some cases you might actually want to use static classes or methods instead.
filename.h:
#import <Foundation/Foundation.h>
typedef enum {
NotReachable
} InternetConnectionStatus;
@interface filename : NSObject
{
BOOL isReachable;
}
@property BOOL isReachable;
@end
filename.m:
#import "filename.h"
@implementation filename
@synthesize isReachable;
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
InternetConnectionStatus internetConnectionStatus = NotReachable;
if (internetConnectionStatus == NotReachable) {
//SET A BOOL VALUE TO FALSE
self.isReachable = FALSE;
} else {
//SET A BOOL VALUE TO TRUE
self.isReachable = TRUE;
}
}
return self;
}
@end
XMLParserfile.h:
#import <Foundation/Foundation.h>
@interface XMLParserfile : NSObject
@end
XMLParserfile.m:
#import "XMLParserfile.h"
#import "filename.h"
@implementation XMLParserfile
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
filename *file = [[filename alloc] init];
if(file.isReachable == TRUE)
{
// DO THIS
}
else
{
// DO THAT.
}
}
return self;
}
@end
You can do this in one of several ways. I've discussed that in this answer to another question.
精彩评论