我们知道在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. 01_数据库连接池,数据源,ResultSetMetaData,jdbc优化

     一.数据库连接池 1. 什么是连接池 传统的开发模式下,Servlet处理用户的请求,找Dao查询数据,dao会创建与数据库之间的连接,完成数据查询后会关闭数据库的链接. 这样的方式会导致用户每 ...

  2. android连接打印机

    android连接  网络打印,主要使用socket连接设备,发送指令给设备. 首先要有设备的IP,端口号一般默认的是9100 //打印设备网络IP etIp.setText("192.16 ...

  3. 查看某一职责下对应的菜单&功能&请求

    查看菜单&功能 SELECT res.RESPONSIBILITY_NAME 职责名称, menu.MENU_NAME 菜单编码, menu.USER_MENU_NAME 菜单名称, func ...

  4. Android初级教程获取手机系统联系人信息

    在手机内部,对联系人信息存在对应的数据库.我们创建的而联系人信息都存在这张表中.如下是对数据库的截图,我已经对表和应该注意的地方做了红笔标注: 好了,现在可以根据数据库里面的数据来写代码了. 代码如下 ...

  5. 怎么对MySQL数据库操作大数据?这里有思路

    最近学到一招关于使用java代码操作MySQL数据库,对大文本数据(LOB)进行CRUD的技巧,虽然向数据库很少向中存入大文本文件(一般都是存储在硬盘上),但是还是很有必要知道这一技巧的.下面我就来说 ...

  6. Docker教程:docker的概念及安装

    http://blog.csdn.net/pipisorry/article/details/50754385 Why docker 对于运维来说,Docker提供了一种可移植的标准化部署过程,使得规 ...

  7. Android进阶(四)一个APP引发的思索之ArrayList的add总是添加相同的值

    解决"ArrayList的add总是添加相同的值"问题 前言 最近在写一个小的Android APP,在用ArrayList的add时,总是出现添加相同值的现象.如下图所示: 错误 ...

  8. Java-IO之对象输入流输出流(ObjectInputStream和ObjectOutputStream)

    ObjectInputStream和ObjectOutputStream的作用是对基本数据和对象进行序列化操作支持.创建文件输出流对应的ObjectOutputStream对象,该ObjectOutp ...

  9. 现代控制理论习题解答与Matlab程序示例

    现代控制理论习题解答与Matlab程序示例 现代控制理论 第三版 课后习题参考解答: http://download.csdn.net/detail/zhangrelay/9544934 下面给出部分 ...

  10. Linux 进程调度小结

    概述 这个问题又是面试常问问题,当时听到感觉太宽泛了,有点大,心里知道但是说不全,这里做一下总结 [1]进程调度的作用 [2]调度德策略 1. 进程调度的作用 ,进程调度就是对进程进行调度,即负责选择 ...