This profile

  • Topic: Apple announces self-repair program.
  • Tips: Mixed | NS_SWIFT_NAME
  • Interview module: binary tree three – order traversal.
  • Good blog: parsing articles from several Swift tripartite libraries.
  • Learning materials: Bang Bang Bin Β· Technology Reference Weekly.
  • Development tools: Mounty: a free small tool for remounting write protected NTFS volumes in read/write mode under macOS.

This topic

Zhangferry: Apple announced to launch self-repair program. Apple will offer parts, tools and repair manuals to individual consumers, starting with the iPhone 12 and 13. Macs with M1 chips will soon be added. The self-repair program will start early next year in the U.S. and expand to other countries in 2022 or later.

This is in line with Apple’s environmental policies, so you can expect the iPhone to last a year or two after replacing the original parts. It’s not going to benefit a lot of users, but apple’s approach is laudable.

At the same time, this also brings a problem, self-help maintenance is a matter of some technical operation, how to ensure the correctness of operation?

Remember my previous experience of changing the battery of iPhone, I succeeded in changing the battery of iPhone6SP for the first two times. As a result, the third attempt to change the battery of the iPhone7P failed. Not only did a misplacement of the battery plug cause a circuit board to burn up, but it also tore a cable connecting to the Home button. Part of the reason is that the link mode between the phone screen and back cover has changed from front and back to left and right. The operation space has become smaller, but the main reason is the lack of relevant knowledge. It’s a simple battery, but replacing a camera, for example, will be more complicated. So what’s important about this self-repair program is who should be allowed to self-repair and how to evaluate them.

Digression: if oneself fix bad, affirmation still calculate to oneself head of πŸ€”.

Source: Apple announces self-repair program

The development of Tips

Mix | NS_SWIFT_NAME

Edit: Xiao Haiteng of Normal University

The NS_SWIFT_NAME macro is used to rename the Objective-C API for Swift when mixing. It can be used for classes, protocols, enumerations, properties, methods or functions, type aliases, and so on. Here are some examples from Apple to see how it can be used:

  • Rename apis that do not fit the Swift style so that they have appropriate names in Swift;
  • Attach the class/enumeration associated with class A as an inner class/enumeration to class A;
  • Rename “name cases that begin with a number after removing the full prefix” enumeration, improving naming of all cases imported into Swift;
  • Rename the factory method “naming does not meet the convention for automatic conversion to constructor import into Swift” so that it is imported into Swift as a constructor (cannot be used in the protocol);
  • It is much more powerful when dealing with global constants, variables, and especially global functions, and can dramatically change the API. For example, you can putGlobal functionInto aA static method, orInstance methodsAnd evenInstance attributes. If you’ve used Core Graphics in both Objective-C and Swift, you’ll know this. Apple said itNS_SWIFT_NAMEUsed on hundreds of global functions, transforming them into methods, properties, and constructors for easy use in Swift.

You can see this example in iOS Mashup | Rename Swift objective-C API.

Parsing the interview

Summer

As one of the most common data structures, tree plays an important role in algorithms.

Understanding trees helps us understand many other data structures, such as graphs, stacks, and so on. It also helps us understand some types of algorithms, such as backtracking and dynamic programming. Of course, in the process of practicing tree problem solving, we can also deepen our understanding of depth first and breadth first algorithm.

Today we take the three order traversal of binary tree as the topic, to start our binary tree learning.

The title

Given a binary tree, return three traversals of the preceding order

Input: [4,2,6,1,3,5,7] 4/2 6 / \ / \ 1 3 5 7

The output

Pre-order traversal: first visits the root, then traverses the left subtree, and finally traverses the right subtree (root -> left -> right)

Order: access the root node -> pre-traverse the left subtree -> pre-traverse the right subtree

Sequential traversal: [4, 2, 1, 3, 6, 5, 7]

Middle-order traversal: First traverses the left subtree, then visits the root node, and finally traverses the right subtree (left -> root -> right)

Order: Middle traverses the left subtree -> visits the root node -> middle traverses the right subtree

Middle order traversal: [1, 2, 3, 4, 5, 6, 7]

Back-order traversal: first traverses the left subtree, then the right subtree, and finally visits the root node (left -> right -> root)

Order: backward traverse the left subtree -> backward traverse the right subtree -> visit the root node

Subsequent traversal: [1, 3, 2, 5, 7, 6, 4]

There are three ways to traverse binary trees

  • recursive
  • Iteration (general iteration with color notation)
  • Morris traversal (not covered today)

recursive

Recursion is the most intuitive and understandable approach to depth-first traversal of a tree (pre-order, mid-order, post-order traversal), but we generally do not recommend recursion for efficiency.

Recursive steps generally follow the following three types:

  1. Determine the recursive parameters and return values
  2. Determine the termination conditions of recursion, recursive algorithm must have termination conditions, to avoid the loop.
  3. Determine the logic of a single recursion
// traversals is an array of outputs
func preorder(_ node: TreeNode?). {
    guard let node = node else {
        return
    }
    /// preorder traversal
    traversals.append(node.val) 
    preorder(node.left)
    preorder(node.right)
    /// middle order traversal
    preorder(node.left)
    traversals.append(node.val) 
    preorder(node.right)
    /// the sequence is traversed
    preorder(node.left)
    preorder(node.right)
    traversals.append(node.val) 
}
Copy the code

The iteration

The iterative step of binary tree is to add nodes to a stack, and then access all matching nodes in traversal order by accessing the stack head/stack tail.

The former sequence traversal
func preorderIteration(_ root: TreeNode?). {
    var st:[TreeNode? ]= [root]
    while !st.isEmpty {
        let node = st.removeFirst()
        if node ! = nil {
            traversals.append(node!.val)
        } else {
            continue
        }
        st.insert(node?.right, at: 0)
        st.insert(node?.left, at: 0)}}Copy the code
In the sequence traversal
func inorderIteration(_ root: TreeNode?). {
    var st:[TreeNode? ]= []
    var cur:TreeNode? = root
    while cur ! = nil || !st.isEmpty {
        if cur ! = nil {
            st.insert(cur, at: 0)
            cur = cur?.left
        } else {
            cur = st.removeFirst()
            traversals.append(cur!.val)
            cur = cur?.right
        }
    }
}
Copy the code
After the sequence traversal

After the sequence traversal its traversal steps are left β†’ right β†’ middle, but this code is not simple to achieve. This allows us to access the middle, right, and left nodes in reverse, and then take the reversed result, which becomes left, right, and center.

func postorderIteration(_ root: TreeNode?). {
    var st:[TreeNode? ]= [root]
    while !st.isEmpty {
        let node = st.removeFirst()
        if node ! = nil {
            print(node!.val)
            traversals.append(node!.val)
        } else {
            continue
        }
        st.insert(node?.left, at: 0)
        st.insert(node?.right, at: 0)
    }
    traversals = traversals.reversed()
}
Copy the code

Color marking

Traditional iteration, known from the above code, is cumbersome and error-prone in the iteration process. Refer to color notation, a general and concise tree traversal method, using a method that is as efficient as stack iteration, as simple as recursion, and more importantly, can write exactly the same code for pre-order, mid-order, and post-order traversals.

The core method is as follows:

  • Marks the status of nodes, marked 1 for visited nodes and 0 for unvisited nodes

  • When an unvisited node is encountered, the node is marked 0 and then pushed in a specific order according to the three-order sorting requirement

    // Follow right β†’ left β†’ middle

    // Middle order left β†’ middle β†’ right Follow right β†’ middle β†’ left

    // Left β†’ right β†’ Middle Follow middle β†’ right β†’ left

  • Adds the value of the node marked 1 to the result array

    func tuple(_ root: TreeNode?).- > [Int] {
        var traversals = [Int] ()var statck = [(0, root)]
        while !statck.isEmpty {
            let (isVisted, node) = statck.removeLast()
            if node = = nil {
                continue
            }
            if isVisted = = 0 {
// /// preorder traversal
// statck.append((0, node? .right))
// statck.append((0, node? .left))
// statck.append((1, node))
// /// order traversal
// statck.append((0, node? .right))
// statck.append((1, node))
// statck.append((0, node? .left))
                /// the sequence is traversed
                statck.append((1, node))
                statck.append((0, node?.right))
                statck.append((0, node?.left))
            } else {
                traversals.append(node!.val)
            }
        }
        return traversals
    }
Copy the code

Using color notation, you can easily understand the iterative method and write the template code.

Morris ergodic

As a morris traversal with both performance and low spatial complexity, it can be discussed offline.

Good blog

Finishing edit: I am xiong big, dongpo elbow son

Lebron, Lebron, Lebron, Lebron, Lebron

Alamofire is written by the original author of AFNetWorking. Compared with AFN, the Swift version is more perfect. This paper introduces the basic usage of Alamofire, which is very comprehensive and suitable for intensive reading. The author also has an advanced usage that is recommended reading.

Kingfisher source code analysis — from the nuggets: Li Kun

@xiongda: SDWebImage in Kingfisher’s OC is written by the famous Wang Wei. This article is a summary of Kingfisher’s source code analysis series and recommended reading.

3. IOS SnapKit architecture: Rimson

@I am Xiongda: SnapKit plays the role of page layout in Swift, which is equivalent to that of NAVIGATION in OC. It is almost the same when used. The author of this paper has sorted out the process and principle of SnapKit layout in detail.

4, the use of third-party Charts detailed explanation – from hangge: Hangge

Charts is a powerful charting framework written using Swift. It’s a port of Android’s famous chart library, MPAndroidChart, to the Apple ecosystem. Author Hangge makes a similar case for the use of Charts with a lot of sample code.

5. Access UIKit components within SwiftUI — from Swift Garden: Cat Cups

Aside from SwiftUI’s incomplete tools, SwiftUI is really exciting for developers because of its ease of building UI. There is some comfort in the fact that many SwiftUI components are actually built on UIKit. This article will take you through an amazing SwiftUI library called Introspect. With it, developers can access the UIKit view underlying the SwiftUI component.

Learning materials

Mimosa

Bang Bang Bin Β· Technology Reference Weekly

Address: www.yuque.com/binboy/incr…

This is a weekly technical reference magazine combed and summarized on Wordfinch by BinboyπŸ‘». This weekly is the precipitation and reflection of the author’s study and life, which has both breadth, depth and attitude. Like the headline of its launch: “Maintain links with the technology of the world,” you can see the author in the weekly learning technology and experience, also can see some of the new technology and life, may be you are looking at here, or is never heard of technical information, this information is not only the author and his “the second brain” link, also is the author and the reader communication medium, At the same time, it pushes the author and readers forward together. Here’s a quote from the first issue:

There is nothing wrong with pursuing depth in technology, but there is no need to favor one over the other. Personally, I tend to “learn energy storage first and then deep”. The open source of the technology world and the opening of the Internet have provided a good soil for the knowledgeable. We can understand the status quo of technologies and tools, fully apply them to solve common problems in the real world, and constantly improve them in the process. When it comes to real boundaries, it’s not too late to combine existing technologies and tools to create truly new technologies and tools.

No matter how far you can look in one direction, you cannot see the new one

β€”β€”β€”β€” Study energy storage, bo first and then deep

Tools recommended

CoderStar

Mounty

Address: mounty. App /

Software status: Free, open source

Software Introduction:

Mounty is a small tool that remounts write protected NTFS volumes in read/write mode on macOS. The function is similar to NTFS For Mac, but the biggest and most important difference is that it is free.

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 33rd issue

IOS Fishing Weekly Thirty-second issue

IOS Fishing Weekly thirty-first issue

IOS Fishing Weekly 30th issue