Files
keyboard/keyBoard/Class/AiTalk/类名冲突解决说明.md
2026-01-23 21:51:37 +08:00

3.6 KiB
Raw Blame History

类名冲突解决说明

🐛 问题

在实现新的聊天 UI 时,创建了 KBChatMessage 类,但与 CustomKeyboard 目录下已有的 KBChatMessage 类产生了命名冲突,导致 KeyboardViewController 编译报错:

Property 'audioFilePath' not found on object of type 'KBChatMessage *'

🔍 原因分析

项目中存在两个同名的 KBChatMessage 类:

1. CustomKeyboard/Model/KBChatMessage旧的

@interface KBChatMessage : NSObject
@property (nonatomic, copy) NSString *text;
@property (nonatomic, assign) BOOL outgoing;
@property (nonatomic, copy, nullable) NSString *audioFilePath;  // ← 旧的属性
@property (nonatomic, copy, nullable) NSString *avatarURL;
@property (nonatomic, copy, nullable) NSString *displayName;
@property (nonatomic, strong, nullable) UIImage *avatarImage;
@end

用途: 键盘扩展CustomKeyboard中的聊天消息模型


2. keyBoard/Class/AiTalk/M/KBChatMessage新的

@interface KBChatMessage : NSObject
@property (nonatomic, assign) KBChatMessageType type;
@property (nonatomic, copy) NSString *text;
@property (nonatomic, strong) NSDate *timestamp;
@property (nonatomic, assign) NSTimeInterval audioDuration;
@property (nonatomic, strong, nullable) NSData *audioData;      // ← 新的属性
@property (nonatomic, copy, nullable) NSString *audioId;        // ← 新的属性
@property (nonatomic, assign) BOOL isComplete;
@end

用途: AI 聊天主界面KBAiMainVC中的消息模型


解决方案

将新创建的类重命名为 KBAiChatMessage,避免与旧类冲突。


📝 修改清单

1. 重命名类文件

  • KBChatMessage.hKBAiChatMessage.h
  • KBChatMessage.mKBAiChatMessage.m

2. 重命名类名

// 原来
@interface KBChatMessage : NSObject

// 修改为
@interface KBAiChatMessage : NSObject

3. 重命名枚举

// 原来
typedef NS_ENUM(NSInteger, KBChatMessageType) {
    KBChatMessageTypeUser,
    KBChatMessageTypeAssistant,
    KBChatMessageTypeTime
};

// 修改为
typedef NS_ENUM(NSInteger, KBAiChatMessageType) {
    KBAiChatMessageTypeUser,
    KBAiChatMessageTypeAssistant,
    KBAiChatMessageTypeTime
};

4. 更新所有引用

Cell 文件

  • KBChatUserMessageCell.h/m
  • KBChatAssistantMessageCell.h/m
  • KBChatTimeCell.h/m

视图文件

  • KBChatTableView.h/m

所有方法参数和属性

// 原来
- (void)configureWithMessage:(KBChatMessage *)message;
@property (nonatomic, strong) NSMutableArray<KBChatMessage *> *messages;

// 修改为
- (void)configureWithMessage:(KBAiChatMessage *)message;
@property (nonatomic, strong) NSMutableArray<KBAiChatMessage *> *messages;

📊 最终结构

CustomKeyboard键盘扩展

CustomKeyboard/Model/
├── KBChatMessage.h          ← 旧的,保持不变
└── KBChatMessage.m

用途: 键盘扩展中的聊天功能


AiTalkAI 聊天主界面)

keyBoard/Class/AiTalk/M/
├── KBAiChatMessage.h        ← 新的,已重命名
└── KBAiChatMessage.m

用途: AI 聊天主界面的消息模型


验证

编译项目,确认:

  • KeyboardViewController 不再报错
  • KBAiMainVC 正常工作
  • 两个模块互不干扰

🎯 总结

通过将新类重命名为 KBAiChatMessage,成功解决了类名冲突问题。现在:

  • CustomKeyboard 使用 KBChatMessage(旧的)
  • AiTalk 使用 KBAiChatMessage(新的)

两者各司其职,互不干扰!