A small example to introduce Obj-C function naming
3991 단어 function
Especially when I open AppDelegate.m, the code that catches my eye turns out to be:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
return YES;
}
I don't know if there will be friends who feel the same way I did - oh my God, this is too long! ! !
In fact, in Obj-C, the function name of the above function should be:
- (BOOL)application:didFinishLaunchingWithOptions:
The application and launchOptions following the parentheses are the parameters used inside the function.
We can interpret this function as "application:finished launch using options:".
The parameter after the "application"colon is, as the name suggests, the application itself.
The parameter after the colon in "Complete Startup Using Options", as the name suggests, is the startup option.
Here I use two "as the name suggests", yes, as the name suggests! The way of naming functions in Obj-C is a bit similar to natural language. I believe that everyone, like me, will become more and more comfortable with or even like this naming method with the gradual deepening of iOS development.
Let's take another example. If we define an addition function in java, it will probably be as follows:
/**
* a+b
* @param a
* @param b
* @return a+b
*/
public int add(int a, int b) {
return a + b;
}
When defining functions in java, we are usually used to writing a simple comment, so that we can use eclipse's smart prompts to see what these parameters mean in other places in the program, and we can also use the Javadoc tool to generate program documentation . This is very convenient and makes sense!
And how would we do it in Obj-C? First define a function, the code is as follows:
- (NSInteger)addA:(NSInteger)a withB:(NSInteger)b {
return a + b;
}
When calling this function, Xcode's prompt will look like this:
[self addA:(NSInteger) withB:(NSInteger)];
At this point, we can interpret this sentence as "add A and B". Do you think it is easier to understand? It should be, hehe.
In Obj-C, if you develop good function naming habits. I believe that over time, you will find that there will be fewer and fewer comments to write. Although there is no intellisense similar to eclipse in Xcode, this does not prevent us from seeing at a glance what each function does, what parameters they require, and even what types of parameters should be.
Let's enjoy a happy iOS development journey together~~~
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
콜백 함수를 Angular 하위 구성 요소에 전달이 예제는 구성 요소에 함수를 전달하는 것과 관련하여 최근에 직면한 문제를 다룰 것입니다. 국가 목록을 제공하는 콤보 상자 또는 테이블 구성 요소. 지금까지 모든 것이 구성 요소 자체에 캡슐화되었으며 백엔드에 대한 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.