1 Sending a message
真正好玩的才伴随着 “sending messages”开始了. Sending a message will be the basic building block of your Objective-C apps because it is how you tell an object to perform some action or to give you some piece of information.
发送消息需要两样东西 - 一个object和一个message name:
[objectName messageName]; #方括号不能少
发送一个简单的消息:
[tryobjc completeThisChallenge];
2 发送描述信息
The tryobjc
object is something we made up, but the objects built-in to Objective-C, like NSString
, NSNumber
, and NSArray
, are real and have a bunch of messages you can send to them.
One of those messages is description
, and there’s something extra cool about it - passing the message returns a result. Not all messages return something, but the description
message always returns an NSString
that represents the contents of the object that you passed the message to.
If you send description
to an NSString object, you’ll get the characters in that string, but if you send it to an NSArray you’ll get a string containing all of the values in that array.
NSArray *foods = @[@"tacos", @"burgers"]; NSLog(@"%@", foods); #输出: #challenge[3]: ( # tacos, # burgers #)