黑苹果编程

Preface

前言

First Mac

I’ve never seen this model. I did see this one:

我从未见过这种模型。 我确实看到了这个:

Mac

Few months ago I bought iPhone. Then Mac mini. I made my first applications for this phone. Then, for my Mac… Goodbye, Microsoft?

几个月前,我买了iPhone。 然后是Mac mini。 我是第一次为这款手机申请的。 然后,对于我的Mac …再见,微软?

I think I found a very easy way to step in to the world of the Apple programming. In this short article I want to show how to begin, how to make a first Mac OS X program. I’m not going to scare anyone, I will not talk about Objective-C, I will not use Cocoa. These are very amazing things, but it is, obviously, not the easiest start.

我认为我找到了进入Apple编程世界的非常简单的方法。 在这篇简短的文章中,我想展示如何开始,如何制作第一个

We all began from the super language – our old and respectful C. From my point of view, it is extremely easy to begin programming for Mac OS X from Carbon – an old procedural 32bit API for developing Mac OS X application.

我们都是从超级语言(我们古老而又受人尊敬的C语言)开始的。从我的角度来看,从

Project Hello

项目你好

In case you have a Mac and Xcode installed on it, it will take 2 seconds to make a window application on Mac:

如果您安装了Mac和

1. Create new project – File menu -> New Project...

1.创建新项目- 文件菜单-> 新建项目 。

2. Choose Carbon Application template in the Application section in the New Project wizard and press Choose button (in the bottom-right corner).

2.在“新建项目”向导的“应用程序”部分中,选择“ Carbon Application”模板,然后按“选择”按钮(在右下角)。

3. Set the project name and press Save button.

3.设置项目名称,然后按保存按钮。

4. Press Build and Go.

4.按构建并执行 。

Here are few images illustrating these steps above:

以下是说明上述步骤的几张图片:

Xcode. New Project.
Set project name

It runs. The window knows how to minimize and maximize itself. The application has the standard Apple menu.

它运行。 窗口知道如何最小化和最大化自身。 该应用程序具有标准的Apple菜单。

Application Hello

Source code

源代码

The application source code is in main.c file. Actually, everything from this file can be removed and replaced by the following code:

应用程序源代码位于

  1. int main (int argc, char* argv[])
  2. {
  3. IBNibRef nibRef;
  4. WindowRef window;
  5. // Create a nib reference to a nib file.
  6. CreateNibReference (CFSTR (“main”), &nibRef);
  7. SetMenuBarFromNib (nibRef, CFSTR(“MenuBar”));
  8. CreateWindowFromNib (nibRef, CFSTR(“MainWindow”), &window);
  9. // Dispose of the nib reference as soon as you don’t need it any more.
  10. DisposeNibReference (nibRef);
  11. // Make the unarchived window visible.
  12. ShowWindow (window);
  13. // Start the event loop. RunApplicationEventLoop is a
  14. // Carbon Event Manager function.
  15. RunApplicationEventLoop ();
  16. return 0;
  17. }

Now the program is much shorter – one C-style function (as it should be in the procedural language) with few lines of code:

现在,程序要短得多-一个

1. Call CreateNibReference function to create the resource object. Call DisposeNibReference in order to release it.

1.调用

2. Call SetMenuBarFromNib function in order to set the application menu.

2.调用

3. Call CreateWindowFromNib to create the window from the resource.

3.调用

4. Call ShowWindow to show the window.

4.调用

5. Use RunApplicationEventLoop function to start the application event loop.

5.使用

For a test purpose I can comment (or delete) three window related lines and the function will look like:

为了进行测试,我可以注释(或删除)三个与窗口相关的行,该函数将如下所示:

  1. int main (int argc, char* argv[])
  2. {
  3. IBNibRef nibRef;
  4. CreateNibReference (CFSTR (“main”), &nibRef);
  5. SetMenuBarFromNib (nibRef, CFSTR(“MenuBar”));
  6. DisposeNibReference (nibRef);
  7. RunApplicationEventLoop ();
  8. return 0;
  9. }

Now this code compiled and launched creates only the menu:

现在,此代码已编译并启动,仅创建菜单:

Only menu
  1. int main (int argc, char* argv[])
  2. {
  3. RunApplicationEventLoop ();
  4. return 0;
  5. }
Default menu

Conclusion

结论

It is a first and trivial experience in the Mac OS X development. But it gives an optimistic feeling, a confidence that the knowledge in C allows to study Mac OS, to make programs for Apple immediately, without long pauses for Objective-C and Cocoa books.

这是Mac OS X开发中的第一次和琐碎的经历。 但是它给人一种乐观的感觉,一种确信,即使用C的知识可以学习Mac OS,可以立即为Apple编写程序,而无需长时间停顿Objective-C和Cocoa书籍。

发表评论

后才能评论