2016-01-06 13 views
5

Test1() 'de, test() ile döndürülen Void'i başarılı bir şekilde döndürebilir. Fakat test2() 'de, hata atar. Niye ya?Neden Void'i doğrudan bir işlevde döndürüyorum?

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

cevap

7

Void bir türdür, bu nedenle döndürülemez. Bunun yerine boş bir tuple olan Void'in temsilini geri vermek istiyorsunuz.

nedenle, onun yerine bu deneyin ve bu derlenir:

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

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

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

test1() 

boş tupple, Void türü dönmek aşağıdaki bağlantıda boşluğa aramak için beklediği bir işlev iade edilebilir nedeni hakkında daha fazla bilgi için : https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html

1

Test1'de() siz dönmeyin, geri dönüşünüz void kendisini döndüren işlev testidir();

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. 
} 

Umarım bu yardımcı olur!

İlgili konular