有很多关于单身人士的信息,何时使用它,为什么你不应该使用它等等。所以希望能够把握得更好一些,也许有人可以用应用程序中的样本来解释它&#39制作。
我正在使用Parse创建一个具有用户注册功能的应用。如果我以这种方式使用单身人士,这是好的还是坏的做法?我认为我将使用我的User
课程在整个应用程序中使用相关操作,并且可能一次创建User
类的实例是个好主意:
// User.h
@interface User : NSObject
+ (instancetype)sharedInstance;
- (void)createNewUser:(NSString *)username password:(NSString *)password email:(NSString*)email;
@end
// User.m
#import "User.h"
#import <Parse/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)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
#pragma mark - IBActions
- (IBAction)loginButtonClicked:(UIButton *)sender
{
[[User sharedInstance] createNewUser:self.usernameTextField.text
password:self.passwordTextField.text
email:self.emailTextField.text];
}
或者更好的做法是这样做:
// User.h
@interface User : NSObject
- (void)createNewUser:(NSString *)username password:(NSString *)password email:(NSString*)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];
}
而且,如果我误解了任何一种方式请说出来,我感谢你的诚实!
答案 0 :(得分:1)
当你有诱惑要做出单身人士时,请这样思考:
如果其中一个选项适用于您的案例,请执行以下操作: