Cocoa原理指南-学习和实践1
【bolg目标】
bolg仅仅针对<Cocoa原理指南>书中代码进行本地测试和文档学习,书中理论不进行摘要
个人觉得此书值得推荐阅读,从整体上学习Cocoa
【实践环境】
Mac 10.6
XCode4
【正文】
Pdf版
Cocoa整体纵览图,在书中页码:
Foundation
P15
P16
P17
Application Kit
P20
P21
/**************************************************/
下面分析下P27书中代码
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSArray *args = [[NSProcessInfo processInfo] arguments];
NSCountedSet *cset = [[NSCountedSet alloc] initWithArray:args];
NSArray *sorted_args = [[cset allObjects]
sortedArrayUsingSelector:@selector(compare:)];
NSEnumerator *enm = [sorted_args objectEnumerator];
id word;
while (word = [enm nextObject]) {
printf("%s\n", [word UTF8String]);
}
[cset release];
[pool release];
return 0;
}
比对资料学习,如下
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
NSProcessInfo
此类是对当前进程信息的访问
The NSProcessInfo class provides methods to access information about the current process. Each process has a single, shared NSProcessInfo object, known as process information
agent.
Getting the Process Information Agent
+ processInfo
Accessing Process Information
– arguments
– environment
– processIdentifier
– globallyUniqueString
– processName
– setProcessName:
Sudden Application Termination
– disableSuddenTermination
– enableSuddenTermination
Getting Host Information
– hostName
– operatingSystem
– operatingSystemName
– operatingSystemVersionString
Getting Computer Information
– physicalMemory
– processorCount
– activeProcessorCount
– systemUptime
关于上面罗列的,一目了然的明白这些方法的目的
本例使用processInfo和arguments
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
NSCountedSet 继承NSMutableSet : NSSet : NSObject
The NSCountedSet class declares the programmatic interface to a mutable, unordered collection of indistinct objects. A counted set is also known as a bag.
在本例,此类用于统计重复输入对象,在后续的调整代码有这么一句输出
NSLog(@"%@,%lu" ,item ,[cset countForObject:item]);
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
sortedArrayUsingSelector:属于NSArray类的,用于数组排序
@selector(compare:),通过文档,发现其中compare:这个函数来自NSNumber类{?这个是现行理解},其原型:
- (NSComparisonResult)compare:(NSNumber *)aNumber
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
按照书中描述,
SimpleCocoaTool a z c a l q m z
a
c
l
m
q
z
实际在调试中发现出现数据类似
/Users/...../SimpleCocoaTool
a
c
l
m
q
z
比书中所写代码多一个行输出
至于为什么会这样,要么书中作者截获,要么Xcode版本问题
下面在XCode4下进行下面改进
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
为了在调试中实践,最后结果类似书中类似输出,改进原有代码:
int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableArray *args = [[NSMutableArray alloc] initWithArray:[[NSProcessInfo processInfo] arguments]];//把NSArray换成NSMutableArray
[args removeObjectAtIndex:0];//移出第一个参数
NSCountedSet *cset = [[NSCountedSet alloc] initWithArray:args];
for(id item in cset)
{
NSLog(@"%@,%lu" ,item ,[cset countForObject:item]);
}
NSArray *sorted_args = [[cset allObjects]
sortedArrayUsingSelector:@selector(compare:)];
NSEnumerator *enm = [sorted_args objectEnumerator];
id word;
while (word = [enm nextObject]) {
printf("%s\n", [word UTF8String]);
}
for(id item in sorted_args)
{
NSLog(@"for %@" ,item);
}
[cset release];
[pool release];
return 0;
}
现在测试
SimpleCocoaTool a z c a l q m z
a
c
l
m
q
z
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
无论生活、还是技术,一切都不断的学习和更新~~~努力~