Ios development-single-sample macro extraction, ios Development Macro Extraction
In daily development, we often use macros, which are essentially replacing the code during compilation.
Example 1:
// ## Variable names that can be used to splice macros
# Define demo (xxx) int # xxx"
Main (){
Int intA = 10;
Int intB = 20;
NSLog (@ "=======% d", demo (A) + demo (B ));
}
How to Use Singleton ios in development
Answer for you.
1. Basic Concepts
The Singleton mode is a common software design mode. Its core structure only contains a special class called Singleton class. The Singleton mode ensures that there is only one instance for one class in the system and the instance is easy to access.
2. Use Singleton mode in IOS
1. if an object is created, it will consume a lot of system resources. In this case, the single-instance mode is used, because only one instance is needed, which will save the alloc time.
2. in IOS development, if many modules use the same variable, if this variable is put into the singleton class, it will become easy for all calls to access this variable. Otherwise, only one module can be passed to another, which increases risks and complexity.
3. Basic Steps for creating the singleton Mode
1. Declare a static instance of the singleton object and initialize it as nil
2. Declare the factory method of a class, generate an instance of this class, and only generate one
3. overwrite the allcoWithZone method to ensure that no redundant object is generated during alloc.
4. Implement the NSCopying protocol and override the release, autorelease, retain, and retainCount methods to ensure that only one instantiated object exists.
5. In a multi-threaded environment, use the @ synchronized keyword.
[Cpp] view plaincopyprint?
//
// UserContext. h
// SingleDemo
//
// Created by andyyang on 9/30/13.
// Copyright (c) 2013 andyyang. All rights reserved.
//
# Import <Foundation/Foundation. h>
@ Interface UserContext: NSObject
@ Property (nonatomic, retain) NSString * username;
@ Property (nonatomic, retain) NSString * email;
+ (Id) sharedUserDefault;
@ End
[Cpp] view plaincopyprint?
//
// UserContext. m
// SingleDemo
//
// Created by andyyang on 9/30/13.
// Copyright (c) 2013 andyyang. All rights reserved.
//
# Import "UserContext. h"
Static UserContext * singleInstance = nil;
@ Implementation UserContext
+ (Id) sharedUserDefault
{
If (singleInstance = nil)
{
@ Synchronized (self)
{
If (singleInstance = nil)
{
SingleInstance = [[[self class] alloc ...... remaining full text>
How to Use the IOS Singleton mode?
The Singleton mode for all programming languages is similar. Object-c, Java, C ++, and so on have little to do with the language, but only have to do with the syntax.
In IOS, if you have a class: AccountManager, You need to define a Singleton, the footwork is as follows:
1. The. h file should have the following definitions:
+ (Id) sharedInstance;
2. classes in the. m file should be defined as follows:
// Declare a globally unique static object, which is also the AccountManager type
Static AccountManager * _ sharedInstance;
// Method implementation
+ (Id) sharedInstance {
@ Synchronized ([AccountManagerclass]) {
If (_ sharedInstance = nil ){
_ SharedInstance = [[AccountManageralloc] init];
}
}
Return_sharedInstance;
}
3. If you want to use this Singleton in other class objects and call a method of this Singleton (todoSomething:
[[AccountManager sharedInstance] todoSomething];
Additional instructions:
1. The sharedInstance name is often used by me. You can use any other name. In short, the AccountManager class can only have one instance during the entire application process.
2. The examples here show you how to implement a "lazy" Singleton. You can also use other Singleton methods to complete tasks, such as the "Hungry" Singleton. If you don't know what lazy and ELE. Me are, you can check it again on Google.