【OC语法快览】二、存取方法

Accessors

存取方法

All instance variables are private in Objective-C by default, so you should use accessors to get and set values in most cases. There are two syntaxes. This is the traditional 1.x syntax:

OC中所有的实例变量默认是私有的,所以多数情况下你应该使用访问器来获得和设置实例变量的值。访问器有两种语法。下面说的是传统的1.x版本:

[photo
setCaption:@"Day at the Beach"];

output = [photo
caption];

The code on the second line is not reading the instance variable directly. It‘s actually calling a method named caption. In most cases, you don‘t add the "get" prefix to
getters in Objective-C.

上面的第二行代码不是直接读取实例变量值,实际上是调用了名叫caption的方法。多数情况下,你不要在OC的取值方法中添加"get"前缀。

Whenever you see code inside square brackets, you are sending a message to an object or a class.

每当你看到中括弧中得代码时,你正在给一个类或实例对象发送消息。

Dot Syntax

点语法

The dot syntax for getters and setters is new in Objective-C 2.0, which is part of Mac OS X 10.5:

存取方法的点语法是在OC 2.0版中作为Mac
OS X 10.5的一部分新加入的。

photo.caption = @"Day at the Beach";

output = photo.caption;

You can use either style, but choose only one for each project. The dot syntax should only be used setters and getters, not for general purpose methods.

你可以使用以上两种方式,但一个项目只能选用一种方式。点语法不适用于普通用意的方法,只能用作设值和取值方法,也就是存取方法。

原文:learn_objective_C part
2

【OC语法快览】二、存取方法

时间: 2024-08-06 03:46:51

【OC语法快览】二、存取方法的相关文章

【OC语法快览】三、创建实例对象

Creating Objects  创建对象 There are two main ways to create an object. The first is the one you saw before: 创建对象主要有两种方法.第一种如下: NSString* myString = [NSString string]; This is the more convenient automatic style. In this case, you are creating an autorel

【OC语法快览】四、基础内存管理

Basic Memory Management                                                           基础内存管理 If you're writing an application for Mac OS X, you have the option to enable garbage collection. In general, this means that you don't have to think about memory

【OC语法快览】六、类实现

Class Implementation      类实现 Let's create an implementation, starting with the getters: 接下来创建一个类实现,从访问器开始: #import "Photo.h" @implementation Photo - (NSString*) caption { return caption; } - (NSString*) photographer { return photographer; } @en

【OC语法快览】五、设计类接口

Designing a Class Interface     设计类接口 The Objective-C syntax for creating a class is very simple. It typically comes in two parts. 创建类的语法是很简单的,通常包括两部分. The class interface is usually stored in the ClassName.h file, and defines instance variables and

【OC语法快览】一、方法调用

调用方法 [object method]; [object methodWithInput:input]; output = [object methodWithOutput]; output = [object methodWithInputAndOutput:input]; id myObject = [NSString string]; NSString* myString = [NSString string]; 嵌套消息 function1 ( function2() ); [NSSt

iOS开发——语法篇OC篇&高级语法精讲二

Objective高级语法精讲二 Objective-C是基于C语言加入了面向对象特性和消息转发机制的动态语言,这意味着它不仅需要一个编译器,还需要Runtime系统来动态创建类和对象,进行消息发送和转发.下面通过分析Apple开源的Runtime代码(我使用的版本是objc4-646.tar)来深入理解Objective-C的Runtime机制. Runtime数据结构 在Objective-C中,使用[receiver message]语法并不会马上执行receiver对象的message方

OC语言的特性(二)-Block

本篇文章的主要内容 了解何谓block. 了解block的使用方法. Block 是iOS在4.0版本之后新增的程序语法. 在iOS SDK 4.0之后,Block几乎出现在所有新版的API之中,换句话说,如果不了解Block这个概念就无法使用SDK 4.0版本以后的新功能,因此虽然Block本身的语法有点难度,但为了使用iOS的新功能我们还是得硬着头皮去了解这个新的程序概念. 一.看一看什么是Block 我们使用'^'运算符来声明一个Block变量,而且在声明完一个Block变量后要像声明普通

【IOS】IOS快速入门之OC语法

Objective-C 是 C 语言的超集 您还可以访问标准 C 库例程,例如在 stdlib.h 和 stdio.h 中声明的那些例程. Objective-C 还是一种非常动态的程序设计语言,而且这种动态是其最大优势.这种动态体现在它允许在运行应用程序时(即运行时)才去确定其行为,而不是在生成期间就已固定下来.因此,Objective-C 的动态机制让程序免受约束(编译和链接程序时施加的约束):进而在用户控制下,将大多数符号解析责任转移到运行时. 当您想要在源代码中包括头文件时,请在头文件或

(转载)OC语法总结

1.定义类:@interface 类名 : 父类@end 2.使用:(冒号)表示继承一个类Student : NSObject 3.使用()定义一个Catagory(类别) * 作用:在不改变原有类结构的基础上,扩展原有类的方法(不能扩展属性),但不建议重载原有类的方法 * 开发工具默认生成的文件为:类名+Catagory名称 * Catagory可以写在单独的文件中,也可以写在原有类的文件中,如何写根据需求来决定. 4.使用<>表示实现一个Protocol(协议),如需实现多个协议,将协议名