好文档 - 专业文书写作范文服务资料分享网站

Objective c学习笔记

天下 分享 时间: 加入收藏 我要投稿 点赞

Objective-C学习

1、Objective-C是C的超级,是完全面向对象的语言,而C是面向过程的语言,具体体现在是要求对象做事,而不是仅仅的调用函数 2、Objective-C中调用方法被成为发送消息

3、Objective-C引用头文件使用#import与C中是用#include选项相同,区别在于#import会自动跳过已经添加的文件,不会因为重复添加而报错

4、定义接口使用关键字@interface和@end实现接口使用关键字@implementation和@end 5、在对象类型的右面有一个星号(*),在Objective-C中,所有的对象变量都是指针类型。id类型已经被预定义为指针类型,所以不需要加一个星号。 6、访问器 1.x语法:

[photo setCaption:@”Day at the Beach”]; NSString* myString =[photo caption];

大多数情况你不用在获取器(getter)前面添加一个”get”前缀

Objective-C 2.0新增了点操作符的设置器(setter)和获取器(getter): photo.caption = @”Day at the Beach”; NSString* myString = photo.caption;

两种语法你可以使用任何一种,但是在一个项目中最好只使用一种。同时,点语法只能使用在设置器(setter)和获取器(getter)上,而不能用于普通方法。 7、alloc方法它的作用是分配内存及实例化一个对象

8、init方法通常做对象的初始化设置工作,比如创建实例变量

9、在Objective-C的语法中,创建一个类是非常简单的。一个类通常分为两部分。 类的接口(interface)通常存放在类似ClassName.h的文件中,在这里,我们定义实例变量和公用(public)方法。

类的实现存放在ClassName.m这样的文件中,它包含了这些方法的实际实现代码。它通常还定义了客户类不能访问的私有(private)方法。 10、Objective-C语言中通常省略设置器方法的“get”前缀。 方法名字前面的单个减号(-)表明该方法是一个实例方法。

如果方法名字前面是一个加号(+),则表明该方法是一个类(static)方法

Phone项目新成立,也没有编码规范的积累,项目组本来是想拿老的C编码规范套用的,但评审一下就发现问题多多,之后找到了Google的Objective-C的编码规范,大家就先翻译一下咯

声明这是无版权翻译,也不对任何错误负责,不保证文章的完整性,我到现在也认不全语法。

(大半年的事后,决定对这份文档做重审,当然不是对修辞手法,而是处理内部的硬伤)

总览

背景知识

Objective-C是一个C语言的扩展语言,非常动态,非常的“面向对象”,它被设计成既拥有复杂的面向对象设计理念又可以轻松使用与阅读的语言,也是Mac OS X和iPhone开发的首选语言。

Cocoa是Mac OS X的主要应用框架,提供迅速开发各种功能的Mac OS X应用的Objective-C类集合。

Apple已经有一个很好也被广泛接受的Objective-C的编码规范,Google也有类似的C++编码规范,这份Objective-C编码规范很自然是Apple和Google的共同推荐的组合。所以,在阅读本规范前,确保你已经阅读了:

Apple's Cocoa Coding Guidelines Google's Open Source C++ Style Guide

注意所有已在Google的C++编码规范里的禁用条款在Objective-C里也适用,除非本文档明确指出反对意见。

本文档旨在描述可供可适用于所有Mac OS X代码的Objective-C(包括Objective-C++)编码规范和实践。规范中的许多条款已经改进也不断的被其他的项目和团队所证明其指导性。Google的相关开源项目都遵守此规范。

Google已经发布了一份作为Google Toolbox for Mac project (文档中简称为GTM)的组成部分的遵守本规范的开源代码。这份开放代码也是本文很好的例证(原文看不太懂-

-Code meant to be shared across different projects is a good candidate to be included in this repository. )

注意本文不是Objective-C的教学指南,我们假设读者已经了解语言。如果你是一个Objective-C的初学者或需要重温,请阅读The Objective-C Programming Language .

示例

人们说一个例子胜过千言万语,所以就让我们用例子来让你感受以下编码规范的风格,留间距,命名等等。

下例是一份头文件,展示对@interface 声明正确的注释和留间距

Java代码

1. // GTMFoo.h 2. // FooProject 3. //

4. // Created by Greg Miller on 6/13/08.

5. // Copyright 2008 Google, Inc. All rights reserved. 6. // 7.

8. #import 9.

10. // A sample class demonstrating good Objective-C style. All interfaces, 11. // categories, and protocols (read: all top-level declarations in a header)

12. // MUST be commented. Comments must also be adjacent to the object they're

13. // documenting. 14. //

15. // (no blank line between this comment and the interface) 16. @interface GTMFoo : NSObject { 17. @private

18. NSString *foo_; 19. NSString *bar_; 20. } 21.

22. // Returns an autoreleased instance of GMFoo. See -initWithString: for detai

ls

23. // about the argument.

24. + (id)fooWithString:(NSString *)string; 25.

26. // Designated initializer. |string| will be copied and assigned to |foo_|.

27. - (id)initWithString:(NSString *)string; 28.

29. // Gets and sets the string for |foo_|. 30. - (NSString *)foo;

31. - (void)setFoo:(NSString *)newFoo; 32.

33. // Does some work on |blah| and returns YES if the work was completed 34. // successfuly, and NO otherwise.

35. - (BOOL)doWorkWithString:(NSString *)blah; 36. 37. @end

view plainprint?

1. // GTMFoo.h 2. // FooProject 3. //

4. // Created by Greg Miller on 6/13/08.

5. // Copyright 2008 Google, Inc. All rights reserved. 6. // 7.

8. #import 9.

10. // A sample class demonstrating good Objective-C style. All interfaces, 11. // categories, and protocols (read: all top-level declarations in a header)

12. // MUST be commented. Comments must also be adjacent to the object they're 13. // documenting. 14. //

15. // (no blank line between this comment and the interface) 16. @interface GTMFoo : NSObject { 17. @private

18. NSString *foo_; 19. NSString *bar_; 20. } 21.

22. // Returns an autoreleased instance of GMFoo. See -initWithString: for detai

ls

23. // about the argument.

24. + (id)fooWithString:(NSString *)string; 25.

26. // Designated initializer. |string| will be copied and assigned to |foo_|. 27. - (id)initWithString:(NSString *)string; 28.

29. // Gets and sets the string for |foo_|. 30. - (NSString *)foo;

31. - (void)setFoo:(NSString *)newFoo; 32.

33. // Does some work on |blah| and returns YES if the work was completed 34. // successfuly, and NO otherwise.

35. - (BOOL)doWorkWithString:(NSString *)blah; 36. 37. @end

下例是一份源文件,展示对接口的@implementation 的实现的正确注释和留间隔。它也包括了主要方法如getters,setters,init ,和dealloc 的相关实现。

Java代码

1. //

2. // GTMFoo.m 3. // FooProject 4. //

5. // Created by Greg Miller on 6/13/08.

6. // Copyright 2008 Google, Inc. All rights reserved. 7. // 8.

9. #import \ 10. 11.

12. @implementation GTMFoo 13.

14. + (id)fooWithString:(NSString *)string {

15. return [[[self alloc] initWithString:string] autorelease]; 16. } 17.

18. // Must always override super's designated initializer.

Objective c学习笔记

Objective-C学习1、Objective-C是C的超级,是完全面向对象的语言,而C是面向过程的语言,具体体现在是要求对象做事,而不是仅仅的调用函数2、Objective-C中调用方法被成为发送消息3、Objective-C引用头文件使用#import与C中是用#include选项相同,区别在于#import会自动跳过已经添加的文件,不会因为重复添加
推荐度:
点击下载文档文档为doc格式
5i80z2kt4617c19373fh7l7tx29ybm00g6t
领取福利

微信扫码领取福利

微信扫码分享