This profile

  • Improving the Objective-C API for Swift
  • Interview module: HTTPS certificate validity verification process.
  • Excellent blog: Core Data, Realm, MMKV libraries.
  • Learning materials: a website for learning regular expressions.
  • Development tool: a CLI tool to install XcodexcinfoMark Text, an open source Markdown editing tool.

The development of Tips

Edit: Xiao Haiteng of Normal University

Improved Objective-C API for Swift

The macro NS_REFINED_FOR_SWIFT, introduced in Xcode 7, can be used to hide the Objective-C API from Swift in order to provide a better version of the same API in Swift, while still using the original Objective-C implementation. Specific application scenarios are as follows:

  • When you want to use an Objective-C API in Swift, use a different method declaration, but use a similar underlying implementation. You can also make Objective-C methods properties in Swift, such as objective-C

    + (instancetype)sharedInstance;
    Copy the code

    The method becomes shared in Swift.

  • When you want to use an Objective-C API in Swift, use some Swift specific types, such as tuples. For example, objective-C’s

    - (void)getRed:(nullable CGFloat *)red green:(nullable CGFloat *)green blue:(nullable CGFloat *)blue alpha:(nullable CGFloat *)alpha;
    Copy the code

    The method in Swift becomes a read-only computed property of type a tuple containing four rGBA elements

    var rgba: (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)
    Copy the code

    For easier use.

  • When you want to use an Objective-C API in Swift, rearrange, combine, rename parameters, etc., to make the API more compatible with other Swift apis.

  • Take advantage of Swift’s support for default parameter values to reduce the number of objective-C apis imported into Swift. For example, the extended methods in SDWebImage’s UIImageView (WebCache) classification are reduced from nine to five when imported into Swift.

  • Solve the problem that when Swift calls objective-C API, it may fail to meet expectations due to inconsistent data types. For example, objective-C methods use C-style multi-parameter types; Or an Objective-C method that returns NSNotFound, expects to return nil in Swift and so on.

NS_REFINED_FOR_SWIFT can be used for methods and properties. The objective-C API with NS_REFINED_FOR_SWIFT is renamed as follows when imported into Swift:

  • For initialization methods, prefix the first argument label with “__”
  • For other methods, prefix the base name with “__”
  • Subscript methods will be treated as any other method, preceded by “__”, rather than imported as Swift subscripts
  • Other declarations will prefix their names with “__”, such as attributes

Note: when NS_REFINED_FOR_SWIFT is used with NS_SWIFT_NAME, NS_REFINED_FOR_SWIFT does not take effect, but instead renaming the Objective-C API with the name specified by NS_SWIFT_NAME.

Check it out:

  • IOS mixed programming | Improved Objective-C API for Swift
  • @Apple: Improving Objective-C API Declarations for Swift

Parsing the interview

zhangferry

How does the client ensure the validity of the certificate during HTTPS establishment?

The HTTPS setup process looks like this:

1, Client -> Server: supported protocol and encryption algorithm, random number A

2, Server -> Client: Server certificate, random number B

3, Client -> Server: verify the validity of the certificate, random number C

SessionKey = f(A + B + C) SessionKey = f(A + B + C)

5. Use SessionKey for symmetric encryption communication

Step 3 requires the client to verify the validity of the certificate. Validity verification mainly uses the trust chain and signature of the certificate.

Certificate trust chain

Take the HTTPS certificate of Zhangferry.com as an example for analysis:

The certificate of Zhangferry.com has a section of Issuer Name indicating the Issuer information. The issuer name is TrustAsia TLS RSA CA, and the upper layer is the TrustAsia TLS RSA CA. DigiCert Global Root CA -> TrustAsia TLS RSA CA -> zhangferry.com

DigiCert Global Root CA is the Root certificate, and its issuer is itself. The root certificate is issued by a specific authority and is considered to be trusted. Our computer will be pre-installed with some CA root certificates when installing, check the key string to find the root certificate:

If you can verify that the issuing chain has not been tampered with, then the current certificate is valid.

Issued by effective

To verify that the DigiCert Global Root CA (A) issued the TrustAsia TLS RSA CA (B), you can take advantage of the asymmetry of RSA. There are two steps: issue and verification.

Issue: When user A issues A certificate to user B, user A generates A Hash value from USER B. Then user A encrypts the Hash value using its private key, generates A signature, and stores the Hash value in the certificate of user B.

Verification: Decrypt the signature using the public key of A (embedded in the key string of the operating system) to obtain the Hash value H1. Hash B separately to obtain H2. If H1== H2, it indicates that the certificate has not been tampered and the verification succeeds.

The symmetric encryption and Hash algorithms used in these processes are described in the certificate. And the same goes for each level of verification, all the way to the final certificate node, even if the certificate has passed. The process is as follows:

Image: cheapsslsecurity.com/blog/digita…

Hash conflict resolution

A Hash conflict occurs when two different items use the same Hash algorithm to get the same result. There are two common solutions to Hash collisions: open addressing and chained addressing.

Open addressing

The idea of open addressing is that when an address is already occupied, it is recalculated until an unoccupied address is generated. The corresponding formula is:

Where di is the incremental sequence, m is the hash table length, and I is the number of conflicts that have occurred. According to the content of DI sequence, it can be divided into different processing schemes:

di = 1, 2, 3… M minus 1 is a linear sequence, which is the linear detection method.

di = 1^2, 2^2, 3^2… K ^2, which is a square sequence, is flat detection.

Di = pseudo random sequence, is pseudo random sequence detection method.

Chain address method

Linked address method is used to solve the data aggregation problem caused by open addressing method. It uses a linked list to record all the conflicting values one by one.

Other methods

Hash method: set multiple hash algorithms, if the conflict on the replacement algorithm, recalculation.

Establish public overflow area: store hash table and overflow data separately, and fill the conflict content in the overflow table.

Reference: Wiki – hash table

Good blog

Finishing edit: I am xiong big, dongpo elbow son

Database design: In-depth understanding of Realm’s multithreaded processing mechanism — from: Realm

Realm is a cross-platform mobile database engine that performs better than Core Data and FMDB. The interface is very humanized and easy to use. This article will help you quickly deepen your understanding of Realm and learn more useful techniques. This article has pretty much cleared me up whenever I’ve had a problem with Realm.

How to reduce Realm database crashes — from Nuggets: Am I a bear

I am Bear: Realm crash, not only Realm crash, any database crash is always a problem, there are always a few random bugs, this article summarizes the problems I encountered in the real work and the solutions.

3. MMKV– MMAP-based iOS high-performance universal key-value component — from Nuggets: I am Xiong Da

I am Xiong: Is there really a need for heavy databases in development? You still need NSUserDefaults to be good. If it’s simple storage in your app, then MMAP-based memory mapping MMKV is probably better for you, and it’s 100 times faster than NSUserDefaults.

4. IOS Database comparison: SQLite vs. Core Data vs. Realm — from OSCHINA

Dongpo Eouzi: In iOS, there are many other persistence schemes to choose from besides Core Data officially provided. Each scheme has its own characteristics and application scenarios. This article provides a horizontal comparison of Core Data, SQLite, and Realm, and discusses the path and considerations for converting from SQLite or Core Data to Realm.

5, Core Data with CloudKit — from: Dongpo Elbow

@Dongpo Elbow: Core Data with CloudKit is a network synchronization solution launched by Apple for Core Data. By combining Core Data with CloudKit, real-time Data synchronization across devices and platforms can be realized within Apple ecosystem with only a small amount of code. This six-part series covers details on private database synchronization, public database synchronization, and sharing data between different users.

Learning materials

Mimosa

Learning regular expressions

Address: regexlearn.com/zh-cn

This is a regular expression learning site. It from scratch, can let don’t understand is a small white simple introduction to regular expressions, more specifically, it USES the answer way, step by step with your understanding of the workings of a regular expression as well as the principle, every little levels have the corresponding knowledge and real-time matching show, when you write expressions that you can see the corresponding results. In addition, the way of this level also makes people feel a sense of achievement, so you can’t stop! I believe that is not very familiar with the regular expression of friends is a good learning material.

Tools recommended

CoderStar

xcinfo

Recommended source: Faimin

Address: github.com/xcodereleas…

Software status: Open source, free

Recommended language:

An alternative to Xcodes, which allows us to download Xcode directly from apple’s official website. Download speeds are claimed to be faster than Xcodes.

Mark Text

Address: marktext. App /

Software status: Open source, free

Software Introduction:

A simple and elegant open source Markdown editor that focuses on speed and usability for Linux, macOS and Windows.

Like Typora, it is a single window.

About us

IOS Fish weekly, mainly share the experience and lessons encountered in the development process, high-quality blog, high-quality learning materials, practical development tools, etc. The weekly warehouse is here: github.com/zhangferry/… If you have a good content recommendation, you can submit it through the way of issue. You can also apply to be our resident editor to maintain the weekly. Also can pay attention to the public number: iOS growth road, backstage click into the group communication, contact us, get more content.

Phase to recommend

IOS Fishing Weekly 38th issue

IOS Fishing Weekly 37th issue

IOS Fishing Weekly 36th issue

IOS Fishing Weekly 35th issue