先学C再学Objective-C [英] Learn C first before learning Objective-C

查看:14
本文介绍了先学C再学Objective-C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个有抱负的 Apple 开发者,我想得到社区的意见,如果在进入 Objective-C 并最终进入 Cocoa 框架之前先学习 C 会更好吗?

Being an aspiring Apple developer, I want to get the opinions of the community if it is better to learn C first before moving into Objective-C and ultimately the Cocoa Framework?

我的直觉告诉我学习 C,这会给我打下良好的基础.

My gut says learn C, which will give me a good foundation.

推荐答案

我会先学 C.在转向 Obj-C 之前,我学习了 C(并且在 C 中做了很多).我有很多同事从来都不是真正的 C 程序员,他们从 Obj-C 开始,只学习了必要的 C.

I would learn C first. I learned C (and did a lot in C) before moving to Obj-C. I have many colleagues who never were real C programmers, they started with Obj-C and learned only as much C as necessary.

我时不时地看到他们如何完全在 Obj-C 中解决问题,有时会导致非常笨拙的解决方案.通常我然后用纯C代码替换一些Obj-C代码(毕竟你可以随意混合它们,Obj-C方法的内容可以完全是纯C代码).无意侮辱任何 Obj-C 程序员,Obj-C 中有一些非常优雅的解决方案,这些解决方案由于 objects(OOP 编程可以让复杂的程序比函数式编程更可爱;例如,多态性是一个很棒的特性)……我真的很喜欢 Obj-C(比 C++ 还多!我讨厌 C++ 语法,而且一些语言特性显然是矫枉过正并导致糟糕的发展模式恕我直言);然而,当我有时重写我同事的 Obj-C 代码时(我真的只这样做,如果我认为这是绝对必要的),得到的代码通常小 50%,只需要它使用的内存的 25%运行时速度提高了约 400%.

Every now and then I see how they solve a problem entirely in Obj-C, sometimes resulting in a very clumsy solutions. Usually I then replace some Obj-C code with pure C code (after all you can mix them as much as you like, the content of an Obj-C method can be entirely, pure C code). Without any intention to insult any Obj-C programmer, there are solutions that are very elegant in Obj-C, these are solutions that just work (and look) a lot better thanks to objects (OOP programming can make complex programs much more lovely than functional programming; polymorphism for example is a brilliant feature)... and I really like Obj-C (much more than C++! I hate the C++ syntax and some language features are plain overkill and lead to bad development patterns IMHO); however, when I sometimes re-write Obj-C code of my colleagues (and I really only do so, if I think this is absolutely necessary), the resulting code is usually 50% smaller, needs only 25% of the memory it used before and is about 400% faster at runtime.

我在这里想说的是:每种语言都有其优点和缺点.C 有利有弊,Obj-C 也是如此.然而,Obj-C 真正伟大的特性(这就是为什么我比 Java 更喜欢它的原因)是你可以随意跳转到普通 C 并再次返回.为什么这是一个如此出色的功能?因为就像 Obj-C 修复了纯 C 的许多缺点一样,纯 C 可以修复 Obj-C 的一些缺点.如果您将它们混合在一起,您将获得一个非常强大的团队.

What I'm trying to say here: Every language has its pros and cons. C has pros and cons and so does Obj-C. However, the really great feature of Obj-C (that's why I even like it more than Java) is that you can jump to plain C at will and back again. Why this is such a great feature? Because just like Obj-C fixes many of the cons of pure C, pure C can fix some of the cons of Obj-C. If you mix them together you'll receive a very powerful team.

如果你只学过 Obj-C 而对 C 一无所知,或者只知道它的基础知识,从未尝试过它如何优雅地解决一些常见问题,那么你实际上只学会了 Obj-C 的一半.C 是 Obj-C 的基础部分.随时随地使用 C 的能力是它的一个基本特征.

If you only learn Obj-C and have no idea of C or only know the very basics of it and never tried how elegantly it can solve some common problems, you actually learned only half of Obj-C. C is a fundamental part of Obj-C. The ability to use C at any time and everywhere is a fundamental feature of it.

一个典型的例子是我们使用的一些代码必须在 base64 中编码数据,但我们不能为此使用外部库(没有 OpenSSL 库).我们使用了完全使用 Cocoa 类编写的 base64 编码器.它工作正常,但是当我们让它编码 200 MB 的二进制数据时,它花费了永恒并且内存开销是不可接受的.我用一个完全写成一个 C 函数的微型、超紧凑的 base64 编码器替换了它(我将函数体复制到方法体中,方法将 NSData 作为输入并返回 NSString 作为输出,但是在函数内部,一切都是 C).C 编码器更加紧凑,它的速度比纯 Cocoa 编码器高 8 倍,而且内存开销也少得多.编码/解码数据、玩比特和类似的低级任务只是 C 的强项.

A typical example was some code we used that had to encode data in base64, but we could not use an external library for that (no OpenSSL lib). We used a base64 encoder, entirely written using Cocoa classes. It was working okay, but when we made it encode 200 MB of binary data, it took an eternity and the memory overhead was unacceptable. I replaced it with a tiny, ultra compact base64 encoder written entirely as one C function (I copied the function body into the method body, method took NSData as input and returned NSString as output, however inside the function everything was C). The C encoder was so much more compact, it beat the pure Cocoa encoder by the factor 8 in speed and the memory overhead was also much less. Encoding/Decoding data, playing around with bits and similar low level tasks are just the strong points of C.

另一个例子是一些绘制大量图形的 UI 代码.为了存储绘制图形所需的数据,我们使用了 NSArray.实际上是 NSMutableArray 的,因为图形是动画的.结果:非常缓慢的图形动画.我们用普通的 C 数组替换了所有 NSArray,用结构替换了对象(毕竟图形坐标信息不是你必须在对象中拥有的),用简单的 for 循环访问枚举器,并开始用 memcopy 在数组之间移动数据,而不是从一个数组中获取数据到另一个,索引索引.结果:速度提高了 4 倍.即使在较旧的 PPC 系统上,图形动画也很流畅.

Another example was some UI code that drew a lot of graphs. For storing the data necessary to paint the graphs, we used NSArray's. Actually NSMutableArray's, since the graph was animated. Result: Very slow graph animation. We replaced all NSArray's with normal C arrays, objects with structs (after all graph coordinate information is nothing you must have in objects), enumerator access with simple for loops and started moving data between the arrays with memcopy instead of taking data from one array to the other one, index for index. The result: A speed up by the factor 4. The graph animated smoothly, even on older PPC systems.

C 的弱点在于,从长远来看,每个更复杂的程序都会变得丑陋.保持 C 应用程序的可读性、可扩展性和可管理性需要程序员的大量纪律.许多项目都失败了,因为缺少这门学科.Obj-C 使您可以轻松地使用类、继承、协议等来构建应用程序.也就是说,除非必要,否则我不会跨越方法的边界使用纯 C 功能.我更喜欢将 Objective-C 应用程序中的所有代码保留在对象的方法中;其他一切都违背了面向对象应用程序的目的.但是在这个方法中,我有时只使用纯 C.

The weakness of C is that every more complex program gets ugly in the long run. Keeping C applications readable, extensible and manageable demands a lot of discipline of a programmer. Many projects fail because this discipline is missing. Obj-C makes it easy for you to structure your application using classes, inheritance, protocols and so on. That said, I would not use pure C functionality across the borders of a method unless necessary. I prefer to keep all code in an Objective-C app within the method of an object; everything else defeats the purpose of an OO application. However within the method I sometimes use pure C exclusively.

这篇关于先学C再学Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆