码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-12-02 22:21:15      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

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

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

标签:

原文地址:http://www.cnblogs.com/shvier/p/5014359.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!