OGeek|极客世界-中国程序员成长平台

标题: ios - 试图了解何时以及何时不使用单例 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 06:38
标题: ios - 试图了解何时以及何时不使用单例

有很多关于单例的信息,什么时候使用它,为什么你不应该使用它等等。所以为了更好地掌握它,也许有人可以用我正在制作的应用程序中的示例来解释它。

我正在使用 Parse 创建一个带有用户注册的应用。如果我以这种方式使用单例,这是好的还是坏的做法?我在想我将使用我的 User 类在整个应用程序中用户相关操作,也许创建一次 User 类的实例是个好主意:

//  User.h

@interface User : NSObject

+ (instancetype)sharedInstance;

- (void)createNewUserNSString *)username passwordNSString *)password emailNSString*)email;

@end

// User.m

#import "User.h"
#import <arse/Parse.h>

@implementation User

+ (instancetype)sharedInstance {
    static id sharedInstance = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
        NSLog(@"sharedInstance User.m");
    });

    return sharedInstance;
}

- (void)createNewUserNSString *)username passwordNSString *)password emailNSString*)email {
    // Create a new user
    PFUser *newUser = [PFUser user];
    newUser.username = username;
    newUser.password = password;

    // Additional user information
    newUser[@"email"] = email;

    [newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (!error) {
            // Hooray! Let them use the app now.
            NSLog(@"Success created user: %@", newUser);
        } else {
            NSString *errorString = [error userInfo][@"error"];
            // Show the errorString somewhere and let the user try again.
            NSLog(@"Error: %@", errorString);
        }
    }];
}

@end

// LoginViewController.m

#pragma mark - IBActions

- (IBAction)loginButtonClickedUIButton *)sender 
{    
    [[User sharedInstance] createNewUser:self.usernameTextField.text
                                password:self.passwordTextField.text
                                   email:self.emailTextField.text];
}

或者这样做更好:

// User.h

@interface User : NSObject

- (void)createNewUserNSString *)username passwordNSString *)password emailNSString*)email;

@end

// User.m

@implementation User

- (void)createNewUser:(NSString *)username password:(NSString *)password email:(NSString*)email {
    // Create a new user
    PFUser *newUser = [PFUser user];
    newUser.username = username;
    newUser.password = password;

    // Additional user information
    newUser[@"email"] = email;

    [newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (!error) {
            // Hooray! Let them use the app now.
            NSLog(@"Success created user: %@", newUser);
        } else {
            NSString *errorString = [error userInfo][@"error"];
            // Show the errorString somewhere and let the user try again.
            NSLog(@"Error: %@", errorString);
        }
    }];
}

@end

// LoginViewController.m

- (IBAction)loginButtonClicked:(UIButton *)sender
{
    User *newUser = [User new];

    [newUser createNewUser:self.usernameTextField.text
                  password:self.passwordTextField.text
                     email:self.emailTextField.text];
}

另外,如果我误用了任何方式,请直说,感谢您的诚实!



Best Answer-推荐答案


当你想把某样东西做成单例时,这样想:

如果任一选项适用于您的情况,请执行以下操作:

关于ios - 试图了解何时以及何时不使用单例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29854821/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4