日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Cocoa原理指南-学习和实践1

發布時間:2025/3/20 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。