0

I have two implementation in a single file. Like the one below. How do I access the second's method in the first? If I try to do that the compiler throws an unknown selector error.

I know that in C you have to have methods which have to have hierarchy in definitions to be able to build. Is this the same in ObjectiveC also? Are there any alternatives to this other than defining the second implementation above the first?

@implementation BaseClass

  -(void)someMethod {
   XCUIElementQuery *elementQuery = [[XCUIApplication alloc] init].tables
   [elementQuery anotherMethod];  //How do I use the category method here?
  }
@end


@implementation XCUIElementQuery (BaseClassCategory)

  -(void)anotherMethod {
   //do something
  }
@end
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Aswath
  • 1,236
  • 1
  • 14
  • 28

2 Answers2

1

At the top of the file, just declare an interface for XCUIElementQuery like so:

@interface XCUIElementQuery (XCUIElementQuery_private)
-(void)anotherMethod;
@end
TyR
  • 718
  • 4
  • 9
  • 1
    This needs to be `@interface XCUIElementQuery (BaseClassCategory)` – rmaddy Sep 16 '16 at 20:44
  • No it doesn't http://stackoverflow.com/questions/11679885/xcode-now-generates-an-empty-category-why – TyR Sep 16 '16 at 20:47
  • Your answer adds the declaration to the class extension. My suggestion adds the declaration to the class category. Those are two different things. The OP is asking about a class category. Therefore the declaration should be added to the class category, not the one and only class extension. – rmaddy Sep 16 '16 at 20:49
  • From an architectural standpoint I'm happy with either a category or a class extension for this- both are used widely for this purpose in Apple sample code. I'll change it to a category to make everybody happy. – TyR Sep 16 '16 at 21:28
  • It's not a matter of making you happy. The OP has a specific category they have created. The forward declaration you propose should be applied to that specific category. It should not be added to the private class extension nor should it be added to some arbitrary category. – rmaddy Sep 16 '16 at 21:33
0

Just swap the two sets of implementations.

@implementation XCUIElementQuery (BaseClassCategory)

-(void)anotherMethod {
   //do something
}
@end

@implementation BaseClass

-(void)someMethod {
    XCUIElementQuery *elementQuery = [[XCUIApplication alloc] init].tables
    [elementQuery anotherMethod];  //How do I use the category method here?
}

@end
rmaddy
  • 314,917
  • 42
  • 532
  • 579