27

Tôi vừa mới bắt đầu một dự án mới chạy Xcode 4.2.1 và iOS5 SDK. Dự án được thiết lập với ARC. Tôi đang cố gắng để thiết lập appdelegate là đại biểu cho UITabBarController bằng cách làm [tabBarController setDelegate:self]; nếu tôi làm điều đó tôi nhận được một thông điệp cảnh báo nói rằng:Cocoa-Touch - Ủy quyền sự nhầm lẫn

warning: Semantic Issue: Sending 'AppDelegate *const __strong' to parameter of incompatible type 'id<UITabBarControllerDelegate>' 

rồi đủ công bằng, tôi đặt appdelegate tôi để phù hợp với các UITabBarControllerDelegate bằng cách thực hiện

@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate> 

Tuyệt, cảnh báo biến mất.

Tôi hiện đã gặp lỗi khác. Trong một bộ điều khiển xem tôi muốn có được một tổ chức của các appdelegate vì vậy tôi làm điều này: AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; nhưng điều này sẽ làm cho một cảnh báo nói:

warning: Semantic Issue: Initializing 'AppDelegate *__strong' with an expression of incompatible type 'id<UIApplicationDelegate>' 

Nhưng nếu tôi loại bỏ appdelegate của tôi phù hợp với các giao thức UITabControllerDelegate biến mất cảnh báo thứ hai của tôi .

Hành vi rất lạ, điều gì mang lại cho các chuyên gia về ca cao?

Trả lời

81

Hãy thử tạo kiểu chữ trước khi gán biến AppDelegate.

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 

Và giữ UITabControllerDelegate.

+1

Tất nhiên! Cảm ơn ... –

+2

Xin chào @Aadhira, Điều này đã giải quyết được lỗi của tôi, nhưng tôi không chắc rằng tôi hiểu lý do giải pháp hoạt động. Bạn hoặc ai đó có thể giải thích cho tôi không? Cảm ơn bạn! – narner

+0

http://stackoverflow.com/questions/14977284/in-objective-c-why-is-casting-needed-when-assigning-to-a-variable-whose-type-is Vui lòng tham khảo liên kết ở trên - @narner – Arundev