看板 MacDev 關於我們 聯絡資訊
各位版上先進大家好: 我目前正在學習Objective-C和Cocoa Frameworks 對於private methods的宣告和實作 在網路上透過Google查詢找到的方式皆為使用category來實現 但是我自己撰寫測試程式的時候雖然build會出現警告訊息 "MYClass may not respond to '-myPrivateMethod' 但是我在該private method內寫的NSLog還是被印出來了。 我目前對category的了解,它的定義是: 1. adding methods to an exisiting class without creating a subclass 2. used to extend my own classes or framework classes (ex: NSString) 3. adding methods to a class in a modular fashion. 這是目前我對categories的理解,我就在想.... 是不是實際上Objective-C並沒有所謂的private method? 還是我的程式本身有問題..? 如果我的觀念上有什麼錯誤,懇請大家不吝指教,非常感謝各位。 by 一個困惑的初學者 程式執行結果: 2010-03-10 21:04:36.140 PrivateMethods[5735:a0f] class method msg 2010-03-10 21:04:36.142 PrivateMethods[5735:a0f] private msg 我的測試範例如下: 共四個檔案 (main.m , MYClass.h , MYClass.m , MYClass+PrivateMethods.h) // main.m #import <Foundation/Foundation.h> #import "MYClass.h" int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; MYClass *m = [[MYClass alloc] init]; [m classMethod]; [m myPrivateMethod]; [m release]; [pool drain]; return 0; } //MYClass+PrivateMethods #import "MYClass.h" @interface MYClass (PrivateMethods) - (void)myPrivateMethod; @end //MYClass.h @interface MYClass : NSObject { } - (void)classMethod; @end //MYClass.m #import "MYClass.h" #import "MYClass+PrivateMethods.h" @implementation MYClass (PrivateMethods) - (void)myPrivateMethod { NSLog(@"private method msg"); } @end @implementation MYClass - (void)classMethod { NSLog(@"class method msg"); } @end -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 123.204.114.122 ※ 編輯: copyshaft 來自: 123.204.114.122 (03/10 21:20)
Blueshiva:1. ObjC 的確沒有 private method 03/10 21:16
Blueshiva:2. NSLog 不管寫在哪,只要有執行到都會印東西出來,就 03/10 21:17
Blueshiva:算在C++裡面也一樣 03/10 21:17
copyshaft:非常感謝你的回應。我瞭解了。 03/10 22:13
copyshaft:我另外Google到一個說明網頁: 03/10 22:14
copyshaft:http://bit.ly/99zjSS 跟大家分享,在最後一段有說明 03/10 22:15