Environment: Mac OS X 10.9, Xcode 5.0.2
I want use a constant fields for Notification name. Like this:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didUploadFinished)
name:uploadNotif_uploadFileFinished
object:nil];
I use a constant uploadNotif_uploadFileFinished
instead @"uploadNotif_uploadFileFinished"
.
Constant fields instead @”string” give us, check name of the notification, during compiling. But realization of this may be different. I`am found methods using extern constant or static constant, see bellow example, but maybe exists better way to realization it?
Example to emulate “enum” of NSString, based on extern constant:
Upload.h:
#import <Foundation/Foundation.h>
@interface Upload : NSObject <NSURLConnectionDelegate>
-(void)finishUpload;
@end
// Declaretion list name of notifications for Upload Objects. Enum strings:
// ________________________________________
extern NSString* const uploadNotif_uploadFileFinished;
extern NSString* const uploadNotif_uploadError;
// ________________________________________
Upload.m:
#import "Upload.h"
@implementation Upload
-(void)finishUpload
{
[[NSNotificationCenter defaultCenter]
postNotificationName:uploadNotif_uploadFileFinished object:nil];
}
@end
// Initialization list name of notifications for Upload Objects. Enum strings:
// ________________________________________
NSString* const uploadNotif_uploadFileFinished = @"uploadNotif_uploadFileFinished";
NSString* const uploadNotif_uploadError = @"uploadNotif_uploadError";
// ________________________________________
This realization not very like to me because of it is not clear where declared “uploadNotif_uploadFileFinished” constant. Ideal variant may like this Upload::uploadFileFinished
:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didUploadFinished)
name:Upload::uploadFileFinished
object:nil];
But how realize this?