preface

Learning is like rowing upstream; not to advance is to drop back. ‘!!!!!

Today I want to share with you a few advanced features of Swift. The iOS data | address

I. Swift exclusive access

Swift memory security check: Exclusive memory access limits occur when two variables access the same block of memory. Read/write permission conflicts occur:

  1. Inout parameter read/write conflict
  2. Struct function modifies member attribute read/write conflict
  3. Value type attribute read/write conflict

1. Read/write conflicts of inout parameters

Var inputStr = "input" func plusSlef1(_ param: Inout String) {// Swift4 has an exception: access 0x103ED30A0 at the same time, but change requires exclusive access. Param += inputStr} // plusSlef1(&inputstr) // Access the same memory address at the same time, resulting in read/write collisions func plusSlef2(_ param1: Inout String, _ param2: inout String) {// In the >= Swift4 version, exceptions are thrown: overlaps access to 'inputStr', but changes require exclusive access; Let result = param1 + param2 print(result)} // plusSlef2(&inputstr, &inputstr)Copy the code

2. There is an read/write conflict in the structure when a function modifs a member attribute

Struct StructA {var field: Mutating func edit(_ param: inout StructA) { field = param.field } } var structA = StructA(field: 100) // A compilation error occurs when calling the following code: // 1. Inout parameters do not allow each other to alias // 2. Overlapping access to 'structA', but modification requires exclusive access; Consider copying to a local variable // structa.edit (&structA)Copy the code

3. Read/write conflicts of value type attributes

Class ClassA {var tuple = (key1:1, key2:2) func test1(_ param1: Inout Int, _ param2: inout Int) {print(param1, param2)} func test2() {// Crash if called, access 0x600000667cd0, but change requires exclusive access. Test1 (&tupl.key1, &tupl.key2)} func test3() {var localTuple = (key1: 3, key2: Test1 (&localtupl.key1, &localtupl.key2)}} let cla = ClassA() // cla.test2() cla.test3() Print, 3, 4,Copy the code

Swift enhanced string

// Swift multi-line string, same as kotlin's original string, does not need to manually add a newline. Var text2 = "escape single quotation marks \'" print(text2) // Escape single quotation marks' // Var text3 = #" escape single quotes '"# print(text3) escape single quotes' Var text4 = #" newline 1 \n newline 2"# print(text4) // newline 1 \n newline 2 Var text5 = #" newline 1 \#n newline 2"# print(text5) // Newline 1 newline 2Copy the code

Swift dynamic member search

@dynamicMemberLookup // Swift uses @dynamicMemberLookup to add the ability to dynamically find members to a class Class Data {var field1: Int = 0 var field2: String = "" subscript(dynamicMember member: Int) -> String { return "class don't have the field: \(member), type int" } subscript(dynamicMember member: String) -> String { return "class don't have the field: \(member), type String"} // Call func dynamicallyCall(withArguments argArray: [Int]) { print("invoke unknown func with args: \(argArray)")} // Pass in the key pair func dynamicallyCall(withKeywordArguments: KeyValuePairs<String, Int>) { let argPairs = pairs.map{ key, value in return "\(key): \(value)"} print(argPairs)}} let data = data () // // class don't have the field: someInt, type String. Class don't have the field: SomeString, type String print(data.someint, data.someString) // Invoke Unknown func with args: [1, 2, 3] // Invoke unknown func with args: [1, 2, 3] 2) // ["key1: 1", "key2: 2"]Copy the code

Swift Enhanced Protocol

Protocol ProtocolA {var field: String {get set}} protocol ProtocolB {func method()} class ClassImpl: ProtocolA, ProtocolB {var field: String = "impl field" func method() {print("impl method")}} ProtocolA & ProtocolB) { print(impl.field) impl.method() } testImpl(impl: ClassImpl())Copy the code

summary

Short step without thousands of miles, not small streams into rivers and seas. The process of learning is always painful, the result of learning is always happy. I hope we’ll see you at the top, so like it and follow it.

Recommend materials | to collect on its own

Recommended reading: Preliminary to Swift Concurrency