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

    我们知道在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.

时间: 2024-08-02 04:58:17

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

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

    Foundation库的内容不可谓不多,就算很精简的说篇幅也受不了啊!笨猫一向反对博客文章一下子拖拖拉拉写一大坨!KISS哦!so将上一篇文章再分一篇来说,于是有了这篇,可能还会有(3)哦...     我发现NSString有这样一个类方法蛮有趣的:stringWithContentsOfURL.看名称就知道是直接将get网页的内容,放在字符串这个类而不是以网络扩充的方式添加这个方法还是很有意思哦: #import<Foundation/Foundation.h> int main(

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

    任何语言都不能避而不谈线程这个东东,虽然他是和平台相关的鸟,虽说unix哲学比较讨厌线程的说...线程不是万能灵药,但有些场合还是需要的.谈到线程就不得不考虑同步和死锁问题,见如下代码: #import <Foundation/Foundation.h> #define msg(...) NSLog(__VA_ARGS__) @interface Foo:NSObject{ int i; //NSLock *lock; NSRecursiveLock *lock; } @propert

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

    隔了好久才有了这新的一篇,还是无奈的时间啊!so这次我们就着重谈谈它喽.     F库中有很多时间相关的类,比如NSDate,NSTimeInterval,NSTimeZone,NSDateComponents,NSCalendar,最后还有一个NSDateFormatter.新手很快就被弄的云里雾里了,我也在雾里呆了好长段时间了.简单地说NSDate只是一个单纯的时间类:NSTimeInterval用来表示2个时间之间的间隔:NSTimeZone和NSCalendar顾名思义自然是表示

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

    好吧,不管神马系统都无可避免的要说到文件,目录,路径(PATH)管理的内容,下面我们来看看在F库中对他们的支持.我简单看了下,不谈其他光从方法命名来说就多少显得有点复杂,如果和ruby相比就呵呵了.     以下代码功能包括文件的打开,复制,移动,取其内容,文件夹的2种遍历方法(递归和非递归): #import <Foundation/Foundation.h> #define msg(...) NSLog(__VA_ARGS__) int main(int argc, char *a

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

    接上一篇文件管理博文.我们可以用NSPathUtilities.h中包含的NSString函数和分类扩展来以兼容的方式处理路径.下面凡是有系统编程经验的童鞋都知道是啥意思了: #import <Foundation/Foundation.h> #define msg(...) NSLog(__VA_ARGS__) int main(int argc, char *argv[]){ @autoreleasepool { NSString *short_name = @"love

在C++编程中如何自编一个比较两个字符串s和t大小的函数strcomp(s,t)

问题描述 在C++编程中如何自编一个比较两个字符串s和t大小的函数strcomp(s,t) 要求:s小于t时返回-1,s等于t时返回0,s大于t时返回1.在主函数中任意输入4个字符串,利用该函数求最小字符串,并输出该字符串.(本人初学C++,希望各位大神指教) 解决方案 这里有http://blog.csdn.net/yushuai007008/article/details/7626446 解决方案二: int strcomp(char *s, char *t) { while (*s !=

张高兴的 Windows 10 IoT 开发笔记:三轴数字罗盘 HMC5883L

原文:张高兴的 Windows 10 IoT 开发笔记:三轴数字罗盘 HMC5883L 注意,数据不包含校验,准确的来说我不知道怎么校验,但方向看起来差不多是对的... GitHub:https://github.com/ZhangGaoxing/windows-iot-demo/tree/master/HMC5883L

VMware 虚拟化编程(10) — VMware 数据块修改跟踪技术 CBT

目录 目录 前文列表 数据块修改跟踪技术 CBT 为虚拟机开启 CBT CBT 修改数据块偏移量获取函数 QueryChangedDiskAreas changeId 一个 QueryChangedDiskAreas 的 DEMO 应用 QueryChangedDiskAreas 设计的增量差异备份算法 前文列表 VMware 虚拟化编程(1) - VMDK/VDDK/VixDiskLib/VADP 概念简析 VMware 虚拟化编程(2) - 虚拟磁盘文件类型详解 VMware 虚拟化编程(3

编程-10个随机数,需要分为两组,每组分别5个数,要求两个组的和值最接近

问题描述 10个随机数,需要分为两组,每组分别5个数,要求两个组的和值最接近 比如一个数组10个随机数,需要分为两个数组,每个数组5个数,要求两个数组的和值最接近,怎么编程? 解决方案 动态规划 http://my.oschina.net/wizardpisces/blog/114538 解决方案二: 先排序,然后最小值与最大值搭配,就像从1到99求和一样 解决方案三: 1.先产生10个随机数.2.5层循环生成第一组组合,剩下的是另一组.3.循环生中,比较二组和之差,记录下来(二组结合和差).每