输出控制台中的 () 是什么意思?

What is the meaning of () in output console?

问题陈述

我是 Swift 的新手,似乎无法理解 () 在我的 class.

的每次函数调用后不断出现的意思

代码

import UIKit


class sample{
        //Member variables
        var number = 34
        var decimal = 3.5
        var subject = ""

        //Member method

        func printSentence(){
            number += 1
            print("The value of number is \(number)")
            print("Subject name: \(subject)")
        }

}

var o: sample = sample()

print(o.subject)
print(o.printSentence())
o.subject = "Swift Programming"

print(o.subject)
print(o.printSentence())

我确实有面向对象的背景,但我无法理解 () 显示在 运行 我的程序的 Live Viewer 和控制台中的含义。 (以下截图)

来自这一行:

print(o.printSentence())

o.printSentence()是一个函数,returns没什么,a.k.a。 ()Void.

您可能打算调用函数,而不是打印函数:

o.printSentence() /// executes all the code inside `printSentence()`

如果您想使用 print 方法,请更改您的函数,使其 returns 成为一个字符串值。然后打印字符串值:)

例如

class sample {
    var number = 34
    var decimal = 3.5
    var subject = ""

func printSentence(subject: String) -> (String){
    self.number+=1
    return ("The value of number is \(number)" + " and my subject name is \(subject)")
        
    }
}

var o: sample = sample()
print(o.number)


print(o.printSentence(subject: "Swift"))
print(o.number)

输出:

34

数字的值为35,我的主题名称是Swift

35