没有合适的资源?快使用搜索试试~ 我知道了~
首页Objective-C 2.0:探索PDF开发教程
Objective-C 2.0:探索PDF开发教程
![](https://csdnimg.cn/release/wenkucmsfe/public/img/star.98a08eaa.png)
本资源是一份关于Objective-C语言的PDF教程,标题明确指出"Objective-C PDF",专注于针对Objective-C 2.0版本的编程学习。Objective-C是一种面向对象的编程语言,由Apple Inc.在2008年11月19日发布,用于其Cocoa框架的开发。这份教程由Apple Inc.官方授权,强调了版权保护,禁止任何形式的复制、存储或未经许可的传播,除非用于个人学习目的。 文档中特别指出,任何个人仅限于在个人计算机上存储文档供自身使用,并可以打印文档供个人参考,但必须保留Apple的版权声明。Apple的商标,如Apple logo,包括"键盘"标志(Option-Shift-K),在商业用途中使用需获得书面许可,否则可能构成商标侵权和不公平竞争,这体现了对知识产权的严谨态度。 值得注意的是,该教程并不授予任何技术使用的许可,Apple公司保留在文档中描述的所有技术相关的知识产权。文档的目的在于提供帮助和支持,以促进应用程序的开发和设计,而不涉及商业授权或技术转让。 对于想要深入学习Objective-C的开发者来说,这份教程无疑包含了重要的基础知识,如类与对象、继承、消息传递、以及Cocoa框架的核心概念。它将引导读者理解如何利用Objective-C进行高效、灵活的程序设计,适应iOS和macOS平台的软件开发需求。因此,对于那些希望在iOS应用开发或Mac OS应用编程领域提升技能的人来说,这是一份不可或缺的学习资料。
资源详情
资源推荐
![](https://csdnimg.cn/release/download_crawler_static/4840513/bg10.jpg)
Methods can also take parameters, or “arguments.” A message with a single argument affixes a colon (:) to
the selector name and puts the argument right after the colon. This construct is called a keyword; a keyword
ends with a colon, and an argument follows the colon, as shown in this example:
[myRectangle setWidth:20.0];
A selector name includes all keywords, including colons, but does not include anything else, such as return
type or parameter types. The imaginary message below tells the myRectangle object to set its origin to the
coordinates (30.0, 50.0):
[myRectangle setOrigin:30.0 :50.0]; // This is a bad example of multiple arguments
Since the colons are part of the method name, the method is named setOrigin::. It has two colons as it
takes two arguments. This particular method does not interleave the method name with the arguments and,
thus, the second argument is effectively unlabeled and it is difficult to determine the kind or purpose of the
method’s arguments.
Instead, method names should interleave the name with the arguments such that the method's name naturally
describes the arguments expected by the method. For example, the Rectangle class could instead implement
a setOriginX:y: method that makes the purpose of its two arguments clear:
[myRectangle setOriginX: 30.0 y: 50.0]; // This is a good example of multiple
arguments
Important: The sub-parts of the method name—of the selector—are not optional, nor can their order be
varied. "Named arguments" and "keyword arguments" often carry the implication that the arguments to a
method can vary at runtime, can have default values, can be in a different order, can possibly have additional
named arguments. This is not the case with Objective-C.
For all intents and purposes, an Objective-C method declaration is simply a C function that prepends two
additional arguments (seeMessaging in the Objective-C 2.0 Runtime Programming Guide). This is different from
the named or keyword arguments available in a language like Python:
def func(a, b, NeatMode=SuperNeat, Thing=DefaultThing):
pass
where Thing (and NeatMode) might be omitted or might have different values when called.
Methods that take a variable number of arguments are also possible, though they’re somewhat rare. Extra
arguments are separated by commas after the end of the method name. (Unlike colons, the commas aren’t
considered part of the name.) In the following example, the imaginary makeGroup: method is passed one
required argument (group) and three that are optional:
[receiver makeGroup:group, memberOne, memberTwo, memberThree];
Like standard C functions, methods can return values. The following example sets the variable isFilled to
YES if myRectangle is drawn as a solid rectangle, or NO if it’s drawn in outline form only.
BOOL isFilled;
isFilled = [myRectangle isFilled];
Note that a variable and a method can have the same name.
One message expression can be nested inside another. Here, the color of one rectangle is set to the color of
another:
16
Object Messaging
2008-11-19 | © 2008 Apple Inc. All Rights Reserved.
CHAPTER 1
Objects, Classes, and Messaging
![](https://csdnimg.cn/release/download_crawler_static/4840513/bg11.jpg)
[myRectangle setPrimaryColor:[otherRect primaryColor]];
Objective-C 2.0 also provides a dot (.) operator that offers a compact and convenient syntax for invoking an
object’s accessor methods. This is typically used in conjunction with the declared properties feature (see
“Declared Properties” (page 57)), and is described in “Dot Syntax” (page 19).
Sending Messages to nil
In Objective-C, it is valid to send a message to nil—it simply has no effect at runtime. There are several
patterns in Cocoa that take advantage of this fact. The value returned from a message to nil may also be
valid:
■ If the method returns an object, then a message sent to nil returns 0 (nil), for example:
Person *motherInLaw = [[aPerson spouse] mother];
If aPerson’s spouse is nil, then mother is sent to nil and the method returns nil.
■ If the method returns any pointer type, any integer scalar of size less than or equal to sizeof(void*),
a float, a double, a long double, or a long long, then a message sent to nil returns 0.
■ If the method returns a struct, as defined by the Mac OS X ABI Function Call Guide to be returned in
registers, then a message sent to nil returns 0.0 for every field in the data structure. Other struct data
types will not be filled with zeros.
■ If the method returns anything other than the aforementioned value types the return value of a message
sent to nil is undefined.
The following code fragment illustrates valid use of sending a message to nil.
id anObjectMaybeNil = nil;
// this is valid
if ([anObjectMaybeNil methodThatReturnsADouble] == 0.0)
{
// implementation continues...
}
Object Messaging 17
2008-11-19 | © 2008 Apple Inc. All Rights Reserved.
CHAPTER 1
Objects, Classes, and Messaging
![](https://csdnimg.cn/release/download_crawler_static/4840513/bg12.jpg)
Note: The behavior of sending messages to nil changed slightly with Mac OS X v10.5.
On MacOSXv10.4 and earlier, a message to nil also is valid, as long as the message returns an object, any
pointer type, void, or any integer scalar of size less than or equal to sizeof(void*); if it does, a message
sent to nil returns nil. If the message sent to nil returns anything other than the aforementioned value
types (for example, if it returns any struct type, any floating-point type, or any vector type) the return value
is undefined. You should therefore not rely on the return value of messages sent to nil unless the method’s
return type is an object, any pointer type, or any integer scalar of size less than or equal to sizeof(void*).
The Receiver’s Instance Variables
A method has automatic access to the receiving object’s instance variables. You don’t need to pass them to
the method as arguments. For example, the primaryColor method illustrated above takes no arguments,
yet it can find the primary color for otherRect and return it. Every method assumes the receiver and its
instance variables, without having to declare them as arguments.
This convention simplifies Objective-C source code. It also supports the way object-oriented programmers
think about objects and messages. Messages are sent to receivers much as letters are delivered to your home.
Message arguments bring information from the outside to the receiver; they don’t need to bring the receiver
to itself.
A method has automatic access only to the receiver’s instance variables. If it requires information about a
variable stored in another object, it must send a message to the object asking it to reveal the contents of
the variable. The primaryColor and isFilled methods shown above are used for just this purpose.
See “Defining a Class” (page 35) for more information on referring to instance variables.
Polymorphism
As the examples above illustrate, messages in Objective-C appear in the same syntactic positions as function
calls in standard C. But, because methods “belong to” an object, messages behave differently than function
calls.
In particular, an object can be operated on by only those methods that were defined for it. It can’t confuse
them with methods defined for other kinds of object, even if another object has a method with the same
name. This means that two objects can respond differently to the same message. For example, each kind of
object sent a display message could display itself in a unique way. A Circle and a Rectangle would respond
differently to identical instructions to track the cursor.
This feature, referred to as polymorphism, plays a significant role in the design of object-oriented programs.
Together with dynamic binding, it permits you to write code that might apply to any number of different
kinds of objects, without you having to choose at the time you write the code what kinds of objects they
might be. They might even be objects that will be developed later, by other programmers working on other
projects. If you write code that sends a display message to an id variable, any object that has a display
method is a potential receiver.
18
Object Messaging
2008-11-19 | © 2008 Apple Inc. All Rights Reserved.
CHAPTER 1
Objects, Classes, and Messaging
![](https://csdnimg.cn/release/download_crawler_static/4840513/bg13.jpg)
Dynamic Binding
A crucial difference between function calls and messages is that a function and its arguments are joined
together in the compiled code, but a message and a receiving object aren’t united until the program is
running and the message is sent. Therefore, the exact method that’s invoked to respond to a message can
only be determined at runtime, not when the code is compiled.
The precise method that a message invokes depends on the receiver. Different receivers may have different
method implementations for the same method name (polymorphism). For the compiler to find the right
method implementation for a message, it would have to know what kind of object the receiver is—what
class it belongs to. This is information the receiver is able to reveal at runtime when it receives a message
(dynamic typing), but it’s not available from the type declarations found in source code.
The selection of a method implementation happens at runtime. When a message is sent, a runtime messaging
routine looks at the receiver and at the method named in the message. It locates the receiver’s implementation
of a method matching the name, “calls” the method, and passes it a pointer to the receiver’s instance variables.
(For more on this routine, see Messaging in the Objective-C 2.0 Runtime Programming Guide.)
This dynamic binding of methods to messages works hand-in-hand with polymorphism to give object-oriented
programming much of its flexibility and power. Since each object can have its own version of a method, a
program can achieve a variety of results, not by varying the message itself, but by varying just the object
that receives the message. This can be done as the program runs; receivers can be decided “on the fly” and
can be made dependent on external factors such as user actions.
When executing code based upon the Application Kit, for example, users determine which objects receive
messages from menu commands like Cut, Copy, and Paste. The message goes to whatever object controls
the current selection. An object that displays text would react to a copy message differently from an object
that displays scanned images. An object that represents a set of shapes would respond differently from a
Rectangle. Since messages don’t select methods (methods aren’t bound to messages) until runtime, these
differences are isolated in the methods that respond to the message. The code that sends the message
doesn’t have to be concerned with them; it doesn’t even have to enumerate the possibilities. Each application
can invent its own objects that respond in their own way to copy messages.
Objective-C takes dynamic binding one step further and allows even the message that’s sent (the method
selector) to be a variable that’s determined at runtime. This is discussed in the section Messaging in the
Objective-C 2.0 Runtime Programming Guide.
Dynamic Method Resolution
You can provide implementations of class and instance methods at runtime using dynamic method resolution.
See Dynamic Method Resolution in the Objective-C 2.0 Runtime Programming Guide for more details.
Dot Syntax
Objective-C provides a dot (.) operator that offers a compact and convenient syntax you can use as an
alternative to square bracket notation ([]s) to invoke accessor methods. It is particularly useful when you
want to access or modify a property that is a property of another object (that is a property of another object,
and so on).
Object Messaging 19
2008-11-19 | © 2008 Apple Inc. All Rights Reserved.
CHAPTER 1
Objects, Classes, and Messaging
![](https://csdnimg.cn/release/download_crawler_static/4840513/bg14.jpg)
Using the Dot Syntax
Overview
You can use the dot syntax to invoke accessor methods using the same pattern as accessing structure
elements as illustrated in the following example:
myInstance.value = 10;
printf("myInstance value: %d", myInstance.value);
The dot syntax is purely “syntactic sugar”—it is transformed by the compiler into invocation of accessor
methods (so you are not actually accessing an instance variable directly). The code example above is exactly
equivalent to the following:
[myInstance setValue:10];
printf("myInstance value: %d", [myInstance value]);
General Use
You can read and write properties using the dot (.) operator, as illustrated in the following example.
Listing 1-1 Accessing properties using the dot syntax
Graphic *graphic = [[Graphic alloc] init];
NSColor *color = graphic.color;
CGFloat xLoc = graphic.xLoc;
BOOL hidden = graphic.hidden;
int textCharacterLength = graphic.text.length;
if (graphic.textHidden != YES) {
graphic.text = @"Hello";
}
graphic.bounds = NSMakeRect(10.0, 10.0, 20.0, 120.0);
(@"Hello" is a constant NSString object—see “Compiler Directives” (page 114).)
Accessing a property property calls the get method associated with the property (by default, property) and
setting it calls the set method associated with the property (by default, setProperty:). You can change the
methods that are invoked by using the Declared Properties feature (see “Declared Properties” (page 57)).
Despite appearances to the contrary, the dot syntax therefore preserves encapsulation—you are not accessing
an instance variable directly.
The following statements compile to exactly the same code as the statements shown in Listing 1-1 (page
20), but use square bracket syntax:
Listing 1-2 Accessing properties using bracket syntax
Graphic *graphic = [[Graphic alloc] init];
NSColor *color = [graphic color];
CGFloat xLoc = [graphic xLoc];
BOOL hidden = [graphic hidden];
int textCharacterLength = [[graphic text] length];
20
Object Messaging
2008-11-19 | © 2008 Apple Inc. All Rights Reserved.
CHAPTER 1
Objects, Classes, and Messaging
剩余132页未读,继续阅读
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://profile-avatar.csdnimg.cn/e120f3017ce943d2b02d96f34aceac72_flashdevelop.jpg!1)
橙色下午茶
- 粉丝: 2
- 资源: 26
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![](https://csdnimg.cn/release/wenkucmsfe/public/img/voice.245cc511.png)
最新资源
- AirKiss技术详解:无线传递信息与智能家居连接
- Hibernate主键生成策略详解
- 操作系统实验:位示图法管理磁盘空闲空间
- JSON详解:数据交换的主流格式
- Win7安装Ubuntu双系统详细指南
- FPGA内部结构与工作原理探索
- 信用评分模型解析:WOE、IV与ROC
- 使用LVS+Keepalived构建高可用负载均衡集群
- 微信小程序驱动餐饮与服装业创新转型:便捷管理与低成本优势
- 机器学习入门指南:从基础到进阶
- 解决Win7 IIS配置错误500.22与0x80070032
- SQL-DFS:优化HDFS小文件存储的解决方案
- Hadoop、Hbase、Spark环境部署与主机配置详解
- Kisso:加密会话Cookie实现的单点登录SSO
- OpenCV读取与拼接多幅图像教程
- QT实战:轻松生成与解析JSON数据
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![](https://csdnimg.cn/release/wenkucmsfe/public/img/green-success.6a4acb44.png)