zoukankan      html  css  js  c++  java
  • OC

    一,字符串 1创建一个字符串

    1) NSString *str2=[[NSString alloc]initWithString:str1];

    2)  NSString *string2=[[NSString alloc]initWithCString:"i am a man"  encoding:NSUTF8StringEncoding];

    3)   NSString *string3=[[NSString alloc]initWithFormat:@"%@ %@",tempString];

      

    是用类方法创建oc字符串(常用)

    1)       NSString *string5=[NSString stringWithString:tempSting];

    2)       NSString *string6=[NSString stringWithCString:"i am a man" encoding:NSUTF8StingEncoding];

    3)       NSString *string7=[NSString stringWithFormat:@"%@ %@",string5,string7];

         

    2,字符串的拼接

    1)       NSString *catSting= [NSString stringWithFormat:@"%@,%@",string8,string9];

    2)       NSString *cat2Sting=[catSting stringByAppendingString:@"我来了"];

    占位

    NSString*str=[NSString stringWithFormat:@"%02d",i];

    一共两位,空位由0占取

    3,计算字符串长度

         NSString *Str=@"SHABI";

            NSInteger len =[ Str length];    

      4,判断字符串是否相等

    BOOL isEqual=[str1 isEqualToString:str2];

    5,字符串比较   //compare  返回值 为1,0,-1;

           int i=[str1 compare str2];

      不区分大小写的字符串比较

      [str1 compare:str2 options:NSCaseInsensitiveSearch];

    6,大小写转换

    NSString *str2=[str1 uppercaseString];//小写转大写,执行一次操作,不改变原来的

    NSString *str4=[str3 lowercaseString]; //大写转换小写

    NSString *capstr=[str5 capitalizedString];//首字母转大写

    7,字符串查找

          NSString *str=@"我们都是大好人,大坏人,大好人";

            NSRange range = [str rangeOfString:@"大坏人"];  //注意没有* 因为  NSRange有两个元素一 个是location,一个是length

    if(range.location==NSNotFound)

            {

                NSLog(@"没找着");

            }

    8,判断前后缀

    BOOL have=[str hasPrefix:@"1234567"]; //前

            BOOL have2=[str hasSuffix:@"90"];     //后

    9,字符串提取(截出来)

             NSString *str1=@"123321234567";

      1      NSString* str2 =[str1 substringToIndex:4]; //从头到哪

      2      NSString *str3=[str1 substringFromIndex:3];//从3到最后

      3      NSString *str4=[str1 substringWithRange:NSMakeRange(8,4)];//从下标开始 截一段

    10,NSMutableString //在使用时候最好不要直接赋值(=@“”),因为这个是可变字符串,他的内容都是append进去的。

    alloc创建

            NSMutableString *str1=[[NSMutableString alloc]initWithCapacity:0];

    类方法创建

            NSMutableString *str2=[NSMutableString stringWithCapacity:0];        

    增 赋值

           [str2 appendString:@"123"];

      [str2 appendFormat:@"啊"];

        [str2 insertString:@"我" atIndex:2];

        删除

         [str2 deleteCharactersInRange:NSMakeRange(2, 3)];

    改        

    [str2 setString:@"abcdefg"];//全改

      [str2 replaceCharactersInRange:NSMakeRange(3, 2) withString:@"doubi"]; //改部分

    二,数组

    NSArray创建:

    NSArray *array=[[NSArray alloc]initWithObjects:@"123",@"456",nil];

    NSArray *array2=[[NSArray alloc]initWithArray:array];

    NSArray *array=[NSArray arrayWithObjects:@"i",@"k",@"f",nil];

    NSArray *array1=[NSArray arrayWithArray:array];

    获取数组的元素

        NSArray *str1=[array1 objectAtIndex:0];

    数组的长度

        int len=[array count];

    分割字符串

    分割

    NSArray *arr1=[s1 componentsSeparatedByCharactersInSet:[NSCharacterSet                         characterSetWithCharactersInString:@"年 月日:"]];

    NSMutableArray

    创建

        NSMutableArray *s1=[NSMutableArray arrayWithCapacity:0];

    增,向数组里添加新的对象

        [s1 addObject:@"hello"];//向数组的最后一位依次增加

      [s1 insertObject:@"my" atIndex:0];//向数组某一位增加

        [s1 addObjectsFromArray:@[@"1",@"2"]];//增加多个

        [s1 removeObjectAtIndex:0];//删除下标

        [s1 removeLastObject];  //删除最后一位

        [s1 removeAllObjects];  //删光

    s1 replaceObjectAtIndex:0 withObject:@"are"];

    交换

        [arr1 exchangeObjectAtIndex:1 withObjectAtIndex:4];

    三,字典部分

    创建  alloc

        NSDictionary *dic =[[NSDictionary alloc]initWithObjectsAndKeys:@"V1",@"K1", nil];

    NSDictionary *dic1=[[NSDictionary alloc]initWithDictionary:dic];

    NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@"v1",@"k1",nil];

    找到对应key的value

        NSString *str1 =[dic objectForKey:@"饮料"];

    遍历字典的key

        NSArray *str=[dic allKeys];

    部分

    创建

    NSMutableDictionary *dic=[NSMutableDictionary dictionaryWithCapacity:0];

    [dic setObject:@"china" forKey:@"country"];

    删除

    [dic removeObjectForKey:@"country"];// 删除key

        [dic removeAllObjects];//全删

    归档,把一个字典赋值给一个类

    -(id)initPeopleWithDic:(NSDictionary *)dic

    {

        self = [super init];

        if(self)

        {

            [self setValuesForKeysWithDictionary:dic];

        }

        return self;

    }

    -(void)setValue:(id)value forUndefinedKey:(NSString *)key

    {

        NSLog(@"出现异常的key%@",key);

    }

    四,单例的创建

    #import "Sign.h"

    //第一步 声明一个全局静态变量(这个类的对象)

    static Sign *single=nil;

    @implementation Sign

    //第二步 实现+方法

    +(id)shareInstance

    {//第三步 做一个局部静态变量 是dispatch_once_t的类型的

        static dispatch_once_t onceTaken;

        //第四步 执行dispatch_once方法,第一个参数是上面的onceTaken的地址,第二个参数是这么写的^{},在大 括号里面实现全局静态变量的alloc方法,并在alloc方法后面加分号。

        dispatch_once(&onceTaken,^{

            single=[[Sign alloc]init];

            single.isSign = @"yes";

        });

        //第五步 最后将静态全局变量return

        return single;

    五 NSData //数据类,用在本地缓存 ,网络请求,NSData就是一个二进制

    NSData 与 NSstring 之间的转换。

          (1) NSstring 转换成  NSData    

    NSString *str=@"我是一个中国人";

            NSData *str2=[str dataUsingEncoding:NSUTF8StringEncoding]; 

      (2) NSData 转换成 NSstring

    NSString *str3=[[NSString alloc]initWithData:str2  encoding:NSUTF8StringEncoding];

           

    NSData 与 NSArray 之间转换j

         NSArray *arr=[NSArray arrayWithObjects:@"haha", nil];

         NSData *arr1=[NSKeyedArchiver archivedDataWithRootObject:arr];

           //NSArray转为NSData

         NSArray *arr2=[NSKeyedUnarchiver unarchiveObjectWithData:arr1];

           

    与 NSDictionary 之间转换

          NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@"aa",@"ff" ,nil];

            //创建一个可变Data

          NSMutableData *data=[[NSMutableData alloc]init];

            //把这个可变的Data 加入到一个归档中

            NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc] initForWritingWithMutableData:data];

            //在 archiver 里 定义一个 dic 的  标记

            [archiver encodeObject:dic forKey:@"1"];

            //归档结束

            [archiver finishEncoding];

            NSLog(@"%@",data);

            

            

    //创建一个解归档对象,这个对象初始化 从一个Data 里面读取数据

            NSKeyedUnarchiver *unArchiver=[[NSKeyedUnarchiver alloc] initForReadingWithData:data];

            //转换,使用这个解归档对象把 这个data的 标记读出来

            NSDictionary *backDic=[unArchiver decodeObjectForKey:@"1"];

            //结束解归档

            [unArchiver finishDecoding];

    plist 文件最后可能是NSDictionary或者NSArray

    NSDictionary * plistDic=[NSDictionary dictionaryWithContentsOfFile:文件位置];

    NSArray *plistArray1=[NSArray arrayWithContentsOfFile:文件地址];

    七,Json

    Json 是个字符串 用在网络交互

    解析

    NSData *dataJson=[strJson dataUsingEncoding:NSUTF8StringEncoding];

            NSArray *arrayJson=[NSJSONSerialization JSONObjectWithData:dataJson options:NSJSONReadingMutableLeaves error:nil];

    八,NSDate

    得到一个date

            NSDate *date = [NSDate date];

    //得到昨天的时间,也就是24小时之前的时间

    NSDate *yesterdayDate = [NSDate dateWithTimeIntervalSinceNow:-(24*60*60)];

    定义时间格式

    NSDateFormatter *format = [[NSDateFormatter alloc] init];

    [format setDateFormat:@"hh:mm:ss:SSS"];

    //得到日历

            //NSCalendar

            //实例化一个日历对象,并初始化,初始化的时候要initWithCalendarIdentifier

            NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

            //实例化一个日期的对象,这个对象不是NSDate的是NSDateComponents的

            NSDateComponents *com = [[NSDateComponents alloc] init];

            //做一个标示,表示我们要什么内容

            NSInteger flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

            //从一个日期里面把这些内容取出来

            com = [calendar components:flags fromDate:date];

            int week = [com weekday];

            int year = [com year];

            int month = [com month];

            int day = [com day];

            int hour = [com hour];

            int min = [com minute];

            int sec = [com second];

            NSLog(@"时间是星期%d,年%d,月%d,ri%d,小时%d,分%d,秒%d",week,year,month,day,hour,min,sec);

            

            //NSTimeInterval时间的间隔

            //把一个字符串的时间,转为NSDate的时间格式

            NSDate *tempDate = [[NSDate alloc] initWithString:@"2000-05-05 10:10:10 +0600"];

            //上面的时间距某个时间的时间差

            NSTimeInterval distance = [tempDate timeIntervalSinceNow];

            //把时间差改为具体的日子

            NSTimeInterval hereDate = distance/(86400*365);

            NSLog(@"从现在开始到%@有多少%f",[tempDate description],hereDate);

            

            NSDate *startDate = [[NSDate alloc] init];

            NSDate *toDate = [[NSDate alloc] initWithString:@"2013-05-05 10:10:10 +0600"];

            NSCalendar *hereCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

            //components: fromDate: toDate: options:

            NSDateComponents *hereCom = [hereCalendar components:flags fromDate:startDate toDate:toDate options:0];

            int hereHour = [hereCom hour];

            int hereMin = [hereCom minute];

            int hereSec = [hereCom second];

            int hereDay = [hereCom day];

            int herMonth = [hereCom month];

            int hereYear = [hereCom year];

            NSLog(@"从现在开始到%@,差了%d年,%d月,%d日,%d小时,%d分,%d秒",[toDate description],hereYear,herMonth,hereDay,hereHour,hereMin,hereSec);

  • 相关阅读:
    Grove.net实践ORM学习笔记
    COM+的事务
    Delphi中MIDAS线程模型
    Delphi中封装ADO之我重学习记录。。。
    100 多个JaveScript 常用函数
    javascript 事件
    js 收藏
    js 常用函数
    表单11种Input的高级用法
    UltraEdit 使用技巧
  • 原文地址:https://www.cnblogs.com/whx060900/p/5751973.html
Copyright © 2011-2022 走看看