zoukankan      html  css  js  c++  java
  • #在蓝懿学习iOS的日子#Day8

    今天学习主要是学习分类,字符串,可变的字符串,number

    一、分类:给现有的类添加方法,就是添加一个类,再添加一个类+方法名

    在类+方法名的.h头文件里,

    引入一个类的

    #import "Dad.h"

    @interface Dad(smoke)

     -(void)smoke;

    然后在Viewcontroller.M就可以调用smoke的方法了

    二、字符串

    (一)创建字符串

    NSString *z1 =@"zbc";

    //  用工厂方法创建字符串

      NSString *z2 =[NSString stringWithFormat:@"李四"];

    (二)字符串的截取

    例:

     NSString *string  = @"http://www.baidu.com";

    1、从头开始到第几个

    NSString *substring1 = [string substringToIndex:7 ];

    2、从第几个后开始到结束

     NSString *substring2 = [string substringFromIndex:7];

    3、截取范围

    NSString *substring3 = [string substringWithRange:NSMakeRange(11, 5)];

    //11是从第几个后开始,5是长度,几个

    (三)字符串的拼接

    NSString *s1 = @"名字是";

        NSString *s2 = @"刘德华";

    1、两个字符串连在一起

        NSString *s3 = [NSString stringWithFormat:@"%@%@",s1,s2];

    2、把一个字符串连在另个的后面

        NSString *s4 = [s1 stringByAppendingString:s2];

    3、前两个的结合版

        NSString *s5 = [s1 stringByAppendingFormat:@":%@",s2];

    4、path是指路径

     NSString *desktopPath =@"/Users/lanyi/Desktop";

        NSString *fileName = @"小电影.mp4";

     NSString *filePath = [desktopPath stringByAppendingPathComponent:fileName];

        NSLog(@"%@",filePath);

    (三)字符串的替换

    NSString *newPath  = [filePath stringByReplacingOccurrencesOfString:@"/" withString:@"-"];

        NSString *newName = [fileName stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:@""];

    (四)判断字符串是否包含

    if ([filePath rangeOfString:@"lanyi"].length>0) {

            NSLog(@"包含");

        }else NSLog(@"不包含");

    (五)字符串编解码

    1、把纯文本文件中的字符串 加载到内存中

       NSString *path = @"/蓝懿二期/正式基础课/Day 8/饿了么.rtf";

        NSString *abcString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

    2、把内存中的字符串保存到纯文本文件中

    NSString *s = @"饿死我了 中午吃啥?????";

        s = [abcString stringByAppendingString:s];

     [s writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];

    三、可变字符串:首先要创建好一个类,并且要添加好属性,然后在Viewcontroller.M就可以编写代码

     

     //纯文本的路径保存到path

     

        NSString*path = @"/Users/yeqian/Desktop/明星.txt";

     

        //文件的路径装进字符串starsString

     

        NSString *starsString =[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

     

        //用数组裁成每一行并存到Lins

     

        NSArray *lines = [starsString componentsSeparatedByString:@" "];

     

        //准备一个可变的数组stars把所有的明星对象保存下来

     

        NSMutableArray*stars = [NSMutableArray array];

     

        //遍历lines并分割lines的字符串

     

        for (NSString*line in lines) {

     

            //用数组每一行裁成单个并存到dan

     

            NSArray *dan = [line componentsSeparatedByString:@" "];

     

            

     

            Star *s = [[Star alloc]init];

     

            //用数组给对象s相应属性赋值

     

            s.name = dan[0];

     

            s.nationality =dan[1];

     

            s.occupation = dan[2];

     

            s.age = [dan[3] intValue] ;

     

            //把对象S存入可变的数组stars

     

            [stars addObject:s];

     

        }

     

        

     

        for (Star*s in stars) {

     

            NSLog(@"%@ %@ %@ %d",s.name,s.nationality,s.occupation,s.age);

     

        }

     

        for (int i = 0; i<stars.count; i++) {

     

            // 可变的数组stars相应属性 赋值给创建一个对象h

     

            Star *h = stars[i];

     

            UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, i*40+20, 80, 40)];

     

            nameLabel.text =h.name;

     

            nameLabel.textColor = [UIColor blackColor];

     

            [self.view addSubview:nameLabel];

     

            UILabel *nationalityLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, i*40+20, 80, 40)];

     

            nationalityLabel.text =h.nationality;

     

            nationalityLabel.textColor = [UIColor blackColor];

     

            [self.view addSubview:nationalityLabel];

     

            UILabel *occupationLabel = [[UILabel alloc]initWithFrame:CGRectMake(160, i*40+20, 80, 40)];

     

            occupationLabel.text =h.occupation;

     

            occupationLabel.textColor = [UIColor blackColor];

     

            [self.view addSubview:occupationLabel];

     

            UILabel *ageLabel = [[UILabel alloc]initWithFrame:CGRectMake(240, i*40+20, 80, 40)];

     

            ageLabel.text =[@(h.age) stringValue];

     

            ageLabel.textColor = [UIColor blackColor];

     

            [self.view addSubview:ageLabel];

     

            

     

        }

     

    }

    四、基本数据类型和结构体不可以直接往数组中放

    1、基本数据类型

        [arr addObject:@(YES)];

        //把基本数据类型转成对象NSNumber

        NSNumber *n = [NSNumber numberWithFloat:2.5];

        //把数据转回基本数据类型

        float f = [n floatValue];

        

      2、结构体

        CGPoint  p = CGPointMake(100, 100);

            //把结构体转成对象NSValue

        NSValue *v =[NSValue valueWithCGPoint:p];

        [arr addObject:v];

          //把数据转回基结构体

        CGPoint p1 = [v CGPointValue];

     

  • 相关阅读:
    shell log
    Python:列出列表中所有元素的组合可能
    scrapy 停止爬虫
    shell split log by data
    mitmproxy 配置
    插件reres的使用,替换网站的js文件
    解决小米Note adb调试无法发现设备
    md5 计算文件一致性
    【Frida Hook 学习记录】Frida Hook Android 常用方法
    监控神器普罗米修斯Prometheus安装配置
  • 原文地址:https://www.cnblogs.com/odileye/p/4924302.html
Copyright © 2011-2022 走看看