iOS登录单例
iOS登录单例
一,工程图。

二,代码。
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 - 获取单例
+ (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"
//加入头文件
#import "UserInfo.h" @interface RootViewController () @end @implementation RootViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
} - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view. //给单例的变量赋值
[[UserInfo sharedManager] setUsername:@"李华"];
[[UserInfo sharedManager] setPassword:@"123456"]; //打印单例的值
NSLog(@"---userName----%@",[[UserInfo sharedManager] username]);
NSLog(@"------password---%@",[[UserInfo sharedManager] password]); }


三,输出。
2015-10-14 15:09:55.922 登陆单例[3940:183668] ---userName----李华
2015-10-14 15:09:55.923 登陆单例[3940:183668] ------password---123456
iOS登录单例的更多相关文章
- iOS - 单例传值 (一)
点击打开链接 iOS - 单例传值 (二) 单例只会对某个类实例化一次/单例类,对单例这个类实例化一次有且仅有一个对象 你单例初始化,只能初始化一次,然后你指向的对象,其实都是指向一个内存地址, ...
- [iOS]封装单例类
[iOS]封装单例类 今天在学习iOS的SQLite开发,发现在需要使用SQLite的每个视图中,都需要对数据库进行打开或关闭,觉得挺麻烦的:于是在想能否写个单例类对这些操作进行封(因以前一直在使用D ...
- iOS 创建单例的两种方法
创建一个单例很多办法.我先列举一个苹果官方文档中的写法. [cpp] view plaincopy static AccountManager *DefaultManager = nil; + (Ac ...
- iOS设计模式 - 单例
备注:只能通过类的类方法才能创建单例类的实例,[[类名 alloc]init]创建实例没有用的. 原理图 说明 1. 单例模式人人用过,严格的单例模式很少有人用过 2. 严格的单例模式指的是无法通过常 ...
- iOS之单例
今天在看多线程同步时,突然想到了单例的同步问题.自从dispatch_once出现后,我们创建单例非常简单且安全: static dispatch_once_t pred; static Single ...
- iOS快速单例宏
// 单例 #define DECLARE_SHARED_INSTANCE(className) \ + (className *)sharedInstance; #define IMPLEMENT_ ...
- iOS 之单例,代理,通知,KVO,Block全能解析
//单例 //.h + (Instannce *)shareInstance; //.m static Instannce *instance = nil; @implementation Insta ...
- iOS利用单例实现不同界面间的数据传输
首先写一个单例类,继承NSObject check.h文件中 @property(strong ,nonatomic) UITable * Table; @property(strong ,nonit ...
- [置顶] Objective-C编程之道iOS设计模式单例解析(2)
上一篇文章,提到了单例子类化的问题.正好最近,我在Stack Overflow看见一位国外高人,也谈及了单例子类化的一些内容.思考之后,总结了一些内容.其大意是利用NSDirectory存储不同子类的 ...
随机推荐
- HDU 2883 kebab(最大流)
HDU 2883 kebab 题目链接 题意:有一个烧烤机,每次最多能烤 m 块肉.如今有 n 个人来买烤肉,每一个人到达时间为 si.离开时间为 ei,点的烤肉数量为 ci,每一个烤肉所需烘烤时间为 ...
- 事务的四大特性ACID介绍
事务是恢复和并发控制的基本单位.ACID 事务应该具有4个属性:原子性.一致性.隔离性.持续性.这四个属性通常称为ACID特性. 原子性(atomicity).一个事务是一个不可分割的工作单位,事务中 ...
- js setTimeout 传递带参数的函数的2种方式
js setTimeout 传递带参数的函数的2种方式 Created by Marydon on 2018年9月14日 1.准备工作 function sayYourName(param) { ...
- Unable to read TLD "META-INF/c.tld" from JAR file
Unable to read TLD "META-INF/c.tld" from JAR file CreationTime--2018年7月18日17点46分 Author: ...
- JAVA遍历Map的方法
import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class TestMap { pu ...
- url请求返回结果测试工具(CURL)
官网:http://curl.haxx.se/download.html 具体用法用时百度 或 到时再补充
- logging日志管理-将日志写入文件
# -*- coding: cp936 -*- # test.py #http://blog.chinaunix.net/uid-27571599-id-3492860.html #logging日志 ...
- VC++程序员如何做好界面
本屌丝在新春放假期间闲来无事,在各大编程论坛溜达了一圈.发现年前的帖子中,有VC++程序员在界面开发方面遇到了很多苦恼,有抱怨界面工作不好做的,有抱怨用错了界面库的,也有紧急求得技术问题帮助的.看到这 ...
- Android Listview 隐藏滚动条
在<ListView>标签中设置属性. android:fastScrollEnabled="false" 以下属性scrollbars可以设置为none也可以不设置为 ...
- 【微信小程序】:实现轮播图3秒滚动
wxml模板:(数据一维数组) <scroll-view scroll-y="true"> <swiper autoplay="auto" i ...