我们知道在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. #VSTS日志# Xamarin构建支持和一大波更新

    距离上次更新#VSTS日志#已经有将近3个月的时间了,赶上最近Xamarin开源免费的消息,VSTS也推出了更多跨平台的支持和许多其他功能.这里列出一些小编觉得比较重要. 1. Xamarin 构建模 ...

  2. 4.1、Android Stuido配置你的Build Variant

    每个版本的build variant代表了你可以构建的每一个版本.虽然你未直接配置build variants,你可以通过配置build type和product flavor. 比如,一个demo的 ...

  3. Android布局性能优化—从源码角度看ViewStub延迟加载技术

    在项目中,难免会遇到这种需求,在程序运行时需要动态根据条件来决定显示哪个View或某个布局,最通常的想法就是把需要动态显示的View都先写在布局中,然后把它们的可见性设为View.GONE,最后在代码 ...

  4. Cocos2D:塔防游戏制作之旅(十三)

    让我们看一下Waves.plist文件,你将注意到它包含了3个数组.每一个数组表示一波攻击,也就是一组敌人一起到达闹事.第一个数组包含6个字典.每一个字典定义1个敌人. 在本次教程中,字典只存储敌人应 ...

  5. STL:vector容器用法详解

    vector类称作向量类,它实现了动态数组,用于元素数量变化的对象数组.像数组一样,vector类也用从0开始的下标表示元素的位置:但和数组不同的是,当vector对象创建后,数组的元素个数会随着ve ...

  6. [问与答]为什么 'a' in ('abc') 是True 而 'a' in ['abc'] 是False呢?

    Why is 'a' in ('abc') True while 'a' in ['abc'] is False? 原文链接 问 在使用解释器的时候,表达式'a' in ('abc') 返回是True ...

  7. 2016 苹果全球开发者大会(WWDC)

    纵观WWDC 2016开发者大会的全部内容,尽管本次大会没有那些新的产品发布,不过能让各位果粉的肾留到秋天,那也是苹果公司对各位果粉的关爱啊.但是对iOS开发者而言,新发布的技术还是比较不错的.主要内 ...

  8. ZooKeeper 实现分布式队列

    使用场景  在传统的单进程编程中,我们使用队列来存储数据结构,用来在多线程之间共享或者传递数据.在分布式环境下,同样需要一个类似单进程的组件, 用来实现跨进程.跨主机.跨网络的数据共享和数据传递.这就 ...

  9. 解决UIScrollView,UIImageView等控件不能响应touch事件的问题

    关于UIScrollView,UIImageView等控件不能响应touch事件,主要涉及到事件响应者链的问题,如果在UIScrollView,UIImageView等控件添加了子View,这样事件响 ...

  10. Android重命名包名

    工程写的差不多了才发现原来用的包名还是自己尝试性的进行写代码的时候用到的.但apk的发布,google map api的申请等等方面都需要用到一个比较规范的包名.这就涉及到修改包名的问题. 包名一开始 ...