zoukankan      html  css  js  c++  java
  • [IOS笔记] objecttivec基础知识

    NS类

    NSString

    NSString - @"", 像常量字符串,不可修改

    NSMutableString - 可以修改字符

    NSMutableString *string = [NSMutableString stringWithCapity :42]; //同C++中容器类似,容量仅是最优值,字符串的大小并不仅限于容量值

    NSMutableString *string = [NSMutableString stringWithFormat : @"%d", 2];

    [string appendString : @""]  [string appendFormat : @"%d", 1] [string deleteCharactersInRange: [string rangeOfString : @"需要删除的字符"]]

    NSLog(@"%@", object); //其实调用了object的description方法

     

    NSArray
    NSArray - 不能放置基本类型,nil表示结束,

    NSArray *array = [NSArray arrayWithObjects : @"one", @"two", nil];

    NSString two = [array objectAtIndex : 1];

    NSMutableArray - 可变数组
    NSMutableArray *array = [NSMutableArray arrayWithCapity :42];

    [array addObject : object]; //添加

    [array removeObjectAtIndex : 1]; //删除

    排序 sortUsingSelector

     

    NSEnumerator

    NSEnumerator - 枚举,通常用于对数组的遍历操作

    NSEnumerator *e = [array objectEnumerator];

    id thingie;
    
    while (thingie = [e nextObject]){ //nextObject请求对象,直到nil
    
      NSLog(@"%@", thingie);
    
    }
    //或者使用 for in
    for (NSString *string in array){
    }

    NSDictionary

    NSDictionary - key_value

    NSDictionary *nd = [NSDictionary dictionaryWithObjectsAndKeys : value1, @"1key", value2, @"2key", nil];

    [nd objectForKey :@"1key"]; //返回value1

    NSMutableDictionary - 可修改的key_value

    NSMutableDictionary *ns = [NSMutableDictionary dictionary];

    NSMutableDictionary *ns = [NSMutableDictionary dictionaryWithCapacity : 11];

    [ns setObject : value1, forKey: @"key1"]; //添加

    [ns removeObjectForKey : @"key1"]; //删除

     

    NSSet

    同C++的Set类似

    NSSet *ns = [NSSet setWithObjects : @"one", @"two", nil];

    NSMutableSet *ns = [NSMutableSet setWithObjects : @"one", @"two", nil];

     

    NSNumber

    NSNumber *n = [NSNumber numberWithInt :42]; //numberWitnChar等

    [n intValue]; //返回42

     

    NSNull

    +(NSNull *) null; 用来替代nil放入集合中。可以用if (o == [NSNull null]) 进行比较

     

    NSFileManager

    NSFileManager *nsf = [NSFileManager defaultManager];

     

    接口部分 .h文件

    @class otherClassName //同C++,当ClassName需要使用到otherClassName而不用调用它的类方法和数据时,进行声明。和#import "otherClassName.h"相比,不需要编译器预处理.h。
    @interface
    ClassName:superClass //不支持多继承 {   //数据
      @public
      @private
      @protected
    }
    //类中不可有static静态变量,方法或源文件中可以声明和定义
    //方法 //setter方法
    -(void)setxxx : ...;
    //getter方法
    -(returntype) xxx;
    //可以使用 @property xxx,yyy指令生成setter和getter方法

    //实例方法 -(returntype) functionname : (firstparametertype) firstparametername, second :(secondparameterype) secondparametername... //类方法,不可访问实例变量 +(returntype) functionname : (firstparametertype) firstparametername, second :(secondparameterype) secondparametername...
    @end

    实现部分 .m文件

    @implementation ClassName
    //可以使用 @synthesize xxx,yyy指令生成setter和getter方法
    //方法实现 -(returntype) function : (firstparametertype) firstparametername second :(secondparametertype) secondparametername { } -(id)int //id为泛型对象指针,可以只想所有对象 {   if(self=[super init]){   } //如果父类可以完成所需的一次性初始化,调用[super init]   return (self) }
    -(void)dealloc 
    {
      //todo your code
      [super dealloc];
    }
    //可以实现在interface中无声明的方法,此方法为private,只可以在类中使用。 @end

     调用数据

    instance.property

    调用方法

    [ClassName function : firstparameter second : seconderparameter]

    创建实例

    [ClassName : new]

    [[ClassName alloc] init]

    分类

    分类向类中添加方法,不可以定义实例变量,可以不必实现分类中的所有方法,可以覆盖原类中的方法,但不建议

    @interface ClassName (yourCategory)

    @end

    @implementation ClassName (yourCategory)

    @end

    协议

    类似java的interface

    @protocol protocolName <superProtocol> //扩展协议
    
    //协议方法
    
    @optional
    
    @end

     

    类采用协议,要实现协议中的方法

    @interface ClassName:  superClass <protocolName, protocolName2>

    @end

    可以使用conformsToProtocol: 检查一个对戏哪个是否遵循某项协议

    id currentObject;
    ...
    if ([currentObject conformsToProtocol: @protocol(protocolName)] == YES){
    //使用@protocol指令回去一个协议名称,并产生一个协议对象
    }

    id <protocolName, protocolName1> currentObject;

    告诉编译器,此变量遵守protocolName和protocolName协议,如果不遵守,编译器将发出警告信息

    类型判断

    - (BOO) isKindOf: class-object //对象是不是class-object的成员

    - (BOOL) isMemberOfClass: class-object //对象是不是class-object或其子类的成员

    - (BOOL) respondsToSelector: selector //对象是否能够响应selector所指定的方法,selector由@selector(方法名)产生

    + (BOOL) instancesRespondToSelectorr: selector //指定的类实例是否能响应selector

    - (BOOL) isSubclassOfClass: class-object //对象是不是指定类的子类

    继承

    同C++相似,重写,但不支持多继承

    对象本身指针为self, 父类指针为super,调用父类方法[super function...]

    内存管理

    [object retain] //引用计数加1

    [object release] //引用计数减1

    [object retainCount] //类的当前引用计数

    自动释放池

    NSAutoreleasePool *pool = [[NSAutoreleasePoll alloc] init];

    [pool release];

    只需调用object的autorelease方法,则将该object添加到自动释放池中,当池子被销毁时,像池中所有对象发送release消息。

    使用alloc,new和copy,需要安排对象的释放,通过release或autorelease,其他形式例如stringWithFormat等不需要考虑释放对象

    浅复制和深复制

    指针赋值,则指向对象相同

    NSCopy协议实现copy和mutableCopy,如果是数组复制,则为浅复制且数组元素为对象指针,则复制对象指针,并不从新分配空间。数组本身为深复制。

    copy  目的是为了创建新的对象副本,在objc中有如下区别:
    不可变对象 copy  == 对象retain    (这是由于ios内存优化的原因,因为不可变对象copy创建新对象其实还是和源对象是一样的,那么分配内存的时候直接不分配了而是指向源对象retain count就+1了,所以相当于retain)

    可变对象 copy      == 创建了一个新的不可变的对象

    可变/不可变对象mutablecopy == 创建一个新的可变对象

    有位前辈这么总结的:
    copy,生成的是不可变对象,无论源对象是否是可变的
    mutablecopy,新对象是可变的,无论源对象是否可变

    垃圾回收机制

     

    对象初始化

    alloc分配空间并将内存初始化为0,类初始化需要嵌套调用init

     

    异常

    @try{

    }@catch{

    }

    @throw

    C语言特性

     

    关键字

    extend;

    static;

    auto; const; volatile;

    枚举

    enum;

    预处理

    typedef; //typedef int counter

    #define;

    #; ##运算符;

    #ifdef #else #endif ifndef; //控制条件编译

    #if #elif #else #endif; //控制条件编译

    #undef;

     

    转换

    ()强制转换

    流程

    if else; for; while; do while; switch; goto

    for in;  //与C语言不同

    数组和多维数组

    type iter[10];

     

    函数

    returntype functionname(parameters)

    {

    }

     

    结构体

    struct structName{

    };

    sizeof函数

     

    联合

    union unionName{

    }

     

    指针

    type* p;

    与C语言相同

  • 相关阅读:
    关于jar项目发布(windows)
    SpringBoot 基础(一) mybatis 通过druid配置多数据库
    redis 基础(二) Redis安装
    测试开发3年,我决定去读个名校硕士
    大厂程序员凡尔赛的一天
    假如我拥有字节工牌。。。
    上海有哪些牛逼的互联网公司?
    那些学计算机的女生后来都怎么样了?
    微信支付零花钱刷屏了!5万额度,能花又能借
    清华集训 part1
  • 原文地址:https://www.cnblogs.com/zengyou/p/2563423.html
Copyright © 2011-2022 走看看