我们知道在mac或iphone上编程最终逃不开os x平台,你无法在windows或linux上开发纯正的apple程序.(so不要舍不得银子买mac啦)虽说linux和windows上有移植的obj-c编译器,但是平台开发框架还是在mac上啊.比如cocoa框架包括Foundation框架,Application Kit框架和Core Data的第三方框架;二cocoa Touch指的则是Foundation,Core Data以及UIKit框架.对于Foundation框架中各个类的使用,可以到apple开发者网站在线查询:https://developer.apple.com/library/mac/navigation/

接下来我们不按顺序,专挑难点,奇怪点和好玩点(如果有的话)的类来看看,直接用代码说话喽

#import<Foundation/Foundation.h>

int main(int argc, char *argv[]){
	@autoreleasepool {
		NSNumber *n;
		NSInteger integer;	//not a class just a typedef

		n = [NSNumber numberWithInteger: 101];
		integer = [n integerValue];
		NSLog(@"%li",(long)integer);

		NSNumber *n1 = [[NSNumber alloc] initWithLong:0x12345678];
		[n1 initWithLong :0xabcd];	//can't change org value 0x12345678!

		integer = [n1 longValue];
		NSLog(@"%lx",(long)integer);

		NSString *str = @"hello apple";
		NSLog(@"str is : %@",str);
		NSLog(@"num is : %@",n);

		NSLog(@"Hello World!");
	}
	return 0;
}

注意代码中%@打印NSNumber型变量的行了吗?有人可能会问这是怎么实现的,我开始也以为%@只能格式化显示NSString类型啊.其实只要类中定义了description方法,就可以以自定义格式显示任何对象的内容啦.这个我们来写个简单类试一下:

#import<Foundation/Foundation.h>

@interface A:NSObject{
	int i;
}
	-(id)init:(int)i_v;
@end

@implementation A
	-(id)init:(int)i_v{
		self = [super init];
		if(self){
			i = i_v;
		}
		return self;
	}

	-(NSString*)description{
		return [NSString stringWithFormat:@"#i is %d#",i];
	}

@end

int main(int argc, char *argv[]){
	@autoreleasepool {
		A *a = [[A alloc] init:99];
		NSLog(@"a is %@",a);
	}
	return 0;
}

注意description方法的实现中stringWithFormat方法的2个参数分割符哦,不是:号而是逗号(,)哦.执行结果如下

wisy@wisy-ThinkPad-X61:~/src/objc_src$ clang -O3 -g0 $OBJ_C_OPT -lobjc -lgnustep-base -o f f.m
wisy@wisy-ThinkPad-X61:~/src/objc_src$ ./f
2014-07-01 12:42:59.380 f[4179] a is #i is 99#

Foundation中的字符串操作看起就显得那么蛋疼,简单的东西给搞的蛮复杂的,尤其是方法名,貌似还驼峰状,我呵呵了.字符串类分为可变和不可变两种,前者类位NSString,后者类为NSMutableString.顾名思义,不可变字符串不可以修改自身,只能返回一个修改后的新字符串,而可变字符串可以修改自身,包括删除subString,拼接啊,替换啊,皆可,下面上代码:

#import<Foundation/Foundation.h>

int main(int argc, char *argv[]){
	@autoreleasepool {
		NSString *str_no_m = @"hello world";
		NSMutableString *str_m;
		NSRange substr;	//just a struct

		str_m = [NSMutableString stringWithString: str_no_m];
		NSLog(@"%@",str_m);

		[str_m insertString: @" xxx" atIndex: 5];
		NSLog(@"%@",str_m);

		[str_m appendString: @" not_fix!"];
		NSLog(@"%@",str_m);

		[str_m deleteCharactersInRange: NSMakeRange(6,4)];	//(index,len)
		NSLog(@"%@",str_m);

		substr = NSMakeRange(6,6);
		[str_m deleteCharactersInRange: substr];
		NSLog(@"%@",str_m);

		substr = [str_m rangeOfString: @"_"];
		if(substr.location != NSNotFound){
			[str_m deleteCharactersInRange: substr];
		}
		NSLog(@"%@",str_m);

		[str_m setString: @"new string!"];
		NSLog(@"%@",str_m);

		substr = [str_m rangeOfString: @"new"];
		if(substr.location != NSNotFound){
			[str_m replaceCharactersInRange: substr withString: @"old"];
		}
		NSLog(@"%@",str_m);

		[str_m setString: @"1122334411223344"];
		[str_m replaceOccurrencesOfString:@"2" withString:@"X" \
			options:0 range:NSMakeRange(0,[str_m length])];
		NSLog(@"%@",str_m);
	}
	return 0;
}

编译及运行结果如下:

wisy@wisy-ThinkPad-X61:~/src/objc_src$ clang -O3 -g0 $OBJ_C_OPT -lobjc -lgnustep-base -o f f.m
wisy@wisy-ThinkPad-X61:~/src/objc_src$ ./f
2014-07-01 14:36:49.380 f[5719] hello world
2014-07-01 14:36:49.382 f[5719] hello xxx world
2014-07-01 14:36:49.382 f[5719] hello xxx world not_fix!
2014-07-01 14:36:49.383 f[5719] hello world not_fix!
2014-07-01 14:36:49.383 f[5719] hello not_fix!
2014-07-01 14:36:49.383 f[5719] hello notfix!
2014-07-01 14:36:49.383 f[5719] new string!
2014-07-01 14:36:49.383 f[5719] old string!
2014-07-01 14:36:49.383 f[5719] 11XX334411XX3344

注意其中的replaceOccurrencesOfString方法,其中的options可能的选项有:

Search and Comparison Options

Several of the search and comparison methods take an “options” argument. This is a bit mask that adds further constraints to the operation. You create the mask by combining the following options (not all options are available for every method):

Search option

Effect

NSCaseInsensitiveSearch

Ignores case distinctions among characters.

NSLiteralSearch

Performs a byte-for-byte comparison. Differing literal sequences (such as composed character sequences) that would otherwise be considered equivalent are considered not to match. Using this option can speed some operations dramatically.

NSBackwardsSearch

Performs searching from the end of the range toward the beginning.

NSAnchoredSearch

Performs searching only on characters at the beginning or, if NSBackwardsSearch is also specified, the end of the range. No match at the beginning or end means nothing is found, even if a matching sequence of characters occurs elsewhere in the
string.

NSNumericSearch

When used with the compare:options: methods, groups of numbers are treated as a numeric value for the purpose of comparison. For example,Filename9.txt
< Filename20.txt < Filename100.txt.

Search and comparison are currently performed as if the NSLiteralSearch
option were specified.

至于我用的值0,我猜是默认选项吧?因为书上值为nil,编译有警告,遂换为0.

obj-c编程10:Foundation库中类的使用(1)[数字,字符串]的更多相关文章

  1. obj-c编程10:Foundation库中类的使用(2)[字符串,数组]

    Foundation库的内容不可谓不多,就算很精简的说篇幅也受不了啊!笨猫一向反对博客文章一下子拖拖拉拉写一大坨!KISS哦!so将上一篇文章再分一篇来说,于是有了这篇,可能还会有(3)哦... 我发 ...

  2. obj-c编程10:Foundation库中类的使用(6)[线程和操作队列]

    任何语言都不能避而不谈线程这个东东,虽然他是和平台相关的鸟,虽说unix哲学比较讨厌线程的说...线程不是万能灵药,但有些场合还是需要的.谈到线程就不得不考虑同步和死锁问题,见如下代码: #impor ...

  3. obj-c编程10:Foundation库中类的使用(5)[时间对象]

    隔了好久才有了这新的一篇,还是无奈的时间啊!so这次我们就着重谈谈它喽. F库中有很多时间相关的类,比如NSDate,NSTimeInterval,NSTimeZone,NSDateComponent ...

  4. obj-c编程10:Foundation库中类的使用(4)[文件管理,查询当前进程信息]

    接上一篇文件管理博文.我们可以用NSPathUtilities.h中包含的NSString函数和分类扩展来以兼容的方式处理路径.下面凡是有系统编程经验的童鞋都知道是啥意思了: #import < ...

  5. obj-c编程10:Foundation库中类的使用(3)[文件管理]

    好吧,不管神马系统都无可避免的要说到文件,目录,路径(PATH)管理的内容,下面我们来看看在F库中对他们的支持.我简单看了下,不谈其他光从方法命名来说就多少显得有点复杂,如果和ruby相比就呵呵了. ...

  6. C++的XML编程经验――LIBXML2库使用指南[转]

    C++的XML编程经验――LIBXML2库使用指南 写这篇文章的原因有如下几点:1)C++标准库中没有操作XML的方法,用C++操作XML文件必须熟悉一种函数库,LIBXML2是其中一种很优秀的XML ...

  7. Java 库:为 Java 程序员而生的 10 + 最佳库

    众所周知,Java 的生态环境相当庞大,包含了数量相当可观的官方及第三方库.利用这些库,可以解决在用 Java 开发时遇到的各类问题,让开发效率得到显著提升. 举些例子,最常用的官方库有 java.l ...

  8. C++的XML编程经验――LIBXML2库使用指南

    C++的XML编程经验――LIBXML2库使用指南 写这篇文章的原因有如下几点:1)C++标准库中没有操作XML的方法,用C++操作XML文件必须熟悉一种函数库,LIBXML2是其中一种很优秀的XML ...

  9. 并发编程 10—— 任务取消 之 关闭 ExecutorService

    Java并发编程实践 目录 并发编程 01—— ThreadLocal 并发编程 02—— ConcurrentHashMap 并发编程 03—— 阻塞队列和生产者-消费者模式 并发编程 04—— 闭 ...

随机推荐

  1. ThreadLocal的使用[代码片段]

    1.ThreadLocal定义,在一个类中定义: 在类A中: private static ThreadLocal<String> kcsHtmlPath = new ThreadLoca ...

  2. TraceView性能分析工具介绍

    一.TraceView简介 TraceView是AndroidSDK里面自带的工具,用于对Android的应用程序以及Framework层的代码进行性能分析. TraceView是图形化的工具,最终它 ...

  3. SpringMVC系列之(一) 入门实例

    Spring MVC是非常优秀的MVC框架,由其是在3.0版本发布后,现在有越来越多的团队选择了Spring3 MVC了.Spring MVC结构简单,应了那句话简单就是美,而且他强大不失灵活,性能也 ...

  4. 深入剖析Tomcat类加载机制

    1JVM类加载机制 JVM的ClassLoader通过Parent属性定义父子关系,可以形成树状结构.其中引导类.扩展类.系统类三个加载器是JVM内置的. 它们的作用分别是: 1)引导类加载器:使用n ...

  5. UNIX网络编程——非阻塞式I/O(套接字)

    套接字的默认状态是阻塞的.这就意味着当发出一个不能立即完成的套接字调用时,其进程将被投入睡眠,等待相应的操作完成.可能阻塞的套接字调用可分为以下4类: (1)输入操作,包括read,readv,rec ...

  6. 解决ActionBar中的item不显示在ActionBar的问题

    今天在用ActionBar,需要增加一个菜单选项,按教程在/res/menu下对应的布局文件中添加了一个item,但是它却是显示在overflow中,而不是直接显示在ActionBar当中的.我的布局 ...

  7. Java采用JDBC的方式连接Hive(SparkSQL)

    前两天,由于系统的架构设计的原因,想通过Java直接访问Hive数据库,对于我这个Java以及Hadoop平台的菜鸟来说,的确是困难重重,不过,还好是搞定了.感觉也不是很麻烦.这篇文章,作为一个感想记 ...

  8. Linux驱动基础:MSM平台AP/CP通信机制

    点击打开链接 概述 MSM平台AP和CP封装到一个芯片,共享内容.所以之前也说过,高通的MSM解决方案中,CP的代码都是由AP放到指定地址的内存中以供CP运行.那上传完代码,CP开始跑之后,AP/CP ...

  9. Cocos2D:塔防游戏制作之旅(十七)

    getHpDamage方法当敌人到达基地时被调用.你需要添加该方法到Enemy.m的update:方法中去,以便检查当敌人到达基地是会发生什么.幸运的是,你已经在之前的代码中实现这些了,你可以接着往下 ...

  10. 检查app的activity是否在当前

    APP开发中经常会遇到这种需求,需要检查当前的APP是不是可见的,比如,如果是可见的就维持一个socket长连接,如果切到后台不可见了,就断开这个连接.Android本来并不允许APP去监听home键 ...