Home > Software design >  How do I access a method in a class with 2 parameters defined on @Interface line in Objective-C? [cl
How do I access a method in a class with 2 parameters defined on @Interface line in Objective-C? [cl

Time:09-16

I have an Objective-C MacOS project containing a class NSImage Rotated.

The class .m file contains the line @implementation NSImage (Rotated) and the .h file contains the line @interface NSImage (Rotated).

How can I access methods in this class from a different class?

CodePudding user response:

You asked:

How can I access methods in this class from a different class?

Your other class should simply import the @interface (but not the @implementation) of your category. E.g. the .m of your other class should:

#import "NSImage Rotated.h"

The compiler will successfully recognize the category’s various methods within that .m file.

CodePudding user response:

Import your #import "NSImage Rotated.h" in the class you want to use it, next use the extension properly like this:

[[NSImage imageWith...] imageRotated:90];
// or better
NSImage *rotatedImage = [[[NSImage alloc] initWithData:theData] imageRotated:90]; 

where imageRotated:(float) is your extension method you want to use.

[NSImage alloc] initWithData:theData] is just an example, change with your real method.

CodePudding user response:

This are not Parameters, in Objective-C they are called Category.

read more in those Apple Docs

In Fact you use them all the time

@interface SomeClass 
@property NSString* someString;
-(int)someGetterMethod;
-(void)setSomeStringWith:(NSArray*)array;
@end
//------------------------
@implementation SomeClass () // <-- empty Category name
//...
@end

As you see the empty category name is addressing/declaring the baseclass of course. So when you have imported a named category you are extending or changing this empty named category.

You need to import your extending category class where you want to access the given methods or changes to the baseclass

#import "NSImage Rotated.h"

Then just call the methods as usuall. As you didn't give an example of method declarations in your Rotated Category of NSImage you do not call the Category anywhere directly. But you can call its methods once the category has extended the baseclass by instancing an NSImage.

Means just importing the NSImage Rotated.h already changes the predecessors declaration where you do so, often there is nothing to call to make it work. You just allocate your NSImage with [NSImage alloc] or [NSImage imageWith...

  • Related