問(wèn)題描述
Objective-C 沒(méi)有命名空間,而且很多(例如 CocoaDevCentral 的 Cocoa 樣式指南) 建議在你的類名前加上首字母以避免命名空間沖突.
Objective-C doesn't have namespaces, and many (such as CocoaDevCentral's Cocoa Style Guide) recommend prefixing your class names with initials to avoid namespace collision.
引用以上鏈接:
Objective-C 沒(méi)有命名空間,所以在你的類名前加上縮寫(xiě).這避免了命名空間碰撞",這是一種情況兩段代碼同名但做不同的事情.
Objective-C doesn't have namespaces, so prefix your class names with initials. This avoids "namespace collision," which is a situation where two pieces of code have the same name but do different things.
我想這是有道理的.但老實(shí)說(shuō),在相對(duì)較小的應(yīng)用程序(比如 iPhone 游戲)的背景下,這真的是個(gè)問(wèn)題嗎?我真的應(yīng)該將 MyViewController 重命名為 ZPViewController 嗎?如果不是,那么命名空間沖突在什么時(shí)候真正成為一個(gè)問(wèn)題?
That makes sense, I suppose. But honestly, in the context of a relatively small app (say, an iPhone game), is this really an issue? Should I really rename MyViewController to ZPViewController? If not, at what point do namespace collisions really become a concern?
推薦答案
如果您正在編寫(xiě)一個(gè)使用某些庫(kù)集的應(yīng)用程序,那么您已經(jīng)知道您的命名空間是什么樣的,您只需要選擇不使用的名稱即可與現(xiàn)有可用功能沖突.
If you're writing an application that uses some set of libraries, then you already know what your namespace looks like and you just need to select names that do not conflict with existing available functions.
但是,如果您正在編寫(xiě)一個(gè) 庫(kù) 供他人使用,那么您應(yīng)該選擇一個(gè)合理唯一的前綴,以避免與其他庫(kù)發(fā)生名稱沖突.只有兩個(gè)字符可能仍然會(huì)發(fā)生名稱沖突,但頻率會(huì)降低.
However, if you are writing a library for use by others, then you should pick a reasonably unique prefix to try to avoid name collisions with other libraries. With only two characters there may still be name collisions, but the frequency will be reduced.
這篇關(guān)于命名空間沖突真的是 Objective-C 中的一個(gè)問(wèn)題嗎?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!