对Objective-c代码感到沮丧 [英] Frustrated with Objective-c code

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

问题描述

嗯,我已经开始使用Head First iPhone Development(O'reilly)进行iPod/iPhone编程,并且正在从书中键入代码.有两个问题,一个与编程有关,另一个与编程无关.

Well, I've started with iPod/iPhone programming using Head First iPhone Development (O'reilly) and I'm typing code out of the book. There are two problems, one is programming related and the other is not.

  • 我不了解Objective-C方法的格式.根据这本书的源代码,我现在遇到一些错误.这导致 我进入下一个问题.

  • I don't understand the format of objective-c methods. I'm getting an few errors now, based on source code from the book. Which leads me to my next issue.

某些代码有错误.我认为是这样,因为如果不修改它就无法运行代码.由于这是第一版,本书中有一些错别字,但我的修复"代码可能与此有关吗?

Some of the code is buggy. I think so because I couldn't get the code to run without modifying it. The book has some typos in the text since it's a first edition and whatnot, but could my "fixing" the code have to do with it?

所以... 在哪里可以了解有关Objective-C方法的更多信息,以及它们在结构上如何工作以及返回类型和参数在哪里使用?

对于那些拥有本书的人,我在开始时处于InstaTweet应用程序的中间.

For those with the book, I'm in the middle of the InstaTweet app towards the beginning.

谢谢.

推荐答案

也许将类似于c的"语言与obj-c进行比较会很有用-让我们一起来看看Java.想象一下带有setBackgroundColor方法的Rectangle类.我们假设我们有一个名为rect的Rectangle实例.

Maybe a comparison between a "c-like" language and obj-c would be useful -- let's go with Java. Imagine a Rectangle class with a setBackgroundColor method. We'll assume we have an instance of Rectangle called rect.

在Java中,方法签名可能是

In Java, the method signature would likely be

public void setBackgroundColor(int r, int g, int b) { ... }

在Objective-C中,参数是方法签名的一部分,因此它可能是

In Objective-C, arguments are part of the method signature, so it might be

- (void)setBackgroundColorWithRed:(int)r green:(int)g blue:(int)b;

-"表示它是Rectangle类的实例方法. (void)是方法的返回类型.然后来论证.每个冒号都定义一个参数,该参数将被键入(在此示例中,每个arg为(int).

The "-" means it's an instance method on the Rectangle class. (void) is the method's return type. Then come the arguments. Each colon defines an argument, which is typed (each arg is (int) in this example).

让我们比较调用这些方法. Java:

Let's compare calling these methods. Java:

rect.setBackgroundColor(255, 255, 0);

Obj-c:

[rect setBackgroundColorWithRed:255 green:255 blue:0];

很多人迷恋obj-c的语法-您并不孤单.希望这种比较可以使事情变得更清楚.它还体现了Objective-C的语言优势:在通话时,很清楚您的论点是什么.如果我正在阅读代码并看到Java方法被调用,那么参数是红色,蓝色和绿色就不是很明显了.令人费解的是,在obj-c中,我们正在设置各个颜色值.当然,大多数开发人员都可以猜测setColor方法的三个参数是什么,但是更复杂的方法会引起混淆.这是用Java和Objective-c定义的更复杂的方法:

A lot of people stumble on obj-c's syntax -- you're not alone. Hopefully this comparison will make things clearer. It also exemplifies a linguistic strength of objective-c: at call time, it's clear what your arguments are. If I were reading code and saw the Java method being called, it wouldn't be obvious that the arguments are red, blue, and green. In obj-c, it's painfully clear that we're setting individual color values. Granted, most developers can guess what the three arguments to a setColor method are, but more complex methods get confusing. Here is a more complex method defined in Java and objective-c:

static Dog createDog(  String name
                     , int age
                     , boolean isNeutered
                     , String ownerName
                    );
+ (Dog *)createDogNamed:(NSString *)name 
     age:     (int)        age 
     neutered:(BOOL)       isNeutered 
     owner:   (NSString *) owner;

在通话时:

Dog.createDog("Fluffy", 2, true, "Moshe");
[Dog createDogNamed:@"Fluffy" age:2 neutered:YES owner:@"Moshe"];

Objective-c较为冗长,但更具可读性.在Java示例中,还不清楚"2"和"true"自变量的含义. Objective-c的读法几乎像英语.

Objective-c is more verbose, but much more readable. In the Java example, it's not really clear what the "2" and "true" arguments mean. Objective-c almost reads like English.

其他人已经发布了指向更深入指南的链接,但是我希望这可以使您摆脱语法绊脚石,足以使这些链接有用.我很乐意回答您的更多具体问题.以我的经验,obj-c新手在语法方面非常艰难,然后单击它并感到光彩.挂在那里!

Other people have posted links to more in-depth guides, but I hope this gets you past the syntactic stumbling blocks enough to make the links useful. I'm happy to answer more specific questions you have. In my experience, obj-c newbies have a really tough time with syntax, and then it clicks and feels brilliant. Hang in there!

这篇关于对Objective-c代码感到沮丧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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