初學mac開發

昨天看來一天官方文檔.由於從ios轉向mac開發.有很多相似之處.


相似之處:mac os 10.7開始使用sandbox.


不同之處:

1,路徑問題:

Applications directory     (/Applications)

Regardless, you should not need to use this path directly. To access resources inside your application bundle, use an NSBundle object instead.

應用程序路徑,官方說沒用,有用沒用以後再說.

Home directory

10.7使用sandbox,這個路徑是隻sandbox的根目錄

10.7以前,這個路徑是/Users

NSHomeDirectory()函數獲得

Library directory

10.7中和ios中一樣.有個library的文件夾

10.7以前文檔中也沒說.猜測是/Library和~/Library

NSLibraryDirectory()函數獲得

Application Support directory

分爲所有用戶和當前用戶.如果想要所有用戶都使用,那就要使用NSLocalDomainMask,如果是當前用戶NSUserDomainMask

NSFileManager* fileManager = [NSFileManager defaultManager];

NSURL* appSupportDir = nil;

NSArray *urls = [fileManager URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask];

if ([paths count] > 0) {

   appSupportDir = [[urls objectAtIndex:0] URLByAppendingPathComponent:@"com.example.MyApp"];

}

或者也可以查找

NSArray * temparray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);


Caches directory

Movies directory

Music directory

Pictures directory

上述幾個和application support的獲得方法一樣.

Temporary directory

路徑通過NSTemporaryDirectory()獲得


2,Automatic and Sudden Termination

Automatic Termination,在用戶點擊紅叉的時候,自動退出.

2種開啓方式

  • Include the NSSupportsAutomaticTermination key (with the value YES) in the application’s Info.plist file.
  • Use the NSProcessInfo class to declare support for automatic termination dynamically. You can also use this class to change the default support of an application that includes the NSSupportsAutomaticTermination key in its Info.plist file.

Sudden Terminnation,在系統關機,或註銷的時候使用.

2種開啓方式

  • Include the NSSupportsSuddenTermination key (with the value YES) in the application’s Info.plist file.
  • Use the NSProcessInfo class to declare support for sudden termination dynamically. You can also use this class to change the default support of an application that includes the NSSupportsSuddenTermination key in its Info.plist file.

3,Garbage Collection

在build setting中搜索garbage可以找到它的設置
unsupport,關閉
support,同時支持retain/release和垃圾回收機制
required,只開啓垃圾回收機制,retain,release失去作用.

GC的優缺點:

Garbage collection offers some significant advantages over a reference-counted environment:

  • Most obviously, it typically simplifies the task of managing memory in your application and obviates most of the memory-related problems that occur, such as retain cycles.
  • It reduces the amount of code you have to write and maintain, and may make some aspects of development easier—for example, zeroing weak references facilitate use of objects that may disappear.
  • It usually makes it easier to write multi-threaded code: you do not have to use locks to ensure the atomicity of accessor methods and you do not have to deal with per-thread autorelease pools. (Note that although garbage collection simplifies some aspects of multi-threaded programming, it does not automatically make your application thread-safe. For more about thread-safe application development, see Threading Programming Guide.)

Garbage collection does though have some disadvantages:

  • The application’s working set may be larger.
  • Performance may not be as good as if you hand-optimize memory management (for more details, see “Performance”).
  • A common design pattern whereby resources are tied to the lifetime of objects does not work effectively under GC.
  • You must ensure that for any object you want to be long-lived you maintain a chain of strong references to it from a root object, or resort to reference counting for that object.
個人習慣,不開啓..影響效率,類內成員調用不方便..也可能是我以前用C++的原因.可能java程序員喜歡..
把GC開啓後,我有種用java寫android的感覺,每次調用控件,還要重新獲得其指針,才能使用.不在我掌握中啊..
發佈了30 篇原創文章 · 獲贊 6 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章