ios developer tiny share-20160815
2016-08-15 16:07
302 查看
今天讲Objective-C的“Objects Send and Receive Messages”,实际就是调用类的方法的语法。
在开头,我们还是Overview下“Working with Objects”话题:
Working with Objects
The majority of work in an Objective-C application happens as a result of messages being sent back and forth across an ecosystem of objects. Some of these objects are instances of classes provided by Cocoa or Cocoa Touch, some are instances of your own classes.
The previous chapter described the syntax to define the interface and implementation for a class, including the syntax to implement methods containing the code to be executed in response to a message. This chapter explains how to send such a message to an object,
and includes coverage of some of Objective-C’s dynamic features, including dynamic typing and the ability to determine which method should be invoked at runtime.
Before an object can be used, it must be created properly using a combination of memory allocation for its properties and any necessary initialization of its internal values. This chapter describes how to nest the method calls to allocate and initialize an
object in order to ensure that it is configured correctly.
Objects Send and Receive Messages
Although there are several different ways to send messages between objects in Objective-C, by far the most common is the basic syntax that uses square brackets, like this:
[someObject doSomething];
The reference on the left, someObject in this case, is the receiver of the message. The message on the right, doSomething, is the name of the method to call on that receiver. In other words, when the above line of code is executed, someObject will be sent the
doSomething message.
The previous chapter described how to create the interface for a class, like this:
@interface XYZPerson : NSObject
- (void)sayHello;
@end
and how to create the implementation of that class, like this:
@implementation XYZPerson
- (void)sayHello {
NSLog(@"Hello, world!");
}
@end
Note: This example uses an Objective-C string literal, @"Hello, world!". Strings are one of several class types in Objective-C that allow a shorthand literal syntax for their creation. Specifying @"Hello, world!" is conceptually equivalent
to saying “An Objective-C string object that represents the string Hello, world!.”
Literals and object creation are explained further in Objects Are Created Dynamically, later in this chapter.
Assuming you’ve got hold of an XYZPerson object, you could send it the sayHello message like this:
[somePerson sayHello];
Sending an Objective-C message is conceptually very much like calling a C function. Figure 2-1 shows the effective program flow for the sayHello message.
Figure 2-1 Basic messaging program flow
In order to specify the receiver of a message, it’s important to understand how pointers are used to refer to objects in Objective-C.
在开头,我们还是Overview下“Working with Objects”话题:
Working with Objects
The majority of work in an Objective-C application happens as a result of messages being sent back and forth across an ecosystem of objects. Some of these objects are instances of classes provided by Cocoa or Cocoa Touch, some are instances of your own classes.
The previous chapter described the syntax to define the interface and implementation for a class, including the syntax to implement methods containing the code to be executed in response to a message. This chapter explains how to send such a message to an object,
and includes coverage of some of Objective-C’s dynamic features, including dynamic typing and the ability to determine which method should be invoked at runtime.
Before an object can be used, it must be created properly using a combination of memory allocation for its properties and any necessary initialization of its internal values. This chapter describes how to nest the method calls to allocate and initialize an
object in order to ensure that it is configured correctly.
Objects Send and Receive Messages
Although there are several different ways to send messages between objects in Objective-C, by far the most common is the basic syntax that uses square brackets, like this:
[someObject doSomething];
The reference on the left, someObject in this case, is the receiver of the message. The message on the right, doSomething, is the name of the method to call on that receiver. In other words, when the above line of code is executed, someObject will be sent the
doSomething message.
The previous chapter described how to create the interface for a class, like this:
@interface XYZPerson : NSObject
- (void)sayHello;
@end
and how to create the implementation of that class, like this:
@implementation XYZPerson
- (void)sayHello {
NSLog(@"Hello, world!");
}
@end
Note: This example uses an Objective-C string literal, @"Hello, world!". Strings are one of several class types in Objective-C that allow a shorthand literal syntax for their creation. Specifying @"Hello, world!" is conceptually equivalent
to saying “An Objective-C string object that represents the string Hello, world!.”
Literals and object creation are explained further in Objects Are Created Dynamically, later in this chapter.
Assuming you’ve got hold of an XYZPerson object, you could send it the sayHello message like this:
[somePerson sayHello];
Sending an Objective-C message is conceptually very much like calling a C function. Figure 2-1 shows the effective program flow for the sayHello message.
Figure 2-1 Basic messaging program flow
In order to specify the receiver of a message, it’s important to understand how pointers are used to refer to objects in Objective-C.
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- js 未结束的字符串常量错误解决方法
- JavaScript 未结束的字符串常量常见解决方法
- IE6下出现JavaScript未结束的字符串常量错误的解决方法
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用开发中AFNetworking库的常用HTTP操作方法小结
- iOS应用中UISearchDisplayController搜索效果的用法
- iOS App开发中的UISegmentedControl分段组件用法总结
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- iOS中UIAlertView警告框组件的使用教程
- 浅析iOS应用开发中线程间的通信与线程安全问题