objective-c编程_Objective-C编程在线教程

objective-c编程

This is the part of a series of tutorials on Programming in Objective-C. It's not about iOS development though that will come with time. Initially, though, these tutorials will teach the Objective-C language. You can run them using ideone.com.

这是有关Objective-C编程的一系列教程的一部分。 尽管这会随着时间的流逝而发生,但这与iOS开发无关。 但是,最初,这些教程将教授Objective-C语言。 您可以使用ideone.com来运行它们。

Eventually, we'll want to go a bit further than this, compiling and testing Objective-C on Windows and I'm looking at GNUStep or using Xcode on Macx.

最终,我们需要做得更多,在Windows上编译和测试Objective-C,我正在研究GNUStep或在Macx上使用Xcode。

Before we can learn to write code for the iPhone, we really need to learn the Objective-C language. Although I'd written a developing for iPhone tutorial before, I realized that the language could be a stumbling block.

在我们真正学习为iPhone编写代码之前,我们确实需要学习Objective-C语言。 尽管我以前曾为iPhone编写过开发教程,但我意识到该语言可能是绊脚石。

Also, memory management and compiler technology have changed dramatically since iOS 5, so this is a restart.

另外,自iOS 5以来,内存管理和编译器技术已发生了巨大变化,因此这是重新启动。

To C or C++ developers, Objective-C can look quite odd with its message sending syntax [likethis] so, a grounding in a few tutorials on the language will get us moving in the right direction.

对于C或C ++开发人员而言,Objective-C的消息发送语法可能看起来很奇怪[likethis],因此,基于该语言的一些教程的基础将使我们朝正确的方向发展。

什么是Objective-C? ( What Is Objective-C? )

Developed over 30 years ago, Objective-C was backwards compatible with C but incorporated elements of the programming language Smalltalk.

30年前开发的Objective-C与C向后兼容,但结合了编程语言Smalltalk的元素。

In 1988 Steve Jobs founded NeXT and they licensed Objective-C. NeXT was acquired by Apple in 1996 and it was used to build the Mac OS X Operating System and eventually iOS on iPhones and iPads.

1988年,史蒂夫·乔布斯(Steve Jobs)创立了NeXT,他们授权了Objective-C。 NeXT在1996年被苹果公司收购,并被用于构建Mac OS X操作系统,并最终在iPhone和iPad上构建iOS。

Objective-C is a thin layer on top of C and retains backward compatibility such that Objective-C compilers can compile C programs.

Objective-C是C之上的薄层,并保持向后兼容性,因此Objective-C编译器可以编译C程序。

在Windows上安装GNUStep ( Installing GNUStep on Windows )

These instructions came from this StackOverflow post. They explain how to install GNUStep for Windows.

这些说明来自此StackOverflow帖子 。 他们介绍了如何为Windows安装GNUStep。

GNUStep is a MinGW derivative that lets you install a free and open version of the Cocoa APIs and tools on many platforms. These instructions are for Windows and will let you compile Objective-C programs and run them under Windows.

GNUStep是MinGW的衍生产品,可让您在许多平台上安装免费和开放版本的Cocoa API和工具。 这些说明适用于Windows,使您可以编译Objective-C程序并在Windows下运行它们。

From the Windows Installer page, go to the FTP site or HTTP Access and download the latest version of the three GNUStep installers for the MSYS System, Core, and Devel. I downloaded gnustep-msys-system-0.30.0-setup.exe, gnustep-core-0.31.0-setup.exe and gnustep-devel-1.4.0-setup.exe. I then installed them in that order, system, core and devel.

从Windows Installer页面上,转到FTP站点或HTTP Access并下载用于MSYS System,Core和Devel的三个GNUStep安装程序的最新版本。 我下载了gnustep-msys-system-0.30.0-setup.exegnustep-core-0.31.0-setup.exegnustep-devel-1.4.0-setup.exe 。 然后,我按顺序,系统,核心和开发来安装它们。

Having installed those, I ran a command line by clicking start, then clicking run and typing cmd and pressing enter. Type gcc -v and you should see several lines of text about the compiler ending in gcc version 4.6.1 (GCC) or similar.

安装完这些程序后,我通过单击“开始”,然后单击“运行”并键入cmd并按Enter键来运行命令行。 键入gcc -v,您应该看到几行有关以gcc版本4.6.1(GCC)或类似版本结尾的编译器的文本。

If you don't, ie it says File not found then you may have another gcc already installed and need to correct the Path. Type in set at the cmd line and you'll see lots of environment variables. Look for Path= and many lines of text which should end in ;C:\GNUstep\bin;C:\GNUstep\GNUstep\System\Tools.

如果没有,则说没有找到文件,那么您可能已经安装了另一个gcc,需要更正路径。 在cmd行输入set,您将看到很多环境变量。 查找Path =和许多应以; C:\ GNUstep \ bin; C:\ GNUstep \ GNUstep \ System \ Tools结尾的文本行。

If it doesn't, then open the Windows Control Panel look for System and when a Window opens, click Advanced System Settings then click the Environment variables. Scroll down the System Variables list on the Advanced tab until you find Path. Click Edit and select All on the Variable Value and paste it into Wordpad.

如果不是,请打开Windows“控制面板”以查找“系统”,然后在打开窗口时单击“高级系统设置”,然后单击“环境”变量。 向下滚动“高级”选项卡上的“系统变量”列表,直到找到“路径”。 单击编辑,然后在变量值上选择全部,然后将其粘贴到写字板中。

Now edit the paths so you add the bin folder path then select all and paste it back into the Variable value then close all the windows. Press ok, open a new cmd line and now gcc -v should work.

现在编辑路径,以便添加bin文件夹路径,然后选择所有路径并将其粘贴回Variable值,然后关闭所有窗口。 按确定,打开新的cmd行,现在gcc -v应该可以工作了。

Mac用户 ( Mac Users )

You should sign up to the free Apple development programs and then download Xcode. There's a bit of setting up a Project in that but once it's done (I'll cover that in a separate tutorial), you will be able to compile and run Objective-C code. For now, the Ideone.com website provides the easiest method of all for doing that.

您应该注册免费的Apple开发程序,然后下载Xcode。 在其中设置了一个项目,但是一旦完成(我将在单独的教程中介绍),您将能够编译和运行Objective-C代码。 目前,Ideone.com网站提供了所有方法中最简单的方法。

Objective-C有什么不同? ( What's Different about Objective-C? )

About the shortest program you can run is this:

关于您可以运行的最短程序是这样的:

#import <Foundation/Foundation.h>int main (int argc, const char *argv[]){    NSLog (@"Hello World") ;    return (0) ;}

#import <Foundation/Foundation.h>int main (int argc, const char *argv[]){ NSLog (@"Hello World") ; return (0) ;}

You can run this on Ideone.com. The output is (unsurprisingly) Hello World, though it will be sent to stderr as that's what NSLOG does.

您可以在Ideone.com上运行它。 输出是(毫不奇怪)Hello World,尽管它会发送到stderr,因为这是NSLOG的工作。

一些要点 ( Some Points )

  • #import is the Objective-C equivalent of #include in C.

    #import是C中#include的Objective-C等效项。
  • Instead of zero terminated C string, I've used Objective-C's strings. These always start with @ as in @"Example of a string".

    我使用了Objective-C的字符串,而不是零终止的C字符串。 它们总是以@开头,如@“字符串示例”中所示。
  • The main function is no different.

    主要功能没有什么不同。

In the next Objective-C tutorial I'll look at objects and OOP in Objective-C.

在下一个Objective-C教程中,我将研究Objective-C中的对象和OOP。

翻译自: https://www.thoughtco.com/objective-c-programming-online-tutorial-one-958338

objective-c编程

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值