[文章原地址]
http://funwithobjc.tumblr.com/
个人翻译,难免会有错误,请各位看官海涵,翻译在末尾
本人将在文章的部分地方添加注释,并根据需求增减文章内容,在此对原作者辛勤劳作表示感谢
How to learn Cocoa
This is something any experienced Cocoa developer has been asked at least half a dozen times:
"What’s the best way to learn Objective-C/Cocoa?"
I have two answers to this question:
- Be curious
- Don’t be satisfied
Be Curious
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.
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:
- What problem does this solve?
- How does it solve the problem?
- Can you use these abilities for anything else?
- How would you implement this class if you had to? (And then go do it!)
Be curious.
Don’t Be Satisfied
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.
Final Thoughts
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…
- Know what a pointer is.
- Memorize the memory management rules.
- Never invoke
-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的世界