[翻译] 如何学习Cocoa

简介:

[文章原地址]
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:

  1. Be curious
  2. 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 (NSStringNSArray,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 ControllerDelegation, 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…

  1. Know what a pointer is.
  2. Memorize the memory management rules.
  3. 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的世界

目录
相关文章
xal
|
JavaScript API
VSCode插件开发全攻略(九)常用API总结
更多文章请戳[VSCode插件开发全攻略系列目录导航](https://www.atatech.org/articles/121864)。 本文提炼一些常见的API使用场景供参考。 # 本文还没写完,有待补充 # 编辑器相关 ## 修改当前激活编辑器内容 替换当前编辑器全部内容: ```js vscode.window.activeTextEditor.e
xal
2936 0
|
12月前
|
缓存 前端开发 JavaScript
使用 aws lambda 时,开发人员面临的常见挑战之一是管理大型 python 依赖项。
在我们快速发展的在线环境中,只需几秒钟加载的网站就能真正脱颖而出。您是否知道加载时间较快的网站的转化率比加载时间较长的网站高出三倍?
149 0
使用 aws lambda 时,开发人员面临的常见挑战之一是管理大型 python 依赖项。
|
16天前
|
存储 弹性计算 人工智能
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
2025年9月24日,阿里云弹性计算团队多位产品、技术专家及服务器团队技术专家共同在【2025云栖大会】现场带来了《通用计算产品发布与行业实践》的专场论坛,本论坛聚焦弹性计算多款通用算力产品发布。同时,ECS云服务器安全能力、资源售卖模式、计算AI助手等用户体验关键环节也宣布升级,让用云更简单、更智能。海尔三翼鸟云服务负责人刘建锋先生作为特邀嘉宾,莅临现场分享了关于阿里云ECS g9i推动AIoT平台的场景落地实践。
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
|
8天前
|
云安全 人工智能 安全
Dify平台集成阿里云AI安全护栏,构建AI Runtime安全防线
阿里云 AI 安全护栏加入Dify平台,打造可信赖的 AI
|
11天前
|
人工智能 运维 Java
Spring AI Alibaba Admin 开源!以数据为中心的 Agent 开发平台
Spring AI Alibaba Admin 正式发布!一站式实现 Prompt 管理、动态热更新、评测集构建、自动化评估与全链路可观测,助力企业高效构建可信赖的 AI Agent 应用。开源共建,现已上线!
1029 34
|
10天前
|
机器学习/深度学习 人工智能 搜索推荐
万字长文深度解析最新Deep Research技术:前沿架构、核心技术与未来展望
近期发生了什么自 2025 年 2 月 OpenAI 正式发布Deep Research以来,深度研究/深度搜索(Deep Research / Deep Search)正在成为信息检索与知识工作的全新范式:系统以多步推理驱动大规模联网检索、跨源证据。
784 55
下一篇
开通oss服务