2016-01-06 28 views
5

In test1(), può restituire Void restituito da test() correttamente. Ma in test2(), l'errore si verifica. Perché?Perché non posso restituire Void direttamente in una funzione

//: 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 
} 

risposta

7

Void è un tipo, quindi non può essere restituito. Invece si desidera restituire la rappresentazione di Void, che è una tupla vuota.

Pertanto, provate questo invece, e questo compilerà:

func test()->Void{ 
    print("Hello") 
} 

func test1(){//print Hello 
    return test() 
} 

func test2()->Void{// throw error 
    return() 
} 

test1() 

Per ulteriori informazioni sul motivo per cui un tupple vuoto può essere restituito in una funzione che si aspetta di tornare tipo Void, cercare vuoto nel seguente link : https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html

1

In test1() non si restituisce Void, il proprio ritorno è la funzione test() che restituisce lo stesso void;

Spero che questo possa essere d'aiuto!