Home  >  Q&A  >  body text

ios - swift函数的一些疑惑

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

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

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

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

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

ringa_leeringa_lee2717 days ago280

reply all(5)I'll reply

  • 大家讲道理

    大家讲道理2017-04-17 17:41:31

    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.

    reply
    0
  • 迷茫

    迷茫2017-04-17 17:41:31

    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.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 17:41:31

    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.

    reply
    0
  • 阿神

    阿神2017-04-17 17:41:31

    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)
     })

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 17:41:31

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

    reply
    0
  • Cancelreply