2017-11-11 18 views
-1

신속하게 올바르게 - (void)를 사용하는 방법에 대한 약간의 오해가 있습니다. - (Void)를 사용하여 코드를 작성하려고했지만 "Expected declaration"또는 때때로 "Binary operator '-' cannot be applied to operands of type 'UIFont' and '(Void).Type'"오류가 발생합니다.Swift에서 "Expected declaration"오류를 수정하는 방법?

왜 이런 일이 발생합니까? 신속하게이 기능을 올바르게 사용하는 올바른 방법은 무엇입니까?

{

- (Void)keyboardWillShow { //>> showing me the error "Binary operator '-' cannot be applied to operands of type 'UIFont' and '(Void).Type'" 
     // Animate the current view out of the way 
     if (self.view.frame.origin.y >= 0) 
     { 
      [self setViewMovedUp:YES]; 
     } 
     else if (self.view.frame.origin.y < 0) 
     { 
      [self setViewMovedUp:NO]; 
     } 
    } 

}

감사

당신이 돌아올 수없는 값을 가진 함수를 선언하려는 것처럼 소리
+1

는 스위프트 프로그래밍 언어 [애플의 문서를 (https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/index.html)를 통해 읽어 보시기 바랍니다. –

+1

문제를 일으키는 실제 코드로 질문을 업데이트하십시오. – rmaddy

+1

그것은 Swift 코드가 아닌 Objective-C 코드입니다. [Swift Programming Language] (https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/) 책을 읽어보십시오. – rmaddy

답변

5

. 당신은 코드를 아래와 같이 전무 값을 반환 할 수 있습니다

func thisFunction() { 
    .. 
} 
-2

: 선언되지 않은 경우

func myFunc(myVar: String) ->()? { 
    print(myVar) 
    return nil 
    } 
var c = myFunc("Hello") 
print(c) // nil 

하지만 스위프트 기능은 항상 기본 nil 값을 반환로 스위프트이 수행됩니다. 코드를 아래와 같이 :

func myFunc(myVar: String) { 
     print(myVar) 
     return 
     } 
    var c = myFunc("Hello") 
    print(c) //() 
+0

* Swift 함수는 선언되지 않은 경우 항상 * 기본값을 반환합니다. * 잘못되었습니다. 두 번째 예제에서는 컴파일러 오류 (Swift 3+에서는 실제로 두 개)가 발생합니다. – vadian

+1

* "Swift 함수는 선언되지 않은 경우 항상 기본값 nil을 반환합니다."- 아니요, 이것은 사실이 아닙니다. – rmaddy

+0

엄밀히 말하면,이 버전의 myFunc (myVar :) 함수는 반환 값이 정의되어 있지 않더라도 여전히 값을 반환합니다. 정의 된 반환 유형이없는 함수는 Void 유형의 특수 값을 반환합니다. 이것은 단순히 빈 튜플이며,()로 작성됩니다. –