File Manager and design mode-singleton mode and file manager Design Mode
# Import <Foundation/Foundation. h> # import "User. h "/* 1. file Manager simple Singleton mode */int main (int argc, const char * argv []) {// File Manager // It is generally not used // NSFileManager * fileManage = // [[NSFileManager alloc] init]; NSFileManager * fm = [NSFileManager defamanager manager]; NSLog (@ "% p", fm ); // create a singleton object // the lifecycle of the singleton object is from the start of the function to the end of the function NSFileManager * fm1 = [NSFileManager defaultManager]; NSLog (@ "% p ", fm1); User * user1 = [User defaultUser]; NSLog (@ "% p", user1); user1.age = 10; NSLog (@ "% p age: % d ", user1, user1.age); User * user2 = [User defaultUser]; NSLog (@ "% p age: % d", user2, user2.age); NSLog (@ "% p ", user2); return 0 ;}
#import <Foundation/Foundation.h>@interface User : NSObject{ int _age;}-(void)setAge:(int)age;-(int)age;+(User *)defaultUser;@end
# Import "User. h "static User * instance = nil; @ implementation User-(void) setAge :( int) age {_ age = age ;}- (int) age {return _ age ;} + (User *) defaultUser {// determines whether the object is null if (instance = nil) {// if it is null, initialize instance = [[self alloc] init];} return instance;} @ end
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.