[文章原地址]
http://funwithobjc.tumblr.com/
我的翻譯,不免會有錯誤,請各位看官海涵,翻譯在末尾
本人將在文章的部分地方添加註釋,並根據需求增減文章內容,在此對原做者辛勤勞做表示感謝 html
This is something any experienced Cocoa developer has been asked at least half a dozen times:ios
"What’s the best way to learn Objective-C/Cocoa?"編程
I have two answers to this question:設計模式
Curiosity is the heart of learning anything. If you can cultivate a deep curiosity about the Objective-C language and the Cocoa frameworks, then I guarantee that you will have absolutely no problem learning them. I’m not talking about the 「oh, wouldn’t it be nice to know how to write iOS apps」 kind of curiosity. I’m talking about the 「why does the @
sign go there? What’s up with these asterisks all over the place? Why are things done in this order instead of that order」 kind of curiosity. Gaining this level of curiosity can be very difficult, especially at first. But without it, you’ll never really understand how everything is working together.app
If you’re familiar with an sort of object-oriented programming language, then Objective-C, as a language, shouldn’t be that hard to pick up. Just use get used to the square brackets, and things will look pretty familiar. Learning the frameworks, on the other hand, will take a lifetime. The Cocoa Frameworks (Foundation, AppKit/UIKit, CoreData, CoreFoundation, MapKit, QuickLook, MediaAccess, CoreLocation, etc) are enormously broad. The Foundation framework itself is composed of 173 public classes and 31 protocols. That’s a lot of pre-built code, and that’s just one framework out of a couple dozen! There are some classes you will come to know very intimately (NSString
, NSArray
,NSDictionary
), but even they will surprise you every now and then. Plus, Apple actively develops these frameworks; they’re being improved and optimized and expanded all the time. There will always be more to learn.框架
Along with the capabilities of the classes, there are patterns to learn. Design patterns describe how classes interact with each other. Some of the first ones you’ll encounter are Model View Controller, Delegation, and Class Clusters. There are many others, and they’re in there for a very good reason. As you come across them, take the time to understand why they’re there.編程語言
Do the same with the classes in the frameworks. Explore them. Play with them. Abuse them. Ask yourself:函數
Be curious.學習
As you learn Cocoa, the code you write will be terrible. Understand that. But don’t accept it. There is always a different way you could do something, and quite often one of those different ways is a better way. Always seek out opportunities to improve your code and to find different and better ways to organize it. Sometimes this will mean rewriting things half a dozen times. But you know what? That’s OK! All developers experience this. If you can learn to not be satisfied with your code, you’ll quickly learn how to iterate over it and make improvements here, tweak some stuff there, maybe rip out some layer of control and totally reorganize it… This is what programming is all about. Don’t be satisfied with how your code looks. Strive to make it a work of art and a thing of beauty.優化
So there you have it! These are my recommendations for how to learn Cocoa. They have served me well, and I think they pretty much summarize what I’ve observed about experienced developers.
Oh, and by the way…
-retainCount
.Welcome to Cocoa.
如何學習Cocoa
這是任何一個牛逼的Cocoa開發者被問了無數次的一個問題:
"有什麼最好的方法來學習Object-C/Cocoa?"
對於這個問題,我有兩項建議
1.保持好奇
2.不要輕易知足
保持好奇
學習任何事物好奇心都是最好使的.若是你能對Objective-C和Cocoa的框架有着濃厚的興趣,我敢確定,你學習它絕對沒有一點問題.我不會說"這麼編寫IOS的程序是否是很酷?"之類,我會說"爲何標示符@放在這裏?爲何星號放在那裏?爲何事情必須這樣作而不能那麼作"之類的,達到如此程度的好奇心很是難,尤爲是剛開始.可是,若是沒有好奇心,你不會真正的懂得那些玩意是怎麼運做的.
若是你熟悉一種面向對象的編程語言,那麼,Objective-C,做爲一種新的語言,學習它不會很難.僅僅須要熟悉方括號的調用函數的方法,那麼,他們看起來就會很是相似.學習這些框架,從某種角度上來講,將會耗費很長時間.這個Cocoa的一些框架(Foundation,AppKit/UIKit,CoreData,CoreFoundation,MapKit,QuickLook,MediaAccess,CoreLocation,之類的)內容是很是多的.Foundation框架自己就包含了173個類和31個協議.那些仍是預先編譯過的代碼,僅僅是那一打雞蛋中的一枚-_-!還有一些類(NSString,NSArray,NSDictionary),如今你可能會感到驚奇,但在未來你將會很是之熟悉.進一步講,蘋果一直活躍於開發這些框架,這些框架一直都在完善.擴展以及優化,因此,須要一直的學習他們.
面對這無數的類,是存在着一種學習模式的.設計模式描述了類與類之間是如何產生聯繫的.你第一次會遇到的一些設計模式有MVC.代理和類集羣(我不知道怎麼翻譯,沒接觸過).實際上還有不少其餘的設計模式,存在即合理,當你遇到它的時候,多花些時間與它熟悉熟悉吧.
也請你對框架中的類作着一樣的事情,探索它們,試着用它們,虐它們,問下你本身:
這個類能解決什麼問題?
它如何解決了這個問題?
你能用它們幹些別的嗎?
在不得不重寫類的狀況下,怎樣去實現這個類?(想好了就去作吧)
保持好奇心.
不要輕易知足
當你學習Cocoa時,開始時你的代碼將會很爛,要懂得這個道理,但不要知足於它.你常常能夠嘗試些別的,且經常可以找到更好的方法.找機會優化你的代碼,找不一樣的和更好的方式來重寫它們.有時候,這意味着重寫無數遍,可是你知道嗎?就應該這樣啊.全部的開發人員都會經歷這個過程.若是你學會了不輕易知足,你將很快的認識到一遍又一遍的去重寫它而獲得進步,在這裏用盡全力,也許就能一層一層的撕開祕密並最終徹底弄懂它...編程就是這檔子事.不要知足於你本身寫的代碼,盡力把他當作一門藝術來作,去體會它的美感.
最後的思考
就這些了!就是我關於學習Cocoa的建議.它們對我很受用,我想,這就是我總結出的牛逼開發者的經驗了吧.
哦,順便提提...
1.要知道什麼叫指針
2.記住內存管理規則
3.永遠也不要嘗試判斷retainCount值(來肯定一個對象有沒有釋放掉)
歡迎來到Cocoa的世界