search

Home  >  Q&A  >  body text

Objective-C无私有方法问题

如题,Objective-C无私有方法是什么意思?确实我在.h文件中声明的所有方法除了静态方法就是public方法,但是我再.m文件中随便声明的方法,子类是看不到的,当然performselector还是可以执行的,但是这样算不算是一种私有方法呢?

PHP中文网PHP中文网2807 days ago462

reply all(4)I'll reply

  • PHPz

    PHPz2017-04-22 09:01:28

    It is recommended to take a look at http://blog.sunnyxx.com/2014/04/13/objc_dig_interface/

    reply
    0
  • PHP中文网

    PHP中文网2017-04-22 09:01:28

    obj c does not have strict private methods. This is determined by its method calling mechanism. Calling a certain method of an object in obj c is actually sending a message to the object, and the object sees if it can handle the message. , if it can be handled, the corresponding method will be called to implement it. If it cannot be handled, an exception will be thrown. This is completely a runtime action. So even if a method is not declared in the header file, it can still be called at runtime, and it is no different than if you declared it in the header file. At most, Xcode will give you a warning.

    The same goes for static methods, because a class itself is also an object. Calling a static method means sending a message to this object.

    The so-called private method (not defined in the header file), I personally think is more of a constraint that programmers place on themselves. For example, if it is not placed in the header file, it means that it is not exposed. This method is You don't want it to be called outside, although you can still use it if you want to.

    Refer to this article, it’s very detailed and clear: http://blog.jobbole.com/45963/

    reply
    0
  • 怪我咯

    怪我咯2017-04-22 09:01:28

    Yes, categories and extensions in interface are equivalent to private methods.
    Category: class name + extension method; extension: @implementation area
    You can search for it.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-22 09:01:28

    All methods declared in .h are public by default.
    If you want it to be private, there is no need to declare it in .h, just implement it directly in .m. It is recommended that all private methods start with p_, ex:
    - (void) p_myFirstMethod
    {
    //todo..
    }

    reply
    0
  • Cancelreply