简体   繁体   English

“ a”前缀在aString中代表什么?

[英]What does the 'a' prefix represent in aString?

I'm just looking at NSString.h and wonder why some method declaration args use (NSString *)string; 我只是看着NSString.h ,想知道为什么某些方法声明args使用(NSString *)string; while others use (NSString *)aString . 其他人则使用(NSString *)aString

What are the conventions practiced here? 这里有哪些惯例?

It's simply the indefinite article "a" , as in the parameter is "a string". 它只是不定冠词 “ a” ,因为参数中是“字符串”。 It appears in some methods to make them read a little more like English. 似乎有些方法使他们读起来更像英语。 For example, compare:(NSString*)aString reads as "compare a string". 例如, compare:(NSString*)aString读为“比较字符串”。 The official naming conventions for method parameters are rather basic; 方法参数的正式命名约定非常基础; they show the use of the indefinite article but don't explain why. 它们显示了不定冠词的用法,但没有解释原因。

The object-oriented half of Objective-C comes from Smalltalk, and so does this convention. Objective-C的面向对象的一半来自Smalltalk,该约定也是如此。

Smalltalk was originally designed to be used by children, without them having to learn programming. Smalltalk最初旨在供儿童使用,而无需学习编程。 Later on that goal was relaxed a bit and it was more targeted at domain experts for doing end user programming. 后来,这个目标放宽了一点,它更针对领域专家进行最终用户编程。 In both cases, the language designers felt that the language should be a compelling user interface, and thus they paid very close attention to even the smallest details, like the naming of parameters in method signatures. 在这两种情况下,语言设计人员都认为该语言应该是一种引人注目的用户界面,因此他们甚至非常注意最小的细节,例如方法签名中的参数命名。

Method signatures themselves are fairly unusual in Smalltalk (and Objective-C), in that the arguments and parameters are not written after the name (as in pretty much every other language ever invented), but rather the name of the method gets broken up into pieces and the arguments and parameters go between them. 方法签名本身在Smalltalk(和Objective-C)中是很不常见的,因为参数和参数不是 名称之后写的(就像在几乎所有其他发明的语言中一样),而是方法的名称被分解为件和参数和参数之间 This makes for some strange looking method names, but when you read the entire signature with the arguments or parameters, then it reads almost like a sentence in the domain jargon of whatever domain that particular method operates in. 这使方法名称看起来有些奇怪,但是当您读取带有参数或参数的整个签名时,它几乎像特定方法所作用的任何域的域术语中的句子一样读取。

So, in some run of the mill OO language, a method for storing a value at a particular index in an array might look like this: 因此,以某种面向对象的语言,一种将值存储在数组中特定索引处的方法可能看起来像这样:

store(i, obj)

or even worse, like this: 甚至更糟,像这样:

public void store(int i, T obj)

or this: 或这个:

store(self, i, obj)

er even this (from my favorite language): 甚至(从最喜欢的语言):

[]=(i, obj)

(How do you even pronounce that?) (你怎么发音?)

and might be called like this: 可能会这样称呼:

ary.store(3, "Hello")

or this: 或这个:

ary[3] = "Hello"

or maybe with little bit less punctuation (in Io and Ioke, for example): 可能与有点少标点(在Io场和伊欧凯,例如):

ary store(3, "Hello")

In Smalltalk, the signature looks like this: 在Smalltalk中,签名如下所示:

at: anInteger put: anObject

and it is called like this: 它的名称如下:

anArray at: 3 put: "Hello".

If you read the signature out loud, it becomes clear that there simply has to be an indefinite article there, otherwise it just wouldn't read right. 如果你读了签名出声,很明显,有只需简单地是一个不定冠词存在,否则它只是不会看错。

Note some of the differences: in the mainstream versions, parameters are only distinguished by their positions. 请注意其中的一些区别:在主流版本中,参数仅根据其位置进行区分。 So, in order to figure out what they mean, you give them descriptive names. 因此,为了弄清楚它们的含义,请给它们提供描述性名称。 But when you actually call the method, those descriptive names are gone, because the parameters get replaced with the arguments. 但是,当您实际调用该方法时,这些描述性名称已消失,因为参数已替换为参数。 (And yes, I am well aware that i isn't exactly a descriptive name. If you want to, you can repeat the experiment with something like copy(source, destination) and copyFrom: aDirectory to: anotherDirectory ). (是的,我很清楚i不是一个描述性的名字。如果愿意,可以使用copy(source, destination)copyFrom: aDirectory to: anotherDirectory类的内容重复该实验)。

In the Smalltalk example, however, the semantics of the parameter are encoded in the method name, thus freeing up the parameter name for other usages. 但是,在Smalltalk示例中,参数的语义被编码在方法名称中,从而为其他用途腾出了参数名称。 In this case, type annotations (since Smalltalk is implicitly typed) or, well, basically some form of Hungarian Notation. 在这种情况下,使用类型注释(因为Smalltalk是隐式键入的),或者基本上是某种形式的匈牙利注释。 Also, when you call the method, the encoded meaning is still there in the method name. 同样,当您调用该方法时,方法名称中仍保留编码的含义。 (In the example above, when you read copy(a, b) , you don't know whether this is copying from a to b or the other way round, but with copyFrom: a to: b. there is no ambiguity. You could fix that by renaming the method: copyFromTo(a, b) , but that doesn't read very well, either.) (在上面的示例中,当您阅读copy(a, b) ,您不知道它是从a复制到b还是以其他方式进行复制,但是使用copyFrom: a to: b.没有歧义。您可以通过重命名方法copyFromTo(a, b)来解决此问题,但这也不是很好。)

Now, in Objective-C, all of this gets a little lost, because of the type annotations. 现在,在Objective-C中,由于类型注释,所有这些都变得有些丢失。 Compare these two signatures, which basically describe the same method: 比较这两个签名,它们基本上描述了相同的方法:

at: anInteger put: anObject
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index

Somehow, the "read it out loud as an English sentence" part got lost, but the "an" prefix for anObject is still there. 不知何故,“大声朗读为英语句子”部分丢失了,但是anObject的“ an”前缀仍然存在。

Anyway, to make a long story short: the reason for those prefixes has to do with Objective-C's heritage. 无论如何,总而言之:这些前缀的原因与Objective-C的传统有关。

This is often done in setters and other method declarations to avoid name clashes with instance variables of the class. 通常在设置器和其他方法声明中执行此操作,以避免名称与类的实例变量发生冲突。 Often in setters you'll see: 通常在二传手中,您会看到:

variable = aVariable 变量=变量

'aString' is just the name of the input variable, internal to the method. 'aString'只是方法内部的输入变量的名称。 Most probably the function already uses a variable called 'string' so it uses 'aString' (like 'a string' or 'some string') to distinguish between them, while keeping the name general. 该函数很可能已经使用了一个名为“字符串”的变量,因此它使用“ aString”(例如“字符串”或“某些字符串”)来区分它们,同时保持名称的通用性。 That's what I believe anyway. 无论如何,这就是我所相信的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 mc_mobile_tunnel在崩溃日志中表示什么? - What does mc_mobile_tunnel represent in a crash log? 快速枚举(循环)如何在Objective-C中工作? (即:for((字典中的NSString * aString)...) - How does Fast Enumeration (looping) work in Objective-C? (ie: for (NSString *aString in aDictionary)…) iOS 文件路径上的 /private 前缀表示什么? - What does the /private prefix on an iOS file path indicate? 在 iPhone/iOS 上使用“颜色未对齐的图像”时,黄色代表什么 - What does yellow tinting represent when using “color misaligned images” on iPhone/iOS if([aString writeToFile:@“outfile”atomically:YES]) - if([aString writeToFile: @“outfile” atomically: YES]) 什么是注册前缀? - What is a registered prefix? ARKit 变换矩阵中的值代表什么? - What do values in ARKit transform matrices represent? 用CocoaTouch(UIKit)表示线性过程的正确方法是什么? - What's the correct way to represent a linear process in CocoaTouch (UIKit)? 低,中,高的AVCaptureSession设置,它们各自代表什么? - AVCaptureSession settings of Low, Medium and High, what res do they each represent? 在 iTunes Connect 上,单位是否代表我的应用程序在特定国家/地区的下载量? - on itunes connect, does units represent no of downloads of my application in particular country?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM