Substring variable assignment in Swift

After exploring stackoverflow about underscores in Swift, I understand that underscores mean: a) ignore this function and b) you can omit the parameter name when using the method. I don’t understand what will happen if we assign the variable an underline? By assigning an underscore to a variable, this causes the compiler warning from Xcode to turn off, which says: "the initialization result is not used," but it does not display a warning if you do not use this variable in any way.

Is this variable still created and stored in memory, or is it completely ignored by the compiler, as if it were a line of code that was commented out?

For instance:

func test_ToDoItem_TakesTitle(){

        let firstToDoItem = ToDoItem(title: "First Instance Title")

        _ = ToDoItem(title: "First ToDoItem instance")

        XCTAssertEqual(firstToDoItem.title, "First Instance Title")

    }

:

 _ = ToDoItem(title: "First ToDoItem instance")

, , , Xcode.

+4
3

, , :

class ToDoItem {
    var title = ""

    init(title: String) {
        self.title = title
    }

    deinit {
        print("deinit \(title)")
    }
}

func test() {
    print("test")

    _ = ToDoItem(title: "First")
    _ = ToDoItem(title: "Second")

    print("end test")
}

func callTest() {
    print("calling test()...")
    test()
    print("back from test()")
}

callTest()

:

calling test()...
test
end test
deinit Second
deinit First
back from test()

:

func test() {
    print("test")

    _ = ToDoItem(title: "Item 1")
    for i in 2...4 {
        _ = ToDoItem(title: "Item \(i)")
    }
    _ = ToDoItem(title: "Item 5")

    print("end test")
}
calling test()...
test
deinit Item 2
deinit Item 3
deinit Item 4
end test
deinit Item 5
deinit Item 1
back from test()

, 2, 3 4 , . 1 5 , , test().


, Swift Playground. :

calling test()...
test
deinit Item 1
deinit Item 2
deinit Item 3
deinit Item 4
deinit Item 5
end test
back from test()

:

  • .
  • .
+1

, , ?

ToDoItem.

. , , ToDoItem -, ? - ToDoItem, ToDoItem(...) . , .

CoreData. , . :

_ = MyEntity(entity: ..., insertInto: ...)

- .

ToDoItem self - , , ToDoItem . ,

SomeClass.someStaticProperty = self

, ToDoItem

+4

As vacawama noted in his answer, an object is created and then destroyed when the object goes beyond.

BUT , this is only true for code executed on the playground. When you run the application on the Simulator or on the device, the object is immediately destroyed. Caution!

I used the following code from vacawamas answer for test:

class ToDoItem {
    var title = ""

    init(title: String) {
        self.title = title
    }

    deinit {
        print("deinit \(title)")
    }
}

func test() {
    print("test")

    _ = ToDoItem(title: "First")
    _ = ToDoItem(title: "Second")

    print("end test")
}

func callTest() {
    print("calling test()...")
    test()
    print("back from test()")
}

callTest()

(Sorry, I can not comment on another answer, although I, although it was important to share)

+2
source

Source: https://habr.com/ru/post/1665442/


All Articles