`
renzhelife
  • 浏览: 669808 次
文章分类
社区版块
存档分类
最新评论

Objective-C学习笔记之一

 
阅读更多

/******by garcon1986********/

int 整型
float 单精度
long 常整型
double 双精度


为什么编译器程序要求声明变量类型?这是因为计算 机程序需要占用部分内存。编译器程序要为每个变量预留出内存空间。不同的数据类型,也就是int型和float型,需要不同的存储空间和代码,编译器程序要预留出足够的空间并使用正确的代码。


% 取余只对整数有效

main() 主函数是程序执行的起点

并不是所有的函数都会 返回一个数值。如果一个函数不返回结果,那么它应该被定义为“void”。“return”语句就成了可选的

函数声明
float circleArea(float theRadius); // function declaration
int main(){ /**/}
float circleArea(float theRadius) // [10.13]
{ /*函数体*/}

函数中的代码只在函数中有效。这是Objective-C的一个重要特点。


反斜杠“/”叫做转意字符(escape character)

如果希望编写的程序也能在Terminal上运行,需要你确保工程的名称只有一个单词组成,习惯上还不可以使用大写字母。而为图形界面编写的程序的名字一般以大写字母开头。

iphone application demo link:
http://itunes.apple.com/us/app/servicemagic-home-improvement/id326666113?mt=8

= = 等于
> = 大于等于
> 大于
< = 小于等于
< 小于
! = 不等于
&& 与
|| 或

在Objective-C语言中,真和假分别以1和0代替。它们属于一个特殊的数值类型——布尔型数值 (BOOL)。“真”可以用1或者YES代替;“假”可以用0或者“NO”代替。

Objective-C语言把抽象的概念当作对象。

.h file and .m file:
The file that ends in ".h" is the Header or Interface file for the class.
The one that ends in ".m" is the Main or Implementation file for the class.

The Application Delegate class that is included with every new Cocoa application.


最主要的类:

King of the hill, among classes, is the class named NSObject . Nearly all the classes you will ever create or use will be subclasses of NSObject , directly or indirectly.

By convention, all class names begin with one or more capital letters, and they may not contain any spaces.
Once you start writing your own applications, we recommend that you choose a two or three letter prefix that you'll use for all your classes in order to avoid confusion with existing class names.
However, don't use NS, as this may confuse you later on. NS is reserved for Apple's classes. It stands for NeXTStep, NeXTStep being the operating system Mac OS X was based on when Apple bought NeXT, Inc.
防止类命名冲突的列表:http://www.cocoadev.com/index.pl?ChooseYourOwnPrefix
select the Identity button in the Inspector palette (Cmd-6)

Remember: object instances themselves don't contain the code to perform the action, but the classes do.

#import <Foundation/Foundation.h> The former is for non-GUI apps,
#import <cocoa/cocoa.h> the latter for GUI-apps.

处于支配地位的是类NSObject

-( void ) setIntValue:( int ) anInt )
In Objective-C, the minus sign marks the beginning of an instance method declaration (as opposed to a class method declaration, which we'll talk about later). void indicates that nothing is returned to the invoker of the method. That is, when we send a setIntValue: message to textField , our MAFoo object does not receive a value back from the text field object. That is ok. After the colon, ( int ) indicates that the variable anInt must be an integer. In our example, we send it a value of 5 or 0 , which are integers, so we are fine.

Accessors
Setter

在函数和方法中的所有变量只在这个函数和方法中有效

polymorphism 多态: 方法在类中是唯一的,不同的类可以有不同的方法名;

“基 本工具”(Foundation Kit)提供了大部分与非图形界面编程相关的服务。另外一个架构被称做“应用程序工具”(Application Kit),用于处理图形界面编程的各种对象。

All the information for the window is stored in a nib file (nib stands for NeXT Interface Builder).

awakeFromNib :When the window is opened, the awakeFromNib method is called automatically.

A pointer is simply a variable that contains the address of another variable. 指针存储变量的地址;指针本身也是一个变量;

使用&来引用变量的地址;而不是变量的值。 这个地址是在内存中的一个数字;指示出变量的位置。
when the computer evaluates the expression &x , it returns the address of the variable x , not the value stored there. The address is a number that denotes a specific place in the memory of the computer (like a room number denotes a specific room in a hotel).

由 类NSString创建的字符串被叫做固定字符串,因为它不可修改。

in Objective-C, objects are never manipulated directly, but always through pointers to them.

Objects are always manipulated through pointers

Multiple variables can reference the same object

The way to make instance variables available is to create a pair of methods to access and modify the instance variable. The methods are collectively known as accessor methods .

Because methods and variables have separate namespaces , no conflict would arise.

Essentially, properties synthesize accessors directly, using the most efficient and appropriate memory management.

启动garbage collection:Garbage collection is needs to be enabled, as it is off by default in a new Xcode project. To enable it, select your Target app from the Source list, and open the Inspector. Put a tick next to the "Enable Objective-C Garbage Collection" item.

ojbect的增加和释放:
[ anObject release] ;
[ anObject release] ;
自动释放:
[ anObject autorelease] ;

Whereas the Cocoa frameworks are designed for application development for Mac OS X desktop and notebook computers, the Cocoa Touch frameworks are for applications targeted for the iPhone and iTouch.
Both Cocoa and Cocoa Touch have the Foundation framework in common. However, the UIKit replaces the AppKit framework under Cocoa Touch, providing support for many of the same types of objects, such as windows, views, buttons, text fields, and so on.


Bundle:包含可执行程序和相关资源的文件夹。

程序,框架,插件等软件都是bundle。

Iphone OS中,只有程序能被第三方开发。

nib文件:interface builder文件。 使用loadNibNamed:owner或thereof来load文件。

info.plist包含运行时配置文件(information property list)

本地化文件放在en.lproj 或fr.lproj等之中。

Objective-C 是ANSI C的扩展语言,它面向对象,是cocoa编程的native语言。

类至少包括两部分: 公开的接口和私有的实现。

interface通常有.h后缀(header头文件)

implementation通常有.m后缀(包含源代码)

这两者类似于c语言的 math.h 和math.c 文件。

protocol声明了一个人和类可以实现的接口。

cmd + R 来运行程序,相当于windows的f5

一般c程序通过gcc编译。

gcc test.c -o test 将编译出一个叫test的程序。 -o用来指定程序名字

一般情况下,如果一个程序在mac os x编译好,不能再linux中直接运行,需要重新编译。

scripting语言中, 变量$variable = 2; 而在C语言中需要声明, int variable = 2;

如何在xcode中show line numbers? xcode preferences -> text editing -> show line number

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics