Understanding the Objective

Understanding the Objective-C Runtime

The Objective-C Runtime is one of the overlooked features of Objective-C initially when people are generally introduced to Cocoa/Objective-C. The reason for this is that while Objective-C (the language) is easy to pick up in only a couple hours, newcomers to Cocoa spend most of their time wrapping their heads around the Cocoa Framework and adjusting to how it works. However the runtime is something that everybody should at least know how it works in some detail beyond knowing that code like[target doMethodWith:var1];gets translated intoobjc_msgSend(target,@selector(doMethodWith:),var1);by the compiler. Knowing what the Objective-C runtime is doing will help you gain a much deeper understanding of Objective-C itself and how your app is run. I think Mac/iPhone Developers will gain something from this, regardless of your level of experience.Objective-C运行时对于刚刚学习cocoa/Objective-c的新手来说是一个很宏观的基础特征。这个原因是Ojbective-c是一门在几个小时之内就可以学会的语言,但是初学者将会花费大量的时间纠结在Cocoa Framework和弄明白他是怎么工作的。但是每一个人都至少得知道运行时在细节上是怎么工作的要超过像这样的代码 [target doMethodWith:var1](可以被编译器翻译成objc_msgSend(target,@selector(doMethodWith:),var1);)理解运行时可以帮助你对objective-c有一个更深的认识,并且知道自己的程序是怎样原型的。我想Mac/iphone开发者将能够从这里获取很多东西,忽略掉你的经验。

The Objective-C Runtime is Open Source

Objective-c运行时是开源的The Objective-C Runtime is open source and available anytime from In fact examining the Objective-C is one of the first ways I went through to figure out how it worked, beyond reading Apples documentation on the matter. You can download the current version of the runtime (as of this writting) for Mac OS X 10.6.2 hereobjc4-437.1.tar.gz.Objective-c运行时是一开源的,并且可以从 获得。实际上测试Objective-c是我解释它怎么运行的第一种方法,要比阅读拼过的文档要好,在这个问题上。你可以下载当前版本的运行时。Dynamic vs Static Languages

动态语言和静态语言的比较Objective-C is a runtime oriented language, which means that when it’s possible it defers decisions about what will actually be executed from compile & link time to when it’s actually executing on the runtime. This gives you a lot of flexibility in that you can redirect messages to appropriate objects as you need to or you can even intentionally swap method implementations, etc. This requires the use of a runtime which can introspect objects to see what they do & don’t respond to and dispatch methods appropriately. If we contrast this to a language like C. In C you start out with amain()method and then from there it’s pretty much a top down design of following your logic and executing functions as you’ve written your code. A C struct can’t forward requests to perform a function onto other targets. Pretty much you have a program like soObjective-c是一个运行时的面向对象的怨言,这意味着它将在运行时决定执行什么而不是在编译时。这给了你很大的便利性,直接按照你的需要向对象发送消息。或者你甚至可以故意的改变方法实现等等。。。这需要对能够对对象内省来看起能够和不能够执行哪些方法并且将方法发送给对象的运行时的使用。如果我们和像C一样的语言比较。使用C语言,你从main()方法开始,然后完全看着你设计的逻辑和你的代码自上而下的执行。一个c结构体不能直接的讲一个函数运行在一个对象上。就像这个程序:#include < stdio.h > int main(int argc, const char **argv[]){printf("Hello World!");return 0;} which a compiler parses, optimizes and then transforms your optimized code into assembly

编译器语法分析,优化然后将你最佳化的代码翻译成汇编语言.text .align 4,0×90 .globl _main_main:Leh_func_begin1: pushq %rbpLlabel1: movq %rsp, %rbpLlabel2: subq $16, %rspLlabel3: movq %rsi, %rax movl %edi, %ecx movl %ecx, -8(%rbp) movq %rax, -16(%rbp) xorb %al, %al leaq LC(%rip), %rcx movq %rcx, %rdi call _printf movl $0, -4(%rbp) movl -4(%rbp), %eax addq $16, %rsp popq %rbp retLeh_func_end1: .cstringLC: .asciz "Hello World!"and then links it together with a library and produces a executable. This contrasts from Objective-C in that while the process is similar the code that the compiler generates depends on the presence of the Objective-C Runtime Library. When we are all initially introduced to Objective-C we are told that (at a simplistic level) what happens to our Objective-C bracket code is something like…

然后将库和它链接起来,产生一个可执行文件。这和Objective-C相比依赖Objective-C库的编译器产生了相同的代码。当我们刚开始接触Objective-C的时候,我们被告诉像这样被中括号包裹的Objective-C代码

[self doSomethingWithVar:var1];gets translated to…

被翻译成objc_msgSend(self,@selector(doSomethingWithVar:),var1);but beyond this we don’t really know much till much later on what the runtime is doing.但是超过这,实际上我们并不知道之后运行时做了些什么。What is the Objective-C Runtime?

如果你希望成功,以恒心为良友,以经验为参谋,以小心为兄弟,以希望为哨兵。

Understanding the Objective

相关文章:

你感兴趣的文章:

标签云: