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 |
|
UI |
UIKit |
UT |
MobileCoreServices |
@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;
}
Class c = [TestClass class];
NSLog(@"%p",c);
//0x100004708
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;
}