2016-12-24 42 views
0

如我們所知,Swift語言允許函數重載,在Objective-c中重載是不合法的。Swift Overloading問題

所以我測試在我的演示項目:

import UIKit 

class FuncOverLoad: NSObject { 

func helloFuc (_ what:String) { 

} 
func helloFuc (_ what:Int) { 

} 


func helloFuc() -> String { 

    return "one" 

} 
func helloFuc() -> Int { 

    return 1 

} 
} 

但是,爲什麼總會有錯誤:

Method ‘helloFuc’ with Objective-C selector ‘helloFunc:’ conflicts with previous declaration with the same Objective-C selector

我拍照:

enter image description here

爲什麼來這個問題,如果Swift函數重載是合法的?

+1

這是這個問題的重複。一個更好的將是http://stackoverflow.com/questions/29457720/compiler-error-method-with-objective-c-selector-conflicts-with-previous-declara?s=1|6.5289 – rmaddy

回答

0

在你的情況下,刪除: NSObject將清除錯誤。

+0

如果子類的' NSObject',如果與'Objective-C'有關? – aircraft

+0

如果這樣,Swift的Overloading特性在iOS開發中的實際意義不大,因爲'vc'或'controls'等等。 – aircraft

+0

子類化'NSObject'意味着'@ objc',所以你被Objective C綁定的規則 – Alexander