2014-09-28 113 views
2

協議的方法,當所需的協議的方法還沒有被實施的Xcode只是給予警告更好的解決方案來處理要求沒有實現

Warning: method 'xxx' in protocol 'xxx' not implemented

我有一個自定義視圖就像UITableView誰擁有一個dataSource屬性。爲了確保數據源是不爲零,是該方法迴應我這樣做

NSAssert(self.dataSource != nil, @"menu's dataSource shouldn't be nil"); 
if ([self.dataSource respondsToSelector:@selector(menu:numberOfRowsInColumn:)]) { 
    return [self.dataSource menu:self 
      numberOfRowsInColumn:self.currentSelectedMenudIndex]; 
} else { 
    NSAssert(0 == 1, @"required method of dataSource protocol should be implemented"); 
    return 0; 
} 

我不知道是否有處理所需的方法的缺失更優雅的方式?

任何想法?

回答

2

該解決方案對我來說很不錯,因爲可以禁用生產代碼中的斷言,使其成爲僅限開發人員的完整性檢查。

如果該協議可以通過第三方代碼(即某種插件)來實現,則應該提出異常。

但是一個稍微容易斷言代碼和理解是簡單的:

NSAssert(NO, @"blah blah blah"); 
+0

哈哈,好一個〜 – dopcn 2014-10-29 06:07:16