iOS uses a macro to write a single example

Source: Internet
Author: User

This article only describes the simple interest in the ARC case

The past has not been back down simple interest how to write, that is to know the matter, but also know through the macro to write simple interest, but have been unable to remember, today to record

- (void)viewDidLoad {    [super viewDidLoad];    SIPerson *person = [[SIPerson alloc] init];    NSLog(@"%@",person);    SIPerson *person1 = [[SIPerson alloc] init];    NSLog(@"%@",person1);}

Create person, print, actually 2 objects. No problem.

Create a method

#import "SIPerson.h"static SIPerson *instance_ = nil;@implementation SIPerson///方法1,快速创建对象+ (instancetype)sharedInstance{    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        instance_ = [[self alloc] init];    });    return instance_;}///方法2.这个方法一定要有,就是alloc] init]方法,一定会调用这个方法+ (instancetype)allocWithZone:(struct _NSZone *)zone{    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        instance_ = [super allocWithZone:zone];    });    return instance_;}@end//此处还应该有一个+ copy方法,因为可能是copy,那么有可能是生成新的方法

Method 12 must be realized in order to be simple interest. If Method 2 is not implemented, sharedInstance the implementation is really a simple interest, but by alloc] init] having another object generated

2016-09-17 14:17:45.086 SharedInstance[9158:611161] <SIPerson: 0x7f9ab260bfe0>2016-09-17 14:17:45.087 SharedInstance[9158:611161] <SIPerson: 0x7f9ab2686da0>

If your object is likely to be called in the future copy (it should be declared <NSCopying> agreed), you should also implement a method

- (id)copyWithZone:(NSZone *)zone{    return instance_;}

Copy of the time, is generally generated a new object, so it is not simple interest, but the use of less, not particularly needed, can not implement this method, for Mao to write this way? Because he's the object method, instance_ there's a value inside.

    [super viewDidLoad];    SIPerson *person = [SIPerson sharedInstance];    NSLog(@"%@",person);    SIPerson *person1 = [[SIPerson alloc] init];    NSLog(@"%@",person1);        SIPerson *person2 = [person1 copy];    NSLog(@"%@",person2);

The results are as follows

2016-09-17 14:24:10.555 SharedInstance[9199:615987] <SIPerson: 0x7f8302d07050>2016-09-17 14:24:10.555 SharedInstance[9199:615987] <SIPerson: 0x7f8302d07050>2016-09-17 14:24:10.556 SharedInstance[9199:615987] <SIPerson: 0x7f8302d07050>

Steps to create a new file

/** *  在.h文件中定义的宏,arc * *  SISingletonH(name) 这个是宏 *  + (instancetype)shared##name;这个是被代替的方法, ##代表着shared+name 高度定制化 * 在外边我们使用 “SISingletonH(gege)” 那么在.h文件中,定义了一个方法"+ (instancetype)sharedgege",所以,第一个字母要大写 * *  @return 一个搞定好的方法名 */#define SISingletonH(name) + (instancetype)shared##name;/** *  在.m文件中处理好的宏 arc * *  SISingletonM(name) 这个是宏,因为是多行的东西,所以每行后面都有一个"\",最后一行除外, * 之所以还要传递一个“name”,是因为有个方法要命名"+ (instancetype)shared##name" *  @return 单利 */#define SISingletonM(name) static id instance_ = nil;+ (instancetype)shared##name{    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        instance_ = [[self alloc] init];    });    return instance_;}+ (instancetype)allocWithZone:(struct _NSZone *)zone{    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        instance_ = [super allocWithZone:zone];    });    return instance_;}- (id)copyWithZone:(NSZone *)zone{    return instance_;}

Actual use

#import <Foundation/Foundation.h>#import "SISingleton.h"@interface SIPerson : NSObject@property(copy,nonatomic) NSString *name;@property(copy,nonatomic) NSString *num;//整个就是宏,在.h文件就这样使用SISingletonH(Default)@end
#import "SIPerson.h"@implementation SIPerson//整个就是宏,在.m文件就这样使用SISingletonM(Default)@end

is a sentence, there is no symbol (when the definition of the symbol), it is so simple

In the actual use of the time

    [super viewDidLoad];    SIPerson *person = [SIPerson sharedDefault];    NSLog(@"%@",person);    SIPerson *person1 = [[SIPerson alloc] init];    NSLog(@"%@",person1);    SIPerson *person2 = [person1 copy];    NSLog(@"%@",person2);//打印结果2016-09-17 14:56:39.508 SharedInstance[9292:633076] <SIPerson: 0x7ff85a51d250>2016-09-17 14:56:39.508 SharedInstance[9292:633076] <SIPerson: 0x7ff85a51d250>2016-09-17 14:56:39.508 SharedInstance[9292:633076] <SIPerson: 0x7ff85a51d250>

The file address you can use to get it.

Simply say how to define the swift version of the simple interest, normal writing, did not study simple interest

That's it.

Extract

iOS uses a macro to write a single example

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.