簡體   English   中英

是否進行類型轉換

[英]Typecast or not

我有一個名為MyCustomCellUITableViewCell子類。 UITableView委托的didSelectRowAtIndexPath:方法中,我有以下內容:

MyCustomCell *cell = [tableView cellForRowAtIndexPath:indexPath];

該行提示來自Xcode的警告:使用“ UITableViewCell *”類型的表達式初始化“ MyCustomCell *”的不兼容類型

如果將其更改為以下行,則警告消失。 但是,無論哪一條線都適合我。

MyCustomCell *cell = (MyCustomCell *)[tableView cellForRowAtIndexPath:indexPath];

我應該完全采用打字嗎?

編輯更多信息:

cellForRowAtIndexPath:方法中,我使用MyCustomCell創建了這些單元格。 日志告訴我該單元確實來自MyCustomCell

NSLog(@"cellForRowAtIndexPath cell:%@", cell);

並且登錄didSelectRowAtIndexPath:還確認UITableView返回了MyCustomCell

NSLog(@"didSelectRowAtIndexPath cell: %@", [tableView cellForRowAtIndexPath:indexPath]);

對我而言,警告只是因為Xcode無法提前告知單元是如何創建的。 由於單元格從[tableView cellForRowAtIndexPath:indexPath]返回,已經是MyCustomCell ,因此它們是兼容的。

是的,你應該投。 由於cellForRowAtIndexPath:返回不是MyCustomCell *UITableViewCell * ,因此您在分配中有兩種不兼容的類型,這是非法的(至少就C標准而言)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM