Objective-C 知识点总结

 

一、objective-c 基础

OC是C语言的超集,是在C语言的基础上添加了面向对象的特性。

1. 与C语言相同的部分

基本数据类型,数组,流程控制,指针,结构体,枚举,函数指针

2. 方法声明与调用

方法声明与C语言有很大差别,oc中的方法都是在类中声明的,用 - 表示实例方法,用 + 表示类方法,

返回值用 (typename)表示,方法名和参数用 : 分隔,而且 : 是方法名的一部分(部分方法名),不同参数用空格分隔,

(Methods and properties for the class are declared next, before the end of the class declaration. The names of

methods that can be used by class objects, class methods, are preceded by a plus sign)

+(instancetype) alloc;

(The methods that instances of a class can use, instance methods, are marked with a minus sign)

如:

-(void)removeObjectAtIndex: (NSInteger)index withFlag:(BOOL)flag;

调用时用 [instance method],称为:向instance发送 method 消息。

注意:Class methods can’t refer directly to instance variables.

3. 面向对象的语法支持

类(Class Interface):用interface声明一个类,实例化后的对象都是指针

The declaration of a class interface begins with the compiler directive @interface and ends with 

the directive  @end. (All Objective-C directives to the compiler begin with“@”.)

            声明:在 .h 文件中,

@interface ClassName : SuperClass
// properties
// methods
@end

            实现(Class Implementation):在 .m 文件中

The definition of a class is structured very much like its declaration. It begins with an @implementation directive and ends with the @end directive. In addition, the class may declare instance variables in braces after the @implementation directive:                 

@implement ClassName
// method implementations
@end

协议(protocol):相当于Java的接口,只定义方法, 遵守此协议的类需要实现这些方法

            声明:                 

@protocol ProtocolName
// method list
@end

            遵守协议: @interface ClassName : SuperClass <ProtocolName1, ProtocolName2>

optional 的方法是可选的,遵守协议时可以不用实现这些方法;required 的方法是必须要实现的,默认是 required 的

继承(Inheritance):定义类的时候可以用 :来表示继承关系,oc只能单继承,一般定义一个类都要但是一个类可以实现多个协议,可以通过这种方式来实现多继承。

多态(polymorphism):动态绑定;不同的对象能够以自己的方式响应相同消息的能力;子类可以重写父类的方法。

The ability of different objects to respond, each in its own way, to identical messages is called polymorphism .

隐藏变量(self, super, _cmd):实例对象可以用 self 表示自己,super 可以调用父类方法。类方法中的 self 表示类本身。每一个方法内都有一个_cmd,表示方法自身,它是 SEL 类型,可以使用 NSStringFromSelector 方法得到 _cmd 的字符串表达。

注意:Within the body of a class method, self refers to the class object itself. 类方法中 self 代表类本身。

类别(category):类别用来给一个类添加新方法,不可以给类添加成员变量。在声明类的时候,在类名后面添加一对括号,其中指定类别的名字。

(1)类别只能添加新方法,无法添加新的实例变量

(2)如果类别名和原来类中的方法产生名称冲突,则类别将覆盖原来的方法,因为类别具有更高的优先级。

扩展(Extension):与类别相似,但是也有一些差别。

类的扩展和类别一样,在类名后添加一对括号,但扩展不需要名字。类的扩展中,可以添加实例变量。

通常,把类的私有变量和私有方法放在扩展中,以此来隐藏信息。

类的扩展必须放在该类的实现文件中,并且要实现扩展中声明的方法。而类别是可以放在一个单独的文件中声明和实现的。

作用域(Scope):To enforce the ability of an object to hide its data, the compiler limits the scope of instance variables—that is,

limits their visibility within the program. But to provide flexibility, it also lets you explicitly set the scope at four

levels. Each level is marked by a compiler directive:

 

5. 方法选择器

用 SEL 可以声明一个方法: SEL sel = @selector(methodName), 可以当做参数传递给一个方法

[myRectangle setOriginX: 30.0 y: 50.0];

A selector name includes all the parts of the name, including the colons, so the selector in the preceding example is named setOriginX:y:. It has two colons, because it takes two parameters. The selector name does not, however, include anything else, such as return type or parameter types.

eg:

SEL setWidthHeight;
setWidthHeight = @selector(setWidth:height:);
setWidthHeight = NSSelectorFromString(aBuffer);

5. 属性与属性修饰词

类可以有若干个属性,用@property 来声明,如:@property ( nonatomic, assign)NSInteger score;

这样该类就有一个私有变量 _score,并且自动生成了 getter 和 setter。

可以给属性指定若干个修饰词,用逗号隔开。

如: notatomic, copy, weak, strong, assign, unsage_unretained, readwrite, readonly,setter=name,getter=name 等等。

注意:没有与 nonatomic 对应的 atomic,没有 指定 nonatomic 的默认都是 atomic

6. id 和 instancetype 类型

typedef struct objc_object {
    Class isa;
} *id;

id是一个可以指向任意对象的指针,能够用来声明变量类型,可以作为函数返回值;而instancetype只能作为函数的返回值类型。

7. 导入头文件(Importing the Interface)

OC中用 #import "header.h" 导入类的头文件,类似于C语言中的 #include。

#import 能够防止重复导入。

除了直接import其他类的头文件来引用该类,还可以用 @class  className 来告诉编译器这是一个类。

@class Rectangle, Circle;

This directive simply informs the compiler that “Rectangle” and “Circle” are class names. It doesn’t import their interface files.

8. 块(block)

block类似函数指针,可以定义一个匿名函数。

void (^blockName)(int, int) = ^(int, int){ }
// use typedef
typedef CGFloat (^MFConnectionDidCloseBlock)(NSString *)

block代码块中可以访问block之外的变量,但是不能修改,如果声明变量时添加 __block ,则可以在block中修改变量。

9. 类型编码(Type Encodings)

可以使用 @encode() 这个编译指令来过去一个基本类型的字符串表达式。

To assist the runtime system, the compiler encodes the return and argument types for each method in a character string and associates the string with the method selector. The coding scheme it uses is also useful in other contexts and so is made publicly available with the @encode() compiler directive. When given a type specification, @encode() returns a string encoding that type. The type can be a basic type such as an int, a pointer, a tagged structure or union, or a class name—any type, in fact, that can be used as an argument to the C sizeof() operator.

   

   

    

 

10. 键值编码(Key-value coding)

Key-value coding is a mechanism for accessing an object’s properties indirectly, using strings to identify properties, rather than through invocation of an accessor method or accessing them directly through instance variables. In essence, key-value coding defines the patterns and method signatures that your application’s accessor methods implement.

NSKeyValueCoding 类中对 NSSet, NSOrderedSet, NSMutableDictionary, NSDictionary, NSArray,  NSObject 用类别添加了

键值编码的方法。

A key is a string that identifies a specific property of an object. Typically, a key corresponds to the name of an accessor method or instance variable in the receiving object. Keys must use ASCII encoding, begin with a lowercase letter, and may not contain whitespace.

A key path is a string of dot separated keys that is used to specify a sequence of object properties to traverse. The property of the first key in the sequence is relative to the receiver, and each subsequent key is evaluated relative to the value of the previous property.

如果 valueForKey:  指定的 key 不存在,那么默认会引发一个 NSUndefinedKeyException 的异常;子类也可以重写

 valueForUndefinedKey: 方法来修改默认的行为。

键值编码一致性的要求:

Accessor Search Implementation Details

Collection Operators

Collection operators allow actions to be performed on the items of a collection using key path notation and an action operator. This article describes the available collection operators, example key paths, and the results they’d produce.

Collection operators are specialized key paths that are passed as the parameter to the valueForKeyPath: method. The operator is specified by a string preceded by an at sign (@). The key path on the left side of the collection operator, if present, determines the array or set, relative to the receiver, that is used in the operation.

The key path on the right side of the operator specifies the property of the collection that the operator uses.

11. 键值监听(Key-Value Observing)

Key-value observing is a mechanism that allows objects to be notified of changes to specified properties of

other objects.It is particularly useful for communication between model and controller layers in an application.

There are three steps to setting up an observer of a property. Understanding these three steps provides a clear

illustration of how KVO works.

-(void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath
options:(NSKeyValueObservingOptions)options context:(void *)context;

向一个对象发送此消息,那么就会给该对象添加一个监听者observer,同时,observer 中要实现:

-(void)observerValueForKeyPath:(NSString *)keyPath ofObject:(id)object 
change:(NSDictionary *)change context:(void *)context;

这样,被监听的对象的属性发生改变时就会调用这个方法,来通知 observer。

12. 单例

In a more sophisticated implementation, you can declare a variable to be static, and provide class methods to manage it. Declaring a variable static limits its scope to just the class—and to just the part of the class that’s implemented in the file. (Thus unlike instance variables, static variables cannot be inherited by, or directly manipulated by, subclasses.) This pattern is commonly used to define shared instances of a class (such as singletons; see “Creating a Singleton Instance” in Cocoa Fundamentals Guide ).

static MyClass *MCLSSharedInstance;
@implementation MyClass
+ (MyClass *)sharedInstance
{
    // check for existence of shared instance
    // create if necessary
     return MCLSSharedInstance;
}
// implementation continues

 

13. 不显示警告

   未使用的警告:__unused

   方法deprecated的警告:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations”
#pragma clang diagnostic pop


二、Apple提供的框架

1. Foundation

2. UIKit

3. CoreGraphics

4.CoreFoundation

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值