[Code notes] logon ticket, Code notes
1. Engineering Drawing.
Ii. Code.
UserInfo. h
#import <Foundation/Foundation.h>@interface UserInfo : NSObject+ (id)sharedManager;@property (nonatomic , retain) NSString* username;@property (nonatomic , retain) NSString* password;@end
UserInfo. m
# Import "UserInfo. h" static UserInfo * userInfo; @ implementation UserInfo # pragma mark-Get Singleton + (id) sharedManager {if (! UserInfo) {userInfo = [[UserInfo alloc] init];} return userInfo;} @ end
RootViewController. h
#import <UIKit/UIKit.h>@interface RootViewController : UIViewController@end
RootViewController. m
# Import "RootViewController. h "// Add the header file # import" UserInfo. h "@ interface RootViewController () @ end @ implementation RootViewController-(id) initWithNibName :( NSString *) bundle :( NSBundle *) handle {self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil]; if (self) {// Custom initialization} return self;}-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view. // assign a value to the single-instance variable [[UserInfo sharedManager] setUsername: @ "Li Hua"]; [[UserInfo sharedManager] setPassword: @ "123456"]; // print the singleton value NSLog (@ "--- userName ---- % @", [[UserInfo sharedManager] username]); NSLog (@ "------ password --- % @", [[UserInfo sharedManager] password]);}
3. output.
15:09:55. 922 login ticket [3940: 183668] --- userName ---- Li Hua 15:09:55. 923 login ticket [3940: 183668] ------ password --- 123456