“Writing High Quality OC Code” has been successfully completed chapter one, two, three, four, five, six, eight! Attach a link: Objective-c code for iOS (1) — Objective C code for iOS (2) — Objective C code for iOS (3) — Interface and API design Objective-c code (5) — Memory management mechanism (6) — Block column iOS Writing high quality Objective-C code for iOS (GCD


The topic of this article is “System Framework”. In our daily development, we will use some framework of the system. Therefore, it is necessary to be familiar with the system framework. So today, let’s explore what are the gems of the system framework?

1. Be familiar with the system framework

The system framework includes but is not limited to:

1. The Foundation. Features include data storage and persistence, text processing, date and time calculations, sorting and filtering, and networking. The classes, protocols, and data types defined by Foundation are used throughout the macOS, iOS, watchOS, and tvOS SDKS.

2. CoreFoundation: Core infrastructure framework. It provides basic c-language functions and provides basic data management and service functions for applications. C language class and OC object can be converted by seamless bridge technology (__bridge).

UIKit: View frame. Interface and view functionality for iOS and tvOS. (Important: Always use the UIKit framework in the main thread unless otherwise specified.)

CoreAnimation: the CoreAnimation framework. Provides high frame rates and smooth animation without taxing the CPU or slowing down application responsiveness.

CoreGraphics: CoreGraphics framework. Based on the Quartz graphics engine, it provides the necessary data structures and functions for lightweight 2D rendering. Data structures include CGPoint, CGSize, and CGRect.

CoreData: CoreData framework. Provides the ability to write OC objects to the database for easy persistence.

7. CoreText: Text framework. Provides c-level text typesetting and rendering operations.

AVFoundation: Audio and video framework. Provides control camera, as well as processing, composition, control, import, export audio/video functions.

9. CFNetwork: network framework. Provides C-language level network communication capabilities, abstracts BSD sockets into easy-to-use network interfaces for network communication.

10. ARKit: AR framework. 11. SpriteKit: 2D game framework. 12. CoreLocation, MapKit: positioning map related framework. Address Book: Address Book framework. 14. HealthKit: Health-related framework. HomeKit: A framework for smart hardware. 16. PhotoKit: Frames for photo albums. . (There are many more frameworks, see the official documentation below for details.)

PS: Apple official documentation

Use block enumerations more often than for loops

There are four ways to traverse a collection: for loop, NSEnumerator traversal, for-in quick traversal, and block enumeration.

1. For loop: The most basic loop method, similar to other languages.

NSArray *arr = @[@1, @2, @3, @4, @5];
    
for (NSInteger i = 0; i < 5; i++) {
    NSLog(@"%@",arr[i]);
}
Copy the code

2. NSEnumerator traversal:

NSArray *arr = @[@1, @2, @3, @4, @5]; NSEnumerator *enumerator = [arr objectEnumerator]; NSNumber *num = nil; while ((num = [enumerator nextObject]) ! = nil) { NSLog(@"%@",num); }Copy the code

3. For-in quick traversal method: Often used in development.

NSArray *arr = @[@1, @2, @3, @4, @5];
    
for (NSNumber *num in arr) {
    NSLog(@"%@",num);
}
Copy the code

4. Block enumeration method: based on the block to traverse, traverse can not only obtain the object, but also know its subscript. In addition, traversal can be terminated by a Stop object. (*stop = YES)

  • Official: Executes a given block using each object in the array, starting with the first object and continuing through the array to the last object.

  • Executes a given block with each object in the array, starting with the first object and ending with the last.

  • Basic use:

NSArray *arr = @[@1, @2, @3, @4, @5];

[arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    
    NSLog(@"%@",obj);
    
    if (idx == arr.count - 1) {
        *stop = YES;
    }
}];
Copy the code

Use seamless bridging for collections that customize their memory management semantics

Wireless bridge technology (__bridge) : convert “OC objects” to “data structures in C” ** back and forth.

A simple example: seamless bridging between NSArray and CFArrayRef.

NSArray *anNSArray = @[@1, @2, @3, @4, @5]; CFArrayRef aCFArray = (__bridge CFArrayRef)anNSArray; / /! < NSLog(@"Size of array = %li", CFArrayGetCount(aCFArray)); // Output: Size of array = 5Copy the code

4. Use NSCache instead of NSDictionary when building cache

Advantages of NSCache:

  • When system resources are running out,NSCacheWill automatically delete. Delete *** “oldest unused object” *** first.
  • NSCacheKeys are not automatically copied. (because theNSCacheDo not follow<NSCopying>/<NSMutableCopying>Protocol)
  • NSCacheThread-safe. Multithreaded access is still supported without synchronous lockingNSCache.

5. Simplify the load and Initialize implementation code

Keep the load and Initialize methods lean.

Note in load:

  1. If both the class and its parent class have load methods defined, the load method in the main class is called first, and then the load method of the class is called.
  2. It is not safe to use other classes within the load method. (Because other classes may not be loaded at the time of the call)
  3. If a class does not implement load itself, it will not be called whether the superclass implements load or not.
  4. Load should only be used for debugging, not performing tasks.

Notes in Initialize:

  1. If a class does not implement Initialize and its superclass does, the methods of the superclass are run.
  2. The developer has no control over the timing of class initialization.
  3. If the Initizalize implementation code for a class is complex, it uses another class. If those classes are not already initialized, they are forced to do so.
  4. The Initialize method should only be used to set internal data.

Don’t forget that NSTimer retains its target object

  • The NSTimer object retains its target until the timer itself expires, calling [_timer invalidate]; Disables the timer. In addition, the one-time timer will also fail after the task is triggered.

  • Repeating timers can easily lead to circular references. If the target object of such a timer also preserves the timer itself, a circular reference will occur, resulting in a memory leak. It may occur directly or indirectly by other objects.