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
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
轉載于:https://www.cnblogs.com/GoGoagg/archive/2011/08/24/2152032.html
總結
以上是生活随笔為你收集整理的Cocoa原理指南-学习和实践1的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: css之align 与 valign 的
- 下一篇: sql小技巧