Reputation: 47
I use the same code at many different locations in my app code. The code is very long and complicated and it's taking up a lot of space having the same code at multiple places.
Is there any way to make it into a function so I can type something like longCode; and it runs all the code I have written in the function when I defined it so I can save some space?
Upvotes: 0
Views: 222
Reputation: 11597
you seemed to answer you own question in the question... put the code in a function, better yet a class/static function.
define a class function like so:
+ (returnType) theFunction: (SomeType*) aParameter {
}
define it in a header as well then you can just import the header
take a look at this for more detail on it
Upvotes: 0
Reputation: 5945
Yes, you can. There is a lot of ways to do it. For example creating class with one public static method.
@interface YourClass : NSObject
+ (int)longCode:(int)x;
@end
Now you can use your code just like:
int y = [YourClass longCode:x];
Upvotes: 2