ios - swift函数的一些疑惑
ringa_lee
ringa_lee 2017-04-17 17:39:31
0
5
316

学习swift的时候看到一些关于函数的命名规范和使用的疑惑。

比如下面这个函数,它是首字母大写的,同时它是定义在类外部的。

let data = UIImagePNGRepresentation(image)
  1. 这种写在类外面的函数叫做什么函数呢?

  2. 什么时候需要写在类外面呢?

  3. 为什么它是首字母大写呢?

ringa_lee
ringa_lee

ringa_lee

reply all(5)
大家讲道理

This is a function, and the one in the class is called a method. This is a C function. C has no type, so it can only be written like this. The first letter is capitalized. It is a function under UIKit, so there is UI in front of it.

迷茫
  1. Generally, the internal part of Class is called Method, which is the method; while the external part is called Function, which is the function.

  2. When you write it outside, this function does not need to be removed through Class or Class instance when calling.

  3. The reason why the first letter is capitalized is because of Apple’s ancient tradition. Because there is no such thing as namespace in the Objective-C era, name conflicts may occur in many cases. Therefore, Classes in the Cocoa framework are all prefixed, such as NS, UI, etc. (to give a simple example: UIImage and CGImage, you can imagine if there is no prefix). And Apple agrees that prefixes are all in uppercase, so this is the case.

洪涛
  1. Generally, the internal part of Class is called Method, which is the method; while the external part is called Function, which is the function.

  2. When you write it outside, this function does not need to be removed through Class or Class instance when calling.

  3. The reason why the first letter is capitalized is because of Apple’s ancient tradition. Because there is no such thing as namespace in the Objective-C era, name conflicts may occur in many cases. Therefore, Classes in the Cocoa framework are all prefixed, such as NS, UI, etc. (to give a simple example: UIImage and CGImage, you can imagine if there is no prefix). And Apple agrees that prefixes are all in uppercase, so this is the case.

阿神

public func after_delay(time:NSTimeInterval,block: dispatch_block_t){
let time_uint64:UInt64 = UInt64(time)
let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(time_uint64 * NSEC_PER_SEC))
dispatch_after(delay, dispatch_get_main_queue(), block)
}
Just like this, you can also write such a function yourself. When you call it, you don’t need to use a class or structure enumeration to call it. You can use it directly

 after_delay(1, block: { () -> Void in
     alert(notice, dismiss: false)
 })
Peter_Zhu

UIImagePNGRepresentation() This is the C function. OC can compile C language. This function is defined in the UIKit framework.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template