Using (id) in Objective-C

前端 未结 5 779
独厮守ぢ
独厮守ぢ 2020-12-09 22:01

I have a function that I want to operate on two different custom objects. My first thought was to accept the argument as an (id) and operate on the id object. I can\'t quit

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-09 22:24

    While you can send a message to any object (id) - property accessors require that the compiler be aware of the type you are dealing with - this is because property accessors are syntactic sugar around calling specific getter and setter methods.

    You have a few of ways of working around this:

    1. Instead of accessing the count property, call the corresponding [getCount] methods.

    2. If the different classes have different versions of this method, you can use a runtime type check:

    3. Provide a base class for both types so that you can pass in something more specific than (id).

    4. Define and implement a Protocol that both objects implement that defines a count property (or method).

    Example of a dynamic type check:

    if( [object isKindOfClass:[Apple Class] )
       // call one overload of getCount
    else if( [object isKindOfClass:[Orange Class] )
       // call another overload of getCount
    

    Personally, I favor strong typing in my code because it makes it easier to understand the intent. It also allows the IDE to support your coding effort with intellisense, static analysis, and refactoring features. So, in your case, I would use either #3 or #4 as an approach - depending on whether inheritance is really appropriate for the problem.

提交回复
热议问题