首页 > 代码库 > 单例demo
单例demo
长时间不用,竟然写不出来了,之前的一个项目里的用户信息单例,现在看来当初理解的很透测,记下来,方便随时复习。
// QYUserSingleton.m
// Hallyu
//
// Created by XJW on 14-9-29.
// Copyright (c) 2014年 qingyun. All rights reserved.
//
#import "QYUserSingleton.h"
@implementation QYUserSingleton
static QYUserSingleton *userIn;
+ (id) allocWithZone:(struct_NSZone *)zone
{
staticQYUserSingleton *userIn;//静态指针,
staticdispatch_once_t once ;
//NSLog(@"%ld",once);
dispatch_once(&once, ^{
// userIn = [[QYUserSingleton alloc] init];
userIn = [superallocWithZone:zone];
});
return userIn;
}
+ (QYUserSingleton *)sharedUserSingleton
{
if (!userIn) {
userIn = [[QYUserSingletonalloc]init];
}
returnuserIn;
}
- (id)init
{
self = [superinit];
if (self) {
[selfinitWithAccount:@""andPhone:@""andTestNum:@"" andPasscode:@""andUserId:@""andNickName:@""andfavouriyesCount:@""andFollowersCount:@""andIsLogin:NOandIcon_url:@"" ];
}
returnself;
}
- (void)initWithAccount:(NSString *)account
andPhone:(NSString *)phone
andTestNum:(NSString *)testNum
andPasscode:(NSString *)passcode
andUserId:(NSString *)userId
andNickName:(NSString *)nickName
andfavouriyesCount:(NSString *)favCount
andFollowersCount:(NSString *)followersCount
andIsLogin:(BOOL)isLogin
andIcon_url:(NSString *)iconUrl
{
_phoneNum = [[NSStringalloc]initWithString:phone];
_testNum = [[NSStringalloc]initWithString:testNum];
_account = [[NSStringalloc]initWithString:account];
_passcode = [[NSStringalloc]initWithString:passcode];
_user_id = [[NSStringalloc]initWithString:userId];
_nickName = [[NSStringalloc]initWithString:nickName];
_favourites_count = [[NSStringalloc]initWithString:favCount];
_follwers_count = [[NSStringalloc]initWithString:followersCount];
}
- (id)copy
{
returnself;
}
@end
单例demo