首页 > 其他 > 详细

OC学习手札之基础语法

时间:2015-11-12 17:43:38      阅读:334      评论:0      收藏:0      [点我收藏+]

Step by step !

1.NS常用的缩写

Prefix

Frameworks

AB

AddressBook / AddressBookUI

AC

Accounts

AD

iAd

AL

AssetsLibrary

AU

AudioUnit

AV

AVFoundation

CA

CoreAnimation

CB

CoreBluetooth

CF

CoreFoundation / CFNetwork

CG

CoreGraphics / QuartzCore / ImageIO

CI

CoreImage

CL

CoreLocation

CM

CoreMedia / CoreMotion

CV

CoreVideo

EA

ExternalAccessory

EK

EventKit / EventKitUI

GC

GameController

GLK

GLKit

JS

JavaScriptCore

MA

MediaAccessibility

MC

MultipeerConnectivity

MF

MessageUI

MIDI

?CoreMIDI

MK

?MapKit

MP

?MediaPlayer

NK?

?NewsstandKit

NS?

?Foundation, AppKit, CoreData

PK

?PassKit

QL

?QuickLook

SC

?SystemConfiguration

Se

?Security

SK

?StoreKit / SpriteKit

SL

?Social

SS

?Safari Services

TW

?Twitter

UI?

?UIKit

UT

?MobileCoreServices

?
?
2.C语言与OC对比
技术分享技术分享
技术分享
?
?
3.常用关键字一览

@interface、@implementation、@end
@public、@protected、@private、@selector
@try、@catch、@throw、@finally
@protocol、@optional、@required、@class
@property、@synthesize、@dynamic
BOOL Class SEL YES NO id self super nil atomic nonatomic retain assign copy block …

4.方法调用以及基础语法

#import <Foundation/Foundation.h>

?

// - 表示动态方方法

// +表示静态方法

@interface TestClass : NSObject

{

@public

? ? //下划线开头

? ? NSString * _name;

? ? int _age;

}

?

@property NSString * NickName;

?

+(void) staticSayHello;

-(void) SayHello;

?

+(double) staticTestWithParams:(int) num ThisIsWord:(int) num2;

?

@end

?

//实现上文中的方法才可以调用

@implementation TestClass

?

//实现构造函数

-(id)init

{

? ? if (self = [super init]) {

? ? ? ? NSLog(@"初始化构造函数...");

? ? }

? ? returnself;

}

-(id)initWithNickname:(NSString*)nickName

{

? ? if (self = [super init]) {

? ? ? ? NSLog(@"带有参数的构造 %@",nickName);

? ? }

? ? returnself;

}

-(id)initAgeAndNickname:(int)age andName :(NSString*)name

{

? ? if (self =[super init]) {

? ? ? ? NSLog(@"带有双参数的构造 %d +++ %@",age,name);

? ? }

? ? returnself;

}

?

?

-(void) SayHello

{

? ? NSLog(@"Name is %@ and age is %d",_name,_age);

}

+(void)staticSayHello{

? ? NSLog(@"Static Call SayHello must call by Class ..");

}

+(double)staticTestWithParams:(int)num ThisIsWord:(int) num2

{

? ? NSLog(@"%d +========+++=%d",num,num2);

? ? return num;

}

@end

?

int main(int argc, const char * argv[]) {

?? ?

? ? TestClass* test = [[TestClassalloc] initAgeAndNickname:21andName:@"xiaonian"];

?? ?

? ? test->_name = @"Hello World";

? ? test->_age = 21;

?? ?

? ? [test SayHello];//Call Method

? ? [TestClassstaticSayHello];//Casll Static Method

? ? [TestClassstaticTestWithParams:123456ThisIsWord:890123];

?? ?

? ? NSLog(@"--------分隔符---------");

?? ?

? ? Class c = [TestClass class];

? ? NSLog(@"%p",c);

? ? //0x100004708

?? ?

? ? NSLog(@"--------分隔符-@SEL--------");

? ? SEL sel = @selector(SayHello);

? ? [test performSelector:sel];

? ? [test performSelector:@selector(SayHello)];

? ? if ([test respondsToSelector:sel]) {

? ? ? ? NSLog(@"%@",@"可以相应sel--sayhello");

? ? }

? ? NSLog(@"--------分隔符-@property--------");

?? ?

? ? [test setNickName:@"xiaonian"];

? ? NSString* nickName = [test NickName];

? ? test.NickName = @"xiaonian2";

? ? NSString* nickName2 = test.NickName;

? ? NSLog(@"%@ and %@",nickName,nickName2);

?? ?

? ? return 0;

}

?
?
8.Self == C# this
9.不支持方法重载
10.继承 多态 与C#一致
11.类的本质还是一个类对象,有点像C#的所有类都归于obejc
? ? (1)类对象使用起来就跟一个实例一样,可以去new这个类对象用原类接收
? ? (2)类对象直接可以当作以及被new出来的对象调用内部方法
?

? ??Class c = [TestClass class];

? ? NSLog(@"%p",c);

? ? //0x100004708

?
?12.@selector(SEL)类型

? ? SEL:全称selector 一种用来表示方法名类型的数据类型(方法名)。

? ? SEL类型作用: 1)可以定义变量 ? ? ? 2)可以用来作为方法的形参 ? ?3)可以用来作为方法的实参

? ? 类中方法存储的原理: 1)类里面的方法都是被转换成SEL变量进行存储的。 2)当类声明一个对象,对象调用方法的时候,系统会把这个方法转换成SEL,然后拿这个SEL到类 方法中去匹配。

? ?

? ??SEL sel = @selector(SayHello);

? ? [test performSelector:sel];

? ? [test performSelector:@selector(SayHello)];

?

13.@property 只能在@interface中定义

? ??[test setNickName:@"xiaonian"];

? ? NSString* nickName = [test NickName];

? ? test.NickName = @"xiaonian2";

? ? NSString* nickName2 = test.NickName;

? ? NSLog(@"%@ and %@",nickName,nickName2);

?

?

14.关于自定义构造函数必须以init开头,在@implement中实现如下

-(id)init

{

? ? if (self = [super init]) {

? ? ? ? NSLog(@"初始化构造函数...");

? ? }

? ? returnself;

}

-(id)initWithNickname:(NSString*)nickName

{

? ? if (self = [super init]) {

? ? ? ? NSLog(@"带有参数的构造 %@",nickName);

? ? }

? ? returnself;

}

-(id)initAgeAndNickname:(int)age andName :(NSString*)name

{

? ? if (self =[super init]) {

? ? ? ? NSLog(@"带有双参数的构造 %d +++ %@",age,name);

? ? }

? ? returnself;

}

?

?

?

?

OC学习手札之基础语法

原文:http://www.cnblogs.com/Keyle/p/4959338.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!