Cell 中按鈕點擊事件打印 indexPath 時,NSLog(@\"@%\", indexPath) 格式字符串錯誤(@% 應為 %@),導致輸出異常。
- (IBAction)addItemIntoOrder:(id)sender {
NSLog(@"@%", indexPath); // ❌ 格式錯誤
NSLog(@"@%", indexPath.row); // ❌
}
- (IBAction)addItemIntoOrder:(id)sender {
NSLog(@"%@", indexPath); // ✅ %@ 輸出對象描述
NSLog(@"%ld", (long)indexPath.row); // ✅ %ld 輸出長整型
}
| 佔位符 | 類型 | 示例 |
|---|---|---|
%@ | 對象 | NSLog(@\"%@\", obj) |
%d/%i | int | NSLog(@\"%d\", 42) |
%ld | long / NSInteger | NSLog(@\"%ld\", (long)idx) |
%f | float/double | NSLog(@\"%f\", 3.14) |
%p | 指針地址 | NSLog(@\"%p\", ptr) |
- (UITableViewCell *)tableView:... cellForRowAtIndexPath:... {
Cell *cell = ...;
cell.indexPath = indexPath; // 屬性傳遞
return cell;
}
// Cell.m
- (IBAction)btnTapped:(id)sender {
NSLog(@"Row: %ld", (long)self.indexPath.row);
}
格式字符串正確,調試輸出才可信。