Core Foundation和Cocoa Touch强调命名约定(1或2个下划线?)

时间:2014-10-30 18:41:07

标签: ios objective-c macos cocoa-touch core-foundation

我试图通过使用乐器和探索来更深入地了解Core Foundation和Cocoa Touch系统。

Core Foundation(CF)和Cocoa Touch(UI)的下划线命名约定是什么?

例如,这是我使用其中一个应用程序时遇到的运行时错误。为什么某些CF方法以2个下划线为前缀(__CFRunLoopDoSources0,__CFRunLoopRun),而其他CF方法不是(CFRunLoopRunSpecific, CFRunLoopRunInMode)?与UI方法相同。为什么有些前缀为1个下划线( _UIApplicationHandleEventFromQueueEvent,_UIApplicationHandleEventQueue),而有些则不是(UIApplicationMain)

2014-10-30 11:02:54.929 Scratchpad3[1822:204482] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Storyboard (<UIStoryboard: 0x790806f0>) doesn't contain a view controller with identifier 'PageViewController''
    *** First throw call stack:
    (
    0   CoreFoundation                      0x0362d946 __exceptionPreprocess + 182
    1   libobjc.A.dylib                     0x032b6a97 objc_exception_throw + 44
    2   UIKit                               0x022a70fc -[UIStoryboard instantiateInitialViewController] + 0
    3   Scratchpad3                         0x00070fcf -[Analytics2ViewController viewDidLoad] + 383
    4   UIKit                               0x01de52a4 -[UIViewController loadViewIfRequired] + 771
    5   UIKit                               0x01de5595 -[UIViewController view] + 35
    6   Scratchpad3                         0x00060d75 -[IIViewDeckController setCenterController:] + 3429
    7   Scratchpad3                         0x0007087a -[SettingsViewController analytics2Clicked:] + 170
    8   libobjc.A.dylib                     0x032cc7cd -[NSObject performSelector:withObject:withObject:] + 84
    9   UIKit                               0x01c8f23d -[UIApplication sendAction:to:from:forEvent:] + 99
    10  UIKit                               0x01c8f1cf -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64
    11  UIKit                               0x01dc2e86 -[UIControl sendAction:to:forEvent:] + 69
    12  UIKit                               0x01dc32a3 -[UIControl _sendActionsForEvents:withEvent:] + 598
    13  UIKit                               0x01dc250d -[UIControl touchesEnded:withEvent:] + 660
    14  UIKit                               0x01cdf60a -[UIWindow _sendTouchesForEvent:] + 874
    15  UIKit                               0x01ce00e5 -[UIWindow sendEvent:] + 791
    16  UIKit                               0x01ca5549 -[UIApplication sendEvent:] + 242
    17  UIKit                               0x01cb537e _UIApplicationHandleEventFromQueueEvent + 20690
    18  UIKit                               0x01c89b19 _UIApplicationHandleEventQueue + 2206
    19  CoreFoundation                      0x035511df __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    20  CoreFoundation                      0x03546ced __CFRunLoopDoSources0 + 253
    21  CoreFoundation                      0x03546248 __CFRunLoopRun + 952
    22  CoreFoundation                      0x03545bcb CFRunLoopRunSpecific + 443
    23  CoreFoundation                      0x035459fb CFRunLoopRunInMode + 123
    24  GraphicsServices                    0x054cb24f GSEventRunModal + 192
    25  GraphicsServices                    0x054cb08c GSEventRun + 104
    26  UIKit                               0x01c8d8b6 UIApplicationMain + 1526
    27  Scratchpad3                         0x00071d5d main + 141
    28  libdyld.dylib                       0x040f3ac9 start + 1
    29  ???                                 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

1 个答案:

答案 0 :(得分:0)

苹果说 避免使用下划线字符作为前缀,意味着方法名称中的私有(使用 允许使用下划线字符作为实例变量名称的前缀。 Apple保留使用此功能 惯例。第三方使用可能导致名称空间冲突;他们可能会无意中重写 现有的私人方法与他们自己的方法之一,带来灾难性的后果。

另外

在大多数情况下,私有方法名称通常遵循与公共方法名称相同的规则。但是,一个 常见的约定是为私有方法提供前缀,以便很容易将它们与公共方法区分开来。 即使采用这种约定,私有方法的名称也可能导致一种特殊类型的问题。什么时候 你设计了一个Cocoa框架类的子类,你无法知道你的私有方法是否是无意的 覆盖具有相同名称的私有框架方法。 Cocoa框架中大多数私有方法的名称都有一个下划线前缀(例如,_fooData )将它们标记为私人。从这个事实出发,遵循两个建议 不要使用下划线字符作为私有方法的前缀。 Apple保留了这一惯例 如果你是一个大型Cocoa框架类(如NSView或UIView)的子类,你想成为 绝对确保您的私有方法的名称与超类中的名称不同,您可以添加 您自己的私有方法前缀。前缀应尽可能唯一,也许一个基于 在您的公司或项目以及表格&#34; XX _&#34;。因此,如果您的项目名为Byte Flogger,则为前缀 可能是BF_addObject: 虽然给私有方法名称一个前缀的建议似乎与之前的声明相矛盾 方法存在于其类的命名空间中,这里的意图是不同的:防止无意覆盖 超类私有方法。