文森說技術

iOS, Web Development Notes
- , ,

在 iOS 中寫 protocol 的時候要加上 NSObject protocol

今天在寫 delegate (@protocol) 要檢查 delegate method 有沒有被實作,結果 Xcode 抱怨:

1
No know instance method for selector 'respondsToSelector:'

respondsToSelector: 這個 method 是未知的。

讓自訂 Protocol 遵循 NSObject Protocol

因為之前在寫 protocol 的時候都太順了。後來發現是在 @protocol 宣告的地方:

1
2
3
4
5
@protocol VCTabBarDelegate

// some methods

@end

沒有讓 protocol conform to NSObject protocol 。因此在宣告的時候加上 <NSObject> 就可以了:

1
2
3
4
5
@protocol VCTabBarDelegate <NSObject>

// some methods

@end

respondsToSelector: 是屬於 NSObject protocol ,所以要是要讓自己的 protocol 能夠使用這些檢查用的 methods ,就要加上去。

如果覺得這篇對你有幫助,歡迎幫忙分享給其他人 😀