Синглтон дизайн шаблона проблема в iphone SDK - PullRequest
0 голосов
/ 05 марта 2012

Я встроил evernote в свое приложение, загрузив образец кода с его сайта. В остальном он работает нормально, только в том, что у него есть одноэлементный класс, который содержит статические значения имени пользователя и пароля. Файл .h этого класса выглядит следующим образом

extern NSString * const username; 
extern NSString * const password; 
@interface Evernote : NSObject {
    Evernote *sharedEvernoteManager;
    }

в .m файле

    NSString * const username = @"username";
    NSString * const password = @"password"; 

@implementation Evernote
    /************************************************************
     *
     *  Connecting to the Evernote server using simple
     *  authentication
     *
     ************************************************************/

    - (void) connect {

        if (authToken == nil) 
        {      
            // In the case we are not connected we don't have an authToken
            // Instantiate the Thrift objects
            NSURL * NSURLuserStoreUri = [[[NSURL alloc] initWithString: userStoreUri] autorelease];

            THTTPClient *userStoreHttpClient = [[[THTTPClient alloc] initWithURL:  NSURLuserStoreUri] autorelease];
            TBinaryProtocol *userStoreProtocol = [[[TBinaryProtocol alloc] initWithTransport:userStoreHttpClient] autorelease];
            EDAMUserStoreClient *userStore = [[[EDAMUserStoreClient alloc] initWithProtocol:userStoreProtocol] autorelease];


            // Check that we can talk to the server
            bool versionOk = [userStore checkVersion: applicationName :[EDAMUserStoreConstants EDAM_VERSION_MAJOR] :    [EDAMUserStoreConstants EDAM_VERSION_MINOR]];

            if (!versionOk) {
               // Alerting the user that the note was created
                UIAlertView *alertDone = [[UIAlertView alloc] initWithTitle: @"Evernote" message: @"Incompatible EDAM client protocol version" delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil];

                [alertDone show];
                [alertDone release];

                return;
            }


            // Returned result from the Evernote servers after authentication
            EDAMAuthenticationResult* authResult =[userStore authenticate:username :password : consumerKey :consumerSecret];

            // User object describing the account
            self.user = [authResult user];
            // We are going to save the authentication token
            self.authToken = [authResult authenticationToken];
            // and the shard id
            self.shardId = [user shardId];

            // Creating the user's noteStore's URL
            noteStoreUri =  [[[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@%@", noteStoreUriBase, shardId] ] autorelease];

            // Creating the User-Agent
            UIDevice *device = [UIDevice currentDevice];
            NSString * userAgent = [NSString stringWithFormat:@"%@/%@;%@(%@)/%@", applicationName,applicationVersion, [device systemName], [device model], [device systemVersion]]; 


            // Initializing the NoteStore client
            THTTPClient *noteStoreHttpClient = [[[THTTPClient alloc] initWithURL:noteStoreUri userAgent: userAgent timeout:15000] autorelease];
            TBinaryProtocol *noteStoreProtocol = [[[TBinaryProtocol alloc] initWithTransport:noteStoreHttpClient] autorelease];
            noteStore = [[[EDAMNoteStoreClient alloc] initWithProtocol:noteStoreProtocol] retain];

        }
    }

Мне нужно сделать имя пользователя и пароль динамическими, чтобы я мог использовать значения из текстовых полей следующим образом

 NSString * const username = usernametextfiled.text;
    NSString * const password = passwrdfiled.text; 

Я получаю сообщение об ошибке, которое говорит о том, что мы не можем добавить текстовое поле до @implementation. Как решить эту проблему?

1 Ответ

2 голосов
/ 05 марта 2012
@interface Evernote : NSObject
...


@property(retain) NSString * username;

@property(retain) NSString * password;

затем

@implementation Evernote


 @synthesize username;
 @synthesize password;

, затем вы можете установить их, используя

[[Evernote sharedEvernoteManager]setusername:yourvariable];
[[Evernote sharedEvernoteManager]setpassword:yourvariable];

, не забудьте освободить переменную в dealloc

...