在 iOS 專案中,如果同一個自定義型別(typedef)在多個檔案裡被重複定義,編譯時就可能出現「typedef redefinition with different types」的錯誤。本文記錄這個問題的成因與解法。
假設有兩個檔案都定義了同名的 ConnectionType 列舉。第一個檔案:
#import <Foundation/Foundation.h>
#import "Blog.h"
// Types
typedef enum {
ConnectionTypeAsynchronously,
ConnectionTypeSynchronously
} ConnectionType;
@class Parser;
@protocol ParserDelegate <NSObject>
end
第二個檔案:
#import <Foundation/Foundation.h>
#import "ASIHTTPRequest.h"
typedef enum {
DownloaderTypeUnknown,
DownloaderTypeData,
DownloaderTypeImage,
DownloaderTypeAudio,
DownloaderTypeVideo,
DownloaderTypeFile
} DownloaderType;
// Types
typedef enum {
ConnectionTypeAsynchronously,
ConnectionTypeSynchronously
} ConnectionType;
@class LRDHttpRequest;
@protocol LRDHttpRequestDelegate <NSObject>
end
錯誤訊息如下:

error: typedef redefinition with different types ('ConnectionType' vs 'ConnectionType')
問題的核心是「衝突、重複」:同一個 ConnectionType 型別被定義了兩次。最簡單的解法就是刪除其中一處重複的定義即可。
更根本的做法,是把這些共用型別抽到一个獨立的標頭檔中集中定義,再透過 #import 或 #ifndef guards 避免重複引入,這樣就能從源頭杜絕這類 typedef 重複定義的錯誤。