• 【Objective-C学习记录】第一天


    1.Foundation框架

    Cocoa可以看作许多框架的集合,在iOS中包括Foundation框架和UIKit框架,Foundation框架里主要是一些API供程序开发使用,UIKit则包括了许多UI绘制的函数。

    2.字符串操作

    初始化字符串:

      NSString *str1 = [[NSString alloc] init];

      str1 = @"Hello,iOS.";

      NSString *str2 = [[NSString alloc] initWithString:@"Hello,iOS."];

      NSString *str3 = [[NSString alloc] initWithFormat:@"Age is %i,name is %.2f",20,56.0f];

      NSString *str4 = [[NSString alloc] initWithUTF8String:@"Hello,iOS."];

    格式化大小写:

      //转换成小写

      NSLog(@"%@",[@"Hello,iOS." lowercaseString]);

      //转换成大写

      NSLog(@"%@",[@"Hello,iOS." uppercaseString]);

      //首字母大写,其他字母小写

      NSLog(@"%@",[@"Hello,iOS." capitalizedString]);

    比较字符串:

      //判断是否相等

      BOOL res = [@"abc" isEqualToString:@"abc"];

      //大小比较,有三种结果:NSOrderedAscending/NSOrderedDescending/NSOrderedSame

      NSComparisonResult res = [@"abc" compare:@"ABC"];

      if(res == NSOrderedAscending)

        NSLog("Left < Right");

      else if(res == NSOrderedDescending)

        NSLog("Left > Right");

      else

        NSLog("Left = Right");

    字符串前缀和后缀的判断:

      [@"abcdef" hasPrefix:@"abc"];

      [@"abcdef" hasSuffix:@"def"];

    查找字符串:

      NSRange range = [@"abcdefg" rangeOfString:@"cd" ];

      if(range.location == NSNotFound)

        NSLog(@"Not found.");

      else

        NSLog(@"range is %@",NSStringFromRange(range));//range is {2,2}

    字符串分割:

      NSLog(@"%@",[@"abcdef" substringFromIndex:3]);//def

      NSLog(@"%@",[@"abcdef" substringToIndex:3]);//abc

      NSLog(@"%@",[@"abcdef" substringWithRange:NSMakeRange(2,3)]);//cde

      NSString *str = @"a.b.c.d.e.f";

      NSArray *array = [str componentsSeparatedByString:@"."];

      NSLog(@"%@",array);//(a,b,c,d,e,f);

    类型转换:

      NSLog(@"%i",[@"12" intValue]);//12

      NSLog(@"%zi",[@"Hello,iOS." length]);//10

      NSLog(@"%c",[@"Hello,iOS." characterAtIndex:0]);//H

      const char *s = [@"Hello,iOS." UTF8String];

      NSLog(@"%s",s);//Hello,iOS.

    文件操作:

      //读取文件内容

      NSString *path = @"/Users/Shvier/Desktop/Demo.txt";

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

      NSLog(@"%@",str);//Hello,iOS.

      //写入文件内容

      NSString *path = @"/Users/Shvier/Desktop/Demo.txt";

      NSString *str = @"Hello,iOS";

      [str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];//atomically表示一次性写入,如果写入过程中出错,就全部都不要写入了。

  • 相关阅读:
    Download: Microsoft Access Database Engine 2010 Redistributable
    18大顺丰不发航空件
    北京南站不是24*7的
    360压缩虽然有占霸道,但是for free,我已经不想去找破解软件了
    VS2010、SQL Server 2008和SQL Server 2012安装详解
    【新提醒】LENOVO_WIN7_SP1_UM_64_CN_RDVD远景Windows7,Windows8,旗舰版,系统下载,主题
    原来qq下载也有类似迅雷的功能了
    如意通5元卡办理了,可以用wifi热点了
    SQLEXPR.EXE 和 SQLEXPR32.EXE的区别 挨踢人 博客园
    HTTP Proxy Support
  • 原文地址:https://www.cnblogs.com/shvier/p/5014359.html
Copyright © 2020-2023  润新知