• OC基础 类别


    类别的创建

     integer.h

    @interface integer : NSObject
    @property int integer;
    @end

    integer.m

    @implementation integer
    
    @end

    类别 

    integer+display.h
    integer+display.m
    #import <Cocoa/Cocoa.h>
    #import "integer.h"
    NS_ASSUME_NONNULL_BEGIN
    // 类别 没有成员变量
    
    @interface integer(display) //原有类名(类别名)
    
    -(void)show;
    -(void)add:(integer*)other;
    -(void)sub:(integer*)other;
    -(void)mul:(integer*)other;
    -(void)div:(integer*)other;
    @end
    #import "integer+display.h"
    
    @implementation integer(display)
    -(void)show
    {
        
        
    //    NSLog(@"hello world");
        NSLog(@"%i",self.integer);
    }
        
    -(void)add:(integer*)other
        {
            self.integer+=other.integer;
            
        }
    
    -(void)sub:(integer*)other
    {
        
        self.integer-=other.integer;
        
    }
    -(void)mul:(integer*)other
    {
        self.integer*=other.integer;
    }
    -(void)div:(integer*)other
    {
        if(other.integer!=0)
        {
            
            self.integer/=other.integer;
            
        }
        
    }
    @end

    字符串的反转

    #import <Foundation/Foundation.h>
    #import "integer.h"
    NS_ASSUME_NONNULL_BEGIN
    
    @interface NSString(reverse)// 原有类名(类别名)
    -(NSString*)reverseString;
    
    
    @end
    #import "integer+reverse.h"
    
    @implementation NSString(reverse)
    -(NSString*)reverseString
    {
     //将原有字符串反转  创建常量字符串无法修改 需要提前添加缓冲区 进行赋值操作
     //1.将字符串内容转换成c语言字符串
        char* str=[self UTF8String];
        
     //2.讲c语言字符串反转
    int len=strlen(str);
    char* new=(char*)malloc(len+1);
        memset(new, 0, len+1);
    while(*str !='')
    {
        new[len-1]=*str;
        str++;
        len--;
       
        
    }
    //3.通过新字符串创建字符串对象
    NSString* reverse=[NSString stringWithUTF8String:new];
    //4.释放缓冲区
       free(new);
       return reverse;
           
           
    }
    
    @end
     NSString* str=@"abcde";
            NSString* reverse=[str reverseString];
            NSLog(@"%@",reverse);
            
            
            

    字符串反转2

    -(NSString*)reverseString2
    {
        //1.获取字符串长度
        int len =[self length];
        //2.创建一个字符串对象
        
        NSMutableString* reverse=[NSMutableString stringWithCapacity:len];
        //3.反转操作
        for (int i=len-1;i>=0;i--)
    {
        
            //3.1 从后向前获取字符串
            unichar c=[self characterAtIndex:i];
            //3.2 将字符串转换为字符串对象
        
            NSString* temp=[NSString stringWithCharacters:&c length:1];
            //3.3 将字符串对象添加到可变字符串对象
        
            [reverse appendString:temp];
            
            
        }
        return reverse;
      
    }
  • 相关阅读:
    Mysql事务的隔离级别,ACID以及三要素的取舍(强一致性弱一致性)、ABA问题
    符合python风格的对象
    python重载运算符之左add右add(radd)
    pycham快捷键
    设置linux时间同步
    描述克隆linux的步骤,以及后续配置
    linux命令
    ransientException: find no Route:SELECT * FROM `ego`.`tb_content_category` LIMIT 0,1000‘
    linux修改vim /etc/profile 配置文件 修改不了
    web.xml
  • 原文地址:https://www.cnblogs.com/zhangqing979797/p/13205530.html
Copyright © 2020-2023  润新知