• OC学习手札之基础语法


    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对比
    NewImageNewImage
    NewImage
     
     
    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;

    }

     

  • 相关阅读:
    实验十一 集合
    实验十 泛型程序设计技术
    实验九 异常、断言与日志
    实验六 接口的定义与使用
    实验七 继承附加实验
    实验六 继承定义与使用
    《面向对象程序设计(Java)》第四周学习总结
    实验三 Java基本程序设计(2)
    201771010112罗松《面向对象程序设计(java)》第十一周学习总结
    201771010112罗松《面向对象程序设计(java)》第十周学习总结
  • 原文地址:https://www.cnblogs.com/Keyle/p/4959338.html
Copyright © 2020-2023  润新知