This profile

  • Tips: Count vs. isEmpty
  • Struct and class in Swift, value type and reference type.
  • Good blog: This installment continues with several excellent Swift open source libraries.
  • Learning materials: Daming’s Swift booklet.
  • Development tools: PhotoSweeper, a fast and powerful repeat photo cleaner.

The development of Tips

zhangferry

count vs isEmpty

There are usually two ways to determine whether a string or array isEmpty: count == 0 or isEmpty. We might assume that they are the same, that the internal implementation of isEmpty is count == 0. However, SwiftLint will force us to use isEmpty for null detection. From this can be judged that there must be differences between the two, today to analyze the difference between the two.

Count and isEmpty come from Collection. Count represents a quantity. There’s nothing special about this. In the standard library, it is defined like this:

extension Collection {
    var isEmpty: Bool { startIndex = = endIndex }
}
Copy the code

If the first index and the last index of the set are equal, then the set is null. There is only one comparison and the complexity is O(1).

Most collection types follow the default implementation, but there are certain collection types that override this method, such as Set:

extension Set {
    var isEmpty: Bool { count = = 0}}Copy the code

So why are there two different cases? Let’s look at the description of count in the Collection.

Complexity: O(1) if the collection conforms to RandomAccessCollection; otherwise, O(n), where n is the length of the collection.

So for types that follow the RandomAccessCollection protocol, the count fetch is O(1), like Array; For unfollowed types, such as String, the count complexity is O(n), but isEmpty remains O(1).

The Set is a little more special because it does not implement RandomAccessCollection but still uses count to determine whether it is empty. This is because the Set implementation is different, and count is O(1).

Of course, to simplify memory, we can always use isEmpty for nullation.

Because there are multiple protocols and specific types involved, here is a diagram showing the relationship between these protocols and types.

photo

Parsing the interview

Edit: Xiao Haiteng of Normal University

Struct and class in Swift, value type and reference type

struct & class

In Swift, there are not many core differences between classes and structs, many of which are inherent in the difference between value types and reference types.

  • Classes can be inherited, but structs cannot. (Of course structs can use protocol to achieve similar effects.) ; The differences affected by this are:

    • structThe distribution methods of the method are all direct distribution, andclassAccording to the actual situation, there are various distribution modes. Please refer toCoderStar | Swift distribution mechanism;
  • Class needs to define its own constructor, struct generated by default;

  • Class is a reference type, struct is a value type; The differences affected by this are:

    • structChanging its properties is affected by the let modifier and cannot be changed,classBe unaffected by;
    • structMethod that needs to modify its own properties (noinitMethod), the method requires a prefix modifiermutating;
    • structBecause of the value type, it is automatically thread safe and there is no risk of memory leaks from circular references;
    • .
  • .

Value type & reference type

  • Storage mode and location: Most value types are stored on the stack and most reference types are stored on the heap;
  • Memory: Value types do not have reference counts and do not have problems with circular references and memory leaks;
  • Thread-safe: Value types are naturally thread-safe, while reference types need to be locked by developers.
  • Copy mode: The value type copies the content, and the reference type copies the pointer. In a sense, it is called deep copy and shallow copy

You can see the details in CoderStar | looking at value types and reference types in Swift from SIL’s perspective.

Good blog

I am Xiong Da, Dongpo Elbow son

1, the third-party date processing library SwiftDate usage details — from the air song: hangge

SwiftDate is the authoritative toolchain for manipulating and displaying dates and time zones on all Apple platforms, even Linux and Swift server-side frameworks such as Vapor or Kitura. There are over 3 million downloads on CocoaPods. SwiftDate features both simple date operations and complex business logic. This article will explain how to use SwiftDate in detail.

Vapor Exploration of doing things — from Nuggets: PJHubs

Vapor is an open source Web framework based on Swift language, which can be used to create RESTful apis, websites and real-time applications using WebSockets. Vapor also provides ORM, a template language, and user authentication and authorization modules in addition to the core framework. This paper mainly recorded the problems, reflections and summaries of the first operation of Vapor.

Publish create blog – Swift notepad from Elbow: Dongpo elbow

Publish is a static website generator for Swift developers. It uses Swift to build the entire site and supports themes, plug-ins, and a host of other customization options. This article series covers the basics of Publish, theme customization, and plug-in development in three separate sections.

Using Realm and Charts with Swift 3 in iOS 10 — from: Sami Korpela

@xiongda: a very powerful and Swift pure chart-related open source framework – Charts. The author of this article builds a Demo using Realm and Charts, a lightweight database in Swift. The Demo is long and suitable for beginners, but there is one drawback: it is based on Swift 3. In addition, I wrote an earlier article about the practical code for Realm: how to reduce crashes in the Realm database.

5. Today we are going to talk about WebSocket (iOS/Golang) — from Nuggets: Dance 647

The author of this article uses GO and Starscream to simulate the interaction between client and server websocket. It is recommended for those who are interested in sockets.

6, Hero Usage Guide — from: Hero official

Hero is the best transition animation I have ever used, not one of them. The score of 20K + star also shows its status in transition animation; Hero should do most of the job. Here’s the official manual.

Learning materials

Mimosa

Daming’s Swift booklet

Address: github.com/ming1016/Sw…

Swift booklet written by Mr. Dai, developed according to declarative UI and responsive programming paradigm. You can find the Syntax guide for Swift, guidelines for using the Combine, Concurrency, and SwiftUI libraries, as well as tracking some well-known repositories, developers’ Github updates, and our repository Issues. Since it is open source, you can also debug your own learning or contribute to the project.

2021 Black Five Special

Address: github.com/mRs-/Black-…

This warehouse collected this year’s black Five discount development class, efficiency class, tool class software, a lot of well-known unknown software are at a discount, there is a need to rush!

Tools recommended

CoderStar

PhotoSweeper

Address: overmacs.com/

Software status: $9.99, trial available

Software Introduction:

PhotoSweeper is a fast and powerful duplicate photo cleaner designed to help you find and remove duplicate and similar photos on your Mac.

We can consider using it in scenarios where similar image resources are scanned while slimming the application.

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 34th issue

IOS Fishing Weekly 33rd issue

IOS Fishing Weekly Thirty-second issue

IOS Fishing Weekly thirty-first issue