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

  • 相关阅读:
    Java线程面试题 Top 50
    抽象类
    this和super的区别
    引用传递
    【代码】递归调用
    java异常
    抽象类 final
    sublime使用技巧
    zabbix表达式
    tcpdump
  • 原文地址:https://www.cnblogs.com/shvier/p/5014359.html
Copyright © 2020-2023  润新知