zoukankan      html  css  js  c++  java
  • 【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表示一次性写入,如果写入过程中出错,就全部都不要写入了。

  • 相关阅读:
    看了支付宝账单,我才知道我是『有钱人』
    用 Pytest+Allure 生成漂亮的 HTML 图形化测试报告
    测试开发城市群,第一站深圳,我们来了!
    测试工作常用 Linux/ Shell 命令之 Bash 逻辑控制基础
    性能测试实战 | JMeter 录制/回放做 App 后端性能压测
    测试圣诞狂欢,万元红包、冲榜豪礼等你拿!
    自动化测试工具 or 自动化策略?孰轻孰重?
    Java 面试题
    疫情之下,普通人高薪就业指南:方向对了,路就不会遥远!
    SharePoint WebPart:扩展SharePoint 2007中图片展示功能
  • 原文地址:https://www.cnblogs.com/shvier/p/5014359.html
Copyright © 2011-2022 走看看