Why Cant I return Void directly to the function

In test1 (), it can return the Void that test () returned successfully. But in test2 () the error is triggered. Why?

//: Playground - noun: a place where people can play import UIKit import AVFoundation func test()->Void{ print("Hello") } func test1(){//print Hello return test() } func test2(){// throw error return Void } 
+5
source share
2 answers

Void is a type, so it cannot be returned. Instead, you want to return the Void view, which is an empty tuple.

So try this instead, and this will compile:

 func test()->Void{ print("Hello") } func test1(){//print Hello return test() } func test2()->Void{// throw error return () } test1() 

For more information on why an empty string can be returned in a function that expects a Void type to return, search for emptiness at the following link: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/ Swift_Programming_Language / Functions.html

+7
source

In test1 (), you do not return Void, your return is the test () function, which returns void itself;

 void function test(){ print("Hello"); } void function test1(){ //print Hello return test(); } /* you can not return a type func test2(){// throw error return Void; } */ void function test2(){ //code or not return test(); //calling test function returns void. } 

Hope this helps!

+1
source

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


All Articles