Classes and classes extend the load and Initialize methods call order structures KVO, KVC, Notification singleton Pointers Other interview must-read

Categories and class extensions

Category (Category)

  • Function Enhances the functionality of existing classes. With classification, we can modularize the extension of existing classes without destroying their structure. Add public methods for all subclasses that inherit from a class (because methods in a class can be inherited by all subclasses) to add informal protocols to objects.
  • limitations
    1. Unable to add a new instance variable to an existing class (the class has no place for instance variables);
    2. Method name conflicts occur when the name of a new method in a class is the same as the name of a method in an existing class, the class has a higher priority, and the method in the class completely replaces the method in the existing class (the method of the same name in the existing class can no longer be accessed).
  • Category usage and caution
    1. If the methods in a Category are exactly the same as the existing methods in the current class, then the Category overrides the methods in the class. Because Category has the highest priority, the system calls the override method in the Category when the method of the class is overridden.
    2. Category extension methods are divided into unoverwritten extension methods and overwritten extension methods according to whether the method in the current class is overwritten. If the class wants to reference the unoverwritten extension method in the Category, it must introduce the declaration file of the Category. A class can refer to its own Category only in the.m implementation file (if it refers to its own Category in the.h declaration file, it will cause an infinite loop because of the header file). A subclass can refer to its parent class in either the.h or.m file. If a class references a Category override method without importing the Category declaration file, the system automatically invokes the Category override method
    3. If A Category overrides the method S that class A inherits from its parent, it theoretically does not affect the method S in the same class (for example, class B, where A and B inherit from the same parent)
    4. Does a subclass inherit its parent’s Category: overriding methods in a Category affect the subclass, but the subclass does not actively inherit non-overriding extending methods in the parent’s Category. But by introducing the Category declaration file of the parent class into a subclass, the subclass inherits the non-overriding extension methods of the Category. Notice, inheritance. When the methods in the subclass are identical to the methods in the parent Category (identical except elsewhere), the methods in the subclass override the parent Category, because the subclass overrides the methods inherited from the parent class
    5. The impact of a Category is downward, that is, it affects all subclasses of the Category. For example, if A and B inherit from Super and add A Gtr to A, then only A and all subclasses of A can use this class. Class B is an Extension that cannot use the class Gtr.
    6. When there are multiple categories for a class, the actual method implementation performed when a subclass object calls a method depends on the compile order.
    7. The super method cannot be called in the class method. — Limitations of categories

Categories use

(1) the framework to provide class extension (no source code, can not be modified). (2) do not want to generate a new subclass, such as an extension to NSArray. (3) convenient to do project management, you can share a source in multiple places or do method version management, multi-person collaborative development, with the local version to replace the public version. (4) It is used to separate a relatively large class and put the method with the same function into a classification. Too many functions are encapsulated into a class, resulting in the class file is too large.

The difference between categories and extensions

  1. Classes can only extend methods, not attributes and member variables (an error is reported if member variables are included). If a property is declared in a class, the class generates only set and get declarations for that property, not setter/getter methods, i.e. no implementation
  2. Class extensions are generally only for custom classes, and it doesn’t make sense to add class extensions to system classes (you can’t directly change the code in system files, i.e. properties don’t have set&& getters, and methods can’t be implemented).
  3. Classes are named, class extensions are not

Class extension in SWIFT (no category)

There is no classification in Swift. There are only Extensions in SWIF and they are very powerful.

  • Extensions are Extensions that add new functionality to an existing class, structure, enumerated type, or protocol type. This includes the ability to extend types without access to the original source code (that is, reverse modeling). The extension is similar to the classification in OC, but more powerful, and unlike OC, the Swift extension has no name.
  • Advantages of Extensions in SWIFT:
    1. You can modularize your code and put the same functional code into an extension. Such as processing of VC on web request separately in an extension, the watch related agents, other view control agent, a private method, method of open, view the creation of the processing and so on all can distinguish between open to their own extension (Extensions), the code in this controller hierarchy is very clear.
    2. Unlike OC extensions, in Swift, methods and attributes in class extensions are available externally. And support inheritance.
    3. Note that the extension cannot have properties of storage type. You can only add compute instance properties and compute type properties, or you can use runtime to add properties to classes in inheritance and OC.

The call order of the load and Initialize methods

The load and initialize methods do not show the corresponding methods for calling super.

The order in which the load methods are called

The load function is called when a class or Category is loaded into the Objective-C Runtime (that is, referenced), and implementing this method allows us to perform some class-related behavior while the class is loaded. The load function is called when a class is referenced in a project (before main starts executing), regardless of whether the class is used or not; each class’s load function is called automatically only once.

Load function call features:

  1. The class load method does not override the load method of this class. When both a parent class and a child class implement a load function, their load methods are called, and the load methods of the parent class take precedence over those of the child class.
  2. When a subclass does not implement a load method, its parent class load is not called when the subclass is loaded.
  3. Load methods on a class take precedence over categories.
  4. When more than one Category implements a load method, all load methods are executed in the same order as they were compiled.
  5. When there are multiple different classes, each load is executed in the same order as it was compiled.

The order in which the initialize method is called

The Initialize function is called before the class or its subclasses receive the first message. The messages referred to here include calls to instance methods and class methods. The initialize method is lazy-loaded and will never be called if the program never sends a message to a class or its subclasses.

  1. The initialize method of the parent class is executed before the subclass.
  2. When a subclass does not implement the Initialize method, the initialize method of its parent class is called before the subclass receives the first message. When a subclass implements the Initialize method, it is overridden. It’s kind of polymorphic.
  3. When more than one Category implements initialize, methods in the class are overridden and only the initialize method of the last Category in the Compile Sources list is executed.

The structure of the body

Common structure

typedef struct _NSRange {
    NSUInteger location;
    NSUInteger length;
} NSRange;
Copy the code
struct CGPoint {
  CGFloat x;
  CGFloat y;
};
typedef struct CGPoint CGPoint;
typedef CGPoint NSPoint;
Copy the code

Constructor function: NSMakePoint(10,9); CGPointMake (10, 9); NSStringFromPoint(point)

struct CGSize {
  CGFloat width;
  CGFloat height;
};
typedef struct CGSize CGSize;
typedef CGSize NSSize;
Copy the code

NSMakeSize(10, 8); CGSizeMake(10, 8); NSStringFromSize(size);

struct CGRect {
  CGPoint origin;
  CGSize size;
};
typedef struct CGRect CGRect;
typedef CGRect NSRect;
Copy the code

Storage location and size, that is, a rectangular range. Create: NSMakeRect(10, 10, 80, 80); CGRectMake(10, 10, 80, 80); (Common) Print: NSStringFromRect(rect)

What’s the difference between a structure and a class

  1. A structure can only encapsulate properties, whereas a class can encapsulate methods as well as properties.
  2. Structs are allocated on the stack, and classes are allocated on the heap (the stack is efficient and the heap is small, and vice versa).
  3. Structure assignments are direct assignments, while class (object) assignments are pointer assignments (address of object).

KVO, KVC, Notification

KVC

KVC is key value coding (NSKeyValueCoding), an informal Protocol that provides a mechanism for indirectly accessing properties of objects. The property of the class can be read and set by specifying a string identifier that represents the name of the property to be accessed.

  • As for how the KVC mechanism finds value through key: When an object is called through KVC, such as [self valueForKey:@ “someKey”], the program automatically tries to resolve the call in several different ways. If not, we will continue to look for an instance variable (iVar) with someKey. If not, we will continue to try to call -(id) valueForUndefinedKey:. If the method is still not implemented, the program throws an NSUndefinedKeyException exception. (Note: key-value Coding searches not only for the method someKey, but also for the method getsomeKey, preceded by get, or _someKey and _getsomeKey.) The main purpose of the valueForUndefinedKey: method is that when you use the -(id)valueForKey method to request a value from an object, the object has a last chance to respond to the request before an error occurs.

KVO

KVO is key value observation, which is one of the key technologies based on KVC. The feature is that key-value observation can be used to register as an observer of an object and be notified when a property of the object changes. KVO use: Observers add addobserver: forkeyPath: options: context: after the keypath value of the observed change (note that simply changing the value will not call this method, only to change the value by getter and sett will trigger the KVO), Will call in the observer method observerValueForKeyPath: ofobject: change: context: to implement this method for KVO response to the notice.

One area that has been specifically used is monitoring the status of button clicks. Button [self addObserver:self forKeyPath:@"highlighted"options:0 context:nil]; #pragma mark KVO - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if([keyPath isEqualToString:@"highlighted"] ) { [self setNeedsDisplay]; }} For the system is based on keypath to change the corresponding value, in theory is the same as KVC mechanism.Copy the code
  • KVO internal implementation principle:

    1. KVO is implemented based on the Runtime
    2. When an object of a class is observed for the first time, the system dynamically creates a derived class of the class at run time, in which setter methods of any observed properties in the base class are overridden. Derived classes implement the real notification mechanism in overridden setter methods (Person -> NSKVONotifying_Person)
  • Use steps:

    1. Register observer (specifies observer and observed attributes for being observed)
    2. Implement the callback method
    3. Trigger the callback method
    4. Remove observer
  • KVO crashed because the observed object was destroyed (the observed object is a local variable). The observer is released, but the listener (pop, etc.) is not removed. The registered listener is not removed, and the listener is registered again.

KVO code examples
- (void)testKvo { HMPerson *p = [[HMPerson alloc] init]; p.age = 20; [p addObserver:self forKeyPath:@"age" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil]; p.age = 30; p.age = 40; self.p = p; } - (void)dealloc { [self.p removeObserver:self forKeyPath:@"age"]; } /** * The ** @param keyPath property name is called when the value of one of the monitored properties has changed. * @param object Which object has its property changed? * @param change property modification (property original value, property latest value) * @param context void * == id */ - (void) observeforkeypath :(NSString) *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {NSLog(@"%@ the %@ property of the object has changed: %@", object, keyPath, change); }Copy the code

Notification

The observer pattern, the controller to defaultNotificationCenter add their own notification, other classes to register the notification can be notified, These classes can be notified when doing your own operations (more than the default random order observer to send notice to observers, and each observer to some observers, such as the current operation can finish his turn to operation, can be arranged by NotificationQueue observer reaction order, also can set a time to reflect in the add a observer, Canceling an observation requires logging out in both viewDidUnload and dealloc.

Is it possible to put time-consuming operations in notifications?

If the notification is sent from an asynchronous thread, then time-consuming operations can be performed. If notifications are sent in the main thread, then time-consuming operations cannot be performed. Because the operation is performed on the same thread as the notification thread.

Protocol and Delegate

Protocol: A list of methods that can be shared by multiple classes. Proxy: This is a design pattern that is essentially a stereotype of call ids between two objects.

Protocol (Protocol)
  • role

    1. Used to declare some method.
    2. In other words, a Protocol is made up of a series of method declarations.
    3. Any class that complies with Protocol owns all of Protocol’s method declarations.
  • The written form

    @protocol the protocol name // method declaration list // There are two keywords in the protocol that control whether the method is implemented or not (the default is @required, which in most cases is for communication between programmers) @required // The method must be implemented (if not, The compiler warns) @optional // This method doesn't have to implement @endCopy the code

    Class compliance agreement:

    @interface Class name: parent class < Protocol name 1, protocol name 2... > @endCopy the code

    Protocol compliance: A protocol can comply with multiple other protocols. If a protocol complies with other protocols, it owns method declarations in other protocols.

    @protocol Indicates the protocol name < protocol 1, protocol 2> @endCopy the code
  • The base protocol NSObject is a base class, the most fundamental, basic class that any other class will eventually inherit from. There’s also a protocol called NSObject, which is a base protocol, the most fundamental, basic protocol. The NSObject protocol declares many of the most basic methods, description, retain, and release. It is recommended that every new protocol comply with NSObject.

The agent (delegate)

  • Purpose of agency
    1. Data and messages are passed between two objects, and the purpose of the broker is to change or transfer the chain of control. Allows a class to notify other classes at certain times without having to retrieve Pointers to those classes. You can reduce framework complexity. On another point, proxies can be understood as an analogy to the callback listening mechanism in Java.
    2. Decouple and split business logic

The code for

Reverse proxy value transfer process

@protocol ViewController4Delegate <NSObject> - (void)labelFontSize: (CGFloat)fontSize; @end @interface ViewController4 : FatherViewController // Have a delegate @property (assign, nonatomic)id <ViewController4Delegate>delegate; End Client: If ([self.delegate respondsToSelector:@selector(labelFontSize:)]) {[self.delegate labelFontSize:_fontSize]; } Proxy:.h@interface ViewController3: FatherViewController <ViewController4Delegate>// Comply with the protocol @end pragma mark implements the ViewController4Delegate protocol method (void)labelFontSize:(CGFloat)fontSize{ _label.font = [UIFont systemFontOfSize:fontSize]; } event vc4. Delegate = self; // Set the proxyCopy the code

Comparison of Notification, KVO and agents

What is the difference between notification and agreement?
  1. The protocol has a chain of control (HAS-A) relationship; the notification does not.
  2. NSNotification allows you to pass data and messages to multiple objects. For example, NSNotification can be used to notify multiple Controller objects of changes in the Module layer; Protocol (proxy mode) can only pass data and messages to one object. In the notification pattern, there is often a one-to-many relationship.
  3. The delegate is more efficient than the Notification.
  4. Delegate methods are more straightforward than Notification. Typically, delegate methods need to focus on the return value, which is the result of the delegate method.
What is the difference and usage between Notification and KVO?

Identical: Both are observer patterns

Difference: KVO can only react to attributes, not methods or actions. If you just want to observe a property of an object, you can use KVO. KVO is a direct interaction between objects, in which the observed sends messages directly to the observer. You can only detect attributes in the class, and attribute names are found through NSString. The compiler will not help you detect errors and complete, pure hand typing will be more prone to error. In the case of notifications, both interact with the notification center object, and the objects do not know each other. The feature of NSNotification is that the observer needs to send the notification actively first, and then the observer registers the monitoring, and then responds, which is one step more than KVO to send the notification. However, its advantage is that the monitoring is not limited to the change of attributes, but also can monitor various state changes, with a wide monitoring range and flexible use.

Advantages and disadvantages of Notification:

  1. You don’t need to write much code and the implementation is relatively simple
  2. Multiple objects can respond to notifications from one object, and the one-to-many approach is easy to implement

Disadvantages:

  1. Compile-time does not determine whether stubble notifications are handled correctly
  2. To release a registered object, cancel the registration in the notification center
  3. During debugging, it is difficult to track the work and control flow of the program
  4. A third party is required to manage the controller’s contact with the observer
  5. Controllers and observers need to know the notification name, UserInfo Dictionary keys, in advance. If these are not defined in the working interval, then out of sync will occur
  6. After the notification is issued, the object that issued the notification cannot get any feedback from the observer.

Advantages and disadvantages of KVO:

  1. Provides a simple method to synchronize two objects
  2. Can react to objects that we didn’t create
  3. The ability to provide the most recent and previous values for a property to observe
  4. Use keypaths to view properties, so you can also view nested objects

Disadvantages:

  1. The observed properties must be defined in string, so the compiler does not issue warnings and checks
  2. Refactoring of attributes will make observations unavailable
  3. Complex “if” statements require that the object is observing multiple values, because all observations are pointed to through a method
Notification, KVO, agent selection

Notifications are flexible (one notification can be received by multiple objects, and one object can receive multiple notifications). The proxy is more formal, but has a lot of code (1 to 1 by default); KVO has poor performance (new classes will be generated dynamically at the bottom). It can only listen for the change of an object’s property and is not recommended (one object’s property can be listened on by multiple objects, and one object can listen on other properties of multiple objects).

The article recommended

In-depth analysis of the three different manual trigger KVO

other

A circular reference
  • What attributes are used in the property declarations of both the principal and the principal? Why is that? The property declaration attribute for both the delegate and the delegate is assign instead of retain. To avoid memory leaks caused by circular references.

  • Why do many built-in classes, such as UITableViewController’s delegate property, assign instead of retain? Causes circular references. The controller will perform a retain operation on its internal view. If tableView also performs a retain operation on the proxy (controller), then the cyclic retain problem will occur. In delegate we just want to get the object that implements its delegate method, and then we just get the pointer to that object, and we don’t want to change it or do anything else, so we just get the pointer using assign. With retain, the counter increases by 1. We might expect somewhere else to release the delegate object, and then do something with it based on whether or not it’s been released. But in fact its retainCount is still 1 and is not released until dealloc of the UITableViewController is released. (I’m just using dealloc as an example. A retained object is released in dealloc.) This is where some problems arise. If you’re sure there won’t be any conflicts, or if you also want to use the delegate object until you don’t use it, use retain as well, just one last release.

  • The problem with circular references is understood as follows: two classes, A and B, are created and now have A reference count of 1. Now let A and B reference each other (A has an attribute that is the B object, and the attribute description is retain; The reference count of both objects has now been increased by 1 to become 2. Now execute [A release]; [B release]; , and then you realize that A and B cannot be released, because to release A, you must first release B, and then release A in B’s dealloc method. Similarly, in order to release B, we must first release A, and then release B in A’s dealloc method. So these two objects will always be in memory without being freed. This is known as the problem of circular references. A common way to solve this problem is to set the reference property to assign instead of retain.

IOS callback mechanism, and a simple comparison
  1. Target action pair: When there is a close relationship between two objects, such as a view controller and a view under it.
  2. Proxy: Also called delegate, when an object receives multiple events and asks the same object to handle all of them. Delegate mechanisms rely on methods defined by a protocol to send messages.
  3. Notification: When multiple objects or two unrelated objects are required to handle the same event.
  4. Block: For simple tasks where the callback occurs only once.

The singleton

Some classes in the Foundation and Application Kit frameworks only allow the creation of singleton objects, which are unique instances of those classes in the current process. For example, the NSFileManager and NSWorkspace classes are used to instantiate singleton objects on a process-based basis. When an instance is requested from these classes, they pass you a reference to a single instance, which is allocated and initialized first if it doesn’t already exist. Singleton acts as a control center, directing or coordinating the various services of the class. If the class is conceptually only one instance (such as NSWorkspace), a singleton instance should be generated, not multiple instances.

How to implement a singleton pattern class, give ideas, do not write code. · A global instance must be created first, usually stored in a global variable set to nil · Factory methods are provided to access the global instance and check if the variable is nil, if nil a new instance is created, Global variables are initialized in +allocWithZone: on the first call to the factory method, so we need to override this method to prevent new instances from being created using the standard alloc method. To prevent new instances from being created using copy, You need to implement the -copyWithZone method. You only need to return the object in this method, and the reference count doesn’t need to change, because singleton objects are not allowed to be destroyed, so you don’t need to keep them. Global instances are not allowed to be freed. Therefore retain, release, autorelease method must be rewritten

Use dispatch_once to implement singletons

Many people implement a singleton like this:

@implementation XXClass + (id)sharedInstance { static XXClass *sharedInstance = nil; @synchronized(self) { if (! sharedInstance) { sharedInstance = [[self alloc] init]; } } return sharedInstance; }Copy the code

By comparison:

@implementation XXClass
+ (id)sharedInstance {
    static XXClass *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!sharedInstance) {
            sharedInstance = [[self alloc] init];
        }
    });
    return sharedInstance;
}
Copy the code

Using Dispatch_once keeps the thread safe and the developer does not have to worry about locking or synchronization. In addition, Dispatch_once is more efficient and does not use heavyweight synchronization mechanisms. Instead, this function uses “atomic access” to query the tag to determine whether the corresponding code has already been executed. The latter performed twice as fast when tested on 64-bit Mac OS X.

Pointer to the

The pointer itself is immutable, but the data structure to which it points can be changed.

Pointers on the heap and stack

The block of memory to which the pointer points is allocated is called the pointer on the heap and the pointer on the stack. Pointers on the heap can be stored in a global data structure for different functions to access the same block of memory. Pointer on the stack that is inaccessible after the function exits.

Pointer types

  • Pointers in data structures? In fact, the address pointing to a piece of memory, through the pointer to achieve complex memory access.
  • Function pointer? An entry address that points to a block of functions.
  • A pointer to a pointer? The variable to which a pointer points is a pointer, that is, the value of a pointer, which is an address. The variable to which the pointer points is also four bits long.

The difference between Pointers and addresses

  1. Pointer means that a pointer variable already exists and its value is an address. The pointer variable itself is stored in a four-byte address, and the address concept itself does not mean that any variable exists.
  2. The value of a pointer, if not restricted, is usually changeable and can point to another address. An address represents a location in memory space that is assigned to a pointer.
  3. Addresses themselves have no concept of size. The size of a pointer to a variable depends on the type of variable stored after the address.

How to prevent the use of Pointers out of bounds?

You must have a pointer to a valid memory address,

  1. Prevents arrays from crossing bounds
  2. Prevents copying too much into a block of memory
  3. Prevent null Pointers
  4. Prevents changes to Pointers modified by const
  5. Prevents changes to content pointing to static storage
  6. Prevents two releases of a pointer
  7. Prevent wild Pointers

Nil nil null NSNull difference

Nil is the macro used by OC objects to indicate that the object is empty; Nil is the macro used by the OC class to indicate that the class points to null; NULL is a macro used by C to represent a NULL pointer (SEL, etc.). NSNull is a class type that represents an empty placeholder object (such as the null value of charity non-nil in NSArray NSDictionary)

Difference between ID and InstanceType

  1. Instancetype can only be returned by a function or method
  2. Id can be the return value of a function or method, the parameter type, or used to define variables
  3. Instancetype has the advantage of being able to precisely limit the type of value returned

The interview questions

Some of the statements

  • At sign property, at sign property is actually a getter and setter, and at sign synthesize is the synthesis of those two methods.

  • @class is used in header files. You only need to know the name of the referenced class and do not need to know its internal entity variables and methods. Therefore, @class is used in header files to declare the name of the class. In the implementation class, we use the internal entity variables and methods of the referenced class, so we use #import to include the header file of the referenced class. At sign class tells the compiler that there is such a class. @class can also solve the problem of loop dependency, for example, A.h imports B.h, and B.h imports A.h. The compilation of each header file must make the object compile successfully. Using @class can avoid this situation.

  • Before @Interface can create an object of a particular class, the Objective-C compiler needs some information about that class. He must know the object’s data members and the features it provides. You can use the @interface directive to pass this information to the compiler.

  • An @implementation compiler instruction indicating that code will be provided for a class. The class name appears after @implementation.

Load and initialize methods

  • +(void)load;

    1. Called when a class is first loaded into the OC runtime system (memory)
    2. It’s called as soon as the program starts
    3. The program is called only once during execution
  • +(void)initialize;

    1. It is called when a class is first used (such as when a method of the class is called)
    2. It’s not called as soon as the program starts

static

  • The static variable is used in the function body. Unlike the auto variable, the memory of the static variable is allocated only once. Therefore, the value of the static variable remains the same in the next call.
  • A static global variable in a module that can be accessed by functions in the module but not by other functions outside the module. The use of this function is restricted to the module in which it was declared.
  • A static member variable of a class is owned by the entire class. There is only one copy of all objects of the class. The static member function in a class is owned by the whole class. This function does not receive the this pointer, so it can only access the static member variable of the class.

Static Defines a static variable that is initialized only once and is not released until the program destroys it. Declare an external variable whose effect is limited to this file module. Extern defines an external variable that acts as an extension of the class for other external classes to access; Can also be used as a pass value.

const

Const means “read only” and is a modifier. Judicious use of the const keyword makes the parameter immutable, preventing it from being inadvertently modified by code. This will reduce the number of bugs. When we define a const variable, we usually need to initialize it because we don’t have a chance to change it later. For Pointers, we can specify that the pointer itself is const, that the data to which it points is const, or that both are const. In a function declaration, const can modify the parameter to indicate that it is an input parameter and cannot change its value inside the function; If a member function of a class is const, it is a constant function and cannot modify a member variable of the class. For member functions of a class, it is sometimes necessary to specify that the return value is const so that the return value is not an “lvalue.”

  • Const int a has the same effect as int const a. A cannot be modified.

  • Const int *a const modifies int, which in turn defines an integer. A is a pointer to a constant integer. So integers cannot be modified, that is, *a cannot be reassigned, and Pointers can be modified to point to another address space.

    const int *a = 0; Int b = 1; a = &b; B = 20; *a = 20 *a = 20; // This is not allowed and will cause an error because *a is const and cannot be reassignedCopy the code
  • Int *const a const modifies a, which is a constant pointer to an integer. So Pointers cannot be modified, but the value of the address they point to can be modified.

    int b = 1; int *const a = &b; B = 20; // the output of *a is 20. *a = 20 can be used instead of a = &b; // This is not allowed and will cause an error because the pointer a is const and cannot be modifiedCopy the code
  • Int const * const a A is a constant pointer to a constant integer, so the pointer cannot be modified, and the integer it points to cannot be modified.

    int b = 1; int c = 2; Float d = 3.0; int const * const a = &b; B = 20; A = &c; // This is not allowed and will report an error because the pointer a cannot be changed. Const int* const a can also be written as const int* const a, they do the same thing.Copy the code
  • Modifier string comparison

    // "*coder" cannot be modified, "coder" can be modified const NSString *coder = @"Hello world!" ; // "*coder" cannot be modified, "coder" can be modified NSString const *coder = @"Hello world!" ; // "coder" cannot be modified, "*coder" can be modified NSString * const coder = @"Hello world!" ;Copy the code

The difference between define and const?

  1. The compiler handles the define macro differently during the preprocessing stage. Const constants are used at compile time.
  2. The define macro doesn’t have a type, it doesn’t do any type checking, it just expands. Const constants have specific types, and type checking is performed at compile time.
  3. Different storage methods define macros just expand, as many times as they are used, no memory allocated. Const constants are allocated in memory (either in the heap or on the stack).
  4. Const saves space and avoids unnecessary memory allocation. For example, from an assembly point of view, const defines only the memory address. It is not an immediate number like #define. Therefore, a constant defined by const has only one copy during program execution, whereas a constant defined by #define has several copies in memory.
  5. Compilers usually do not allocate storage for ordinary const constants. Instead, they are stored in a symbol table. This makes them a compile-time constant and makes them efficient without storing and reading memory.
  6. Advantages of const over #define: Const constants have data types, while macro constants have no data types. The compiler can perform type safety checks on the former. The latter, on the other hand, does character substitution only, has no type-safety check, and may produce unexpected errors (marginal effects) in character substitution.

How are atomic locks added

@synthesize name = _name; - (void)setName:(NSString *)name { @synchronized(self) { _name = [name copy]; } } - (NSString *)name { @synchronized(self) { return _name; }}Copy the code

Push mechanism

  • IOS notification push works

    The working mechanism of iOS notification push can be summarized as follows:

    Provider is the Push server of an iPhone application.

    APNS, short for Apple Push Notification Service, is an Apple server.

    Phase 1: The application packages the message to be sent, with the identity of the destination iPhone, and sends it to APNS.

    Phase 2: APNS looks for iphones with corresponding logos in its list of iphones registered with Push service and sends the message to iPhone.

    Phase 3: The iPhone delivers the incoming message to the appropriate application and pops up a Push notification.

  • The whole push process



    From the picture above, we can see:

    1. The application registers the message push.
    2. The iOS device token is obtained from the APNS Server, and the application receives the Device token.
    3. The application sends the Device token to the PUSH server program.
    4. The server sends a message to the APNS service.
    5. The APNS service sends the message to the iPhone app.

The difference between heap and stack

First of all, the heap and stack are an area of memory. Heap is a discontinuous memory area, is dynamically allocated, there is no static allocation of the heap, his release is controlled by the programmer, easy to produce storage leakage, reduce the efficiency of the program. But the heap is more flexible and has more space. A stack is a contiguous area of memory with both static allocation (such as allocation of local variables) and dynamic allocation (alloc), but dynamic allocation of the stack is freed by the compiler without manual implementation. Less space is available from the stack. To be specific:

  1. Application size:

Stack: Under Windows, a stack is a low-address data structure, a contiguous area of memory. The address of the top of the stack and the maximum size of the stack are predetermined by the system. On WINDOWS, the stack size is 2M (or 1M, which is a constant determined at compile time), and overflow will be prompted if the amount of space requested exceeds the stack’s remaining space. Therefore, less space can be obtained from the stack. Heap: A heap is a data structure that extends to a high address and is a discontinuous area of memory. This is because the system uses a linked list to store the free memory address, which is naturally discontinuous, and the traversal direction of the list is from low address to high address. The size of the heap is limited by the amount of virtual memory available in the computer system. Thus, the heap is more flexible and larger. 2. Fragmentation problem: For the heap, frequent new/ DELETE is bound to cause the discontinuity of memory space, resulting in a large number of fragmentation, reducing the efficiency of the program. This is not a problem with stacks, because stacks are first in, last out queues, and they are so one-to-one that it is never possible for a block of memory to eject from the middle of the stack. 3. Allocation method: The heap is dynamically allocated, there is no statically allocated heap. There are two types of stack allocation: static allocation and dynamic allocation. Static assignment is done by the compiler, such as assignment of local variables. Dynamic allocation is done by the Alloca function, but stack dynamic allocation is different from heap dynamic allocation, which is released by the compiler without manual implementation. 4. Distribution efficiency: the stack is a data structure provided by the machine system, and the computer provides support for the stack at the bottom: special registers are allocated to store the address of the stack, and special instructions are executed for pushing and removing the stack, which determines the high efficiency of the stack. The heap is provided by the C/C++ library, and its mechanism is quite complex.

Interview is required

Ali data iOS terminal start speed optimization experience in iOS performance optimization on load and Initialize Han Junqiang blog