Classes
Objective-C is an object-oriented language:it manages classes and objects.Objective-C uses a strict object model.
strict |strɪkt| adj 严厉的、明确的、精确的、严格的
Like in C++, it is useful to split the code between interface and implementation for each class.
split |splɪt| v 切开、劈开
Objective-C uses .h files for heads,and .m files for the code.
Objective-C introduces the #import directive to replace #include. Indeed,every C header should use compilation guards to prevent multiple inclusions. This is automatic when using #import.
introduce |ˌɪntrəˈdjuːs, American -ˈduːs| v 介绍、引出、推行
indeed |ɪnˈdiːd| 实际上
compilation |ˌkɒmpɪˈleɪʃn| noun 编辑、编写、撰写、编制
guard |gɑːd| noun 警卫、警戒
prevent |prɪˈvent| v 阻止
multiple |ˈmʌltɪpl| noun 倍数、连锁店 adj 多个部分组成的、多处的、多种的、多重的
inclusion |ɪnˈkluːʒn| noun 包括
header file 头文件
#import keeps track of which headers have already been included and is ignored if a header is imported more than once in a compilation unit. This makes it unnecessary to use header guards.
track |træk| noun 足迹、车辙
necessary |ˈnesəsəri, American -serɪ| adjective 必要的、必需的
Below is a typical interface/implementation example.The Objective-C syntax is explained later.
syntax |ˈsɪntæks| noun 句法、[计算机语言的] 句法
explain |ɪkˈspleɪn| v 解释、说明
C++:
//In file Foo.h
#ifndef __FOO_H__ //compilation guard
#define __FOO_H__
class Foo
{
…
};
#endif
//In file Foo.cpp
#include “Foo.h”
….
Objective-C:
//In file Foo.h
@interface Foo:NSObject
{
…
}
@end
//In file Foo.m
#import “Foo.h”
@implementation Foo
…
@end
1.Class Definition 类的声明
Interface Section 接口部分(.h文件)
General Format:
@interface className : parentClass<protocol,…>//OC不支持多继承,可以通过支持多个协议的方法来实现相似的功能。
{
instanceVariable Declarations;
}
method Declarations
…
@end
Objective-C defines a root class:NSObject.Every new class should be a descendant of the NSObject class.It provides a huge number of facilities for the run-time system.
descendant |dɪˈsendənt| noun 后代
facility |fəˈsɪləti| noun 场所、设施、便利、才能
Strictly speaking,every object should be of type NSObject,and every pointer to an object could be declared as NSObject*. In face, one can use the type id instead. This is a short and handy way to declare a pointer to any object, and provides dynamic type-checking instead of static type-checking.It is very useful for some weak typing on generic methods.
strictly |ˈstrɪktli| adverb 严格地、绝对地
handy |ˈhændi| adjective 好用的、近便的
generic |dʒɪˈnerɪk| adj 普通的
Method Declarations
General Format
#import ””
mType (returnType) name1:(type1)param1 name2:(type2)param2…
The method name1:name2:… is declared, which returns a value of type returnType and has formal parameters param1,param2,…param1 is declared to be of type type1,param2 is declared to be of type2,and so on.
formal |ˈfɔːml| adjective 正式的、正规的
name2 and type2 can be omitted(omit |əˈmɪt| v 删除、疏忽).
[self testFun:@"one" :@"two"];//onetwo__NSCFConstantString
- (void)testFun:(NSString*)str1 :str2
{
NSLog(@"%@%@%@",str1,str2,NSStringFromClass([str2 class]));
}
Colons(:) are parts of the method name.
colon |ˈkəʊlən,-lɒn| noun 冒号
If myType is +,a class method is declared,but if myType is -, an instance method is declared.
iOS中id与NSObject* 和id<NSObject>的区别
参考:http://www.devdiv.com/ios_id_nsobject_id_lt_nsobject_gt_-blog-1-50764.html
1. 我们来看看id的定义,它就是一个指针,一个结构体指针,它可以指向的类型不仅限于NSObject
/// An opaque type that represents an Objective-C class.
typedef struct objc_class *Class;
/// Represents an instance of a class.
struct objc_object {
Class isa OBJC_ISA_AVAILABILITY;
};
/// A pointer to an instance of a class.
typedef struct objc_object *id;
2. NSObject*就是 NSObject类型的指针了,它范围较小。
@interface NSObject <NSObject> {
Class isa OBJC_ISA_AVAILABILITY;
}
3. id<NSObject>是指针,它要求它指向的类型要实现NSObject protocol,
iOS中很多类定义很奇葩,类名叫NSObject,定义个接口也叫NSObject,但是它俩不是一个东东。
而NSObject类实现了NSOject接口,所以id<NSObject>可以指向NSObject的对象。
如果我们来看看NSProxy的定义,你会发现,它不是继承自NSObject,但是却实现了NSObjecct接口,
NSProxy定义类似这样
@interface NSProxy <NSObject> {
Class isa;
}
所以id<NSObject>可以指向NSProxy的对象。