Tutorial :Obj-C methods: calling with parameters



Question:

I've been using C-style functions, but I just learned they can't see instance variables. So I was advised to convert them to Objective-C methods.

NSString* myfunc ( int x )  

becomes:

- (NSString *)myfunc:(int)x  

and

myString = myfunc(x);  

becomes

myString = [myString myfunc:x];  

??

This compiles with ominous warnings, but does not work. What have I misunderstood?


Solution:1

It looks like your call is incorrect. Perhaps try:

NSString *myString = [self myfunc:x];  


Solution:2

As far as I understand, you send the -myfunc: message to a NSString object. So the -myfunc: method should be declared in NSString class (or a category of NSString).

If this is what you want to do, you don't need to return the object itself as the result of the method: you can modify its instance variables in the method implementation. The method call (or message sending) looks like:

[myString myfunc:x];  

If you want to send the message to an object of another class and return a string, your method declaration is correct but must appear in your class implementation and the receiver of the message (this is the item on the left in the square brackets) must be of that class:

@implementation MyClass  -(NSString *)myfunc:(int)x  {      NSString * returnString;      ...// do something with x, returnString and instance variables      return returnString;  }  @end;  ...  MyClass * myobj = [[MyClass alloc] init];  NSString * myString = [myobj myfunc:42];  


Solution:3

As a second answer, I am trying to understand your problem through all your recent questions.

At the beginning, there was a C function returning a pointer to a NSString object:

NSString * myfunc( int x )  {             ... // Do something with x     NSString * myString = @"MYDATA";             ... // Do something with myString     return myString;          }  

Then, you wanted to add in that function some code about an UIImage object:

image1.image = [UIImage imageNamed:@"image1.png"];  

You were advised to convert the function to a method. If you want to access .image instance variable, this method has to belong to the class of image1 object (let's say this is AlanImage class). Something like this:

@interface AlanImage : NSObject {      UIImage image;  }  - (NSString *) myfuncWithParam: (int) x;  @end;    @implementation AlanImage  - (NSString *) myfuncWithParam: (int) x  {      NSString * myString = @"MYDATA";      image = [UIImage imageNamed:@"image1.png"];      return myString;          }  @end  

Third, you didn't know what was the receiver of the method. My investigations tend to lead to your image object as a good candidate:

aNiceString = [image myfunc:aNiceInteger];  

Finally (this question), not getting a satisfying answer, you reworded your third question, with success this time as it happens.

I am curious to get a more complete view of your project in order to give you some hints. Anyway, it seems that you are learning Objective-C and object oriented concepts: congratulations and stay motivated!


Solution:4

You haven't worked out what Object Oriented Programming is. With [theObject method] you can only call methods belonging to the specific instance.


Solution:5

I am not sure that following trick correctly work for a "general" objective-c, but in apple implementation you can do such:

@interface SomeClass: NSObject {    int m_someVariable;    ...  };    - (NSString *) someMethod;  ...  @end     @implementation SomeClass  ...  //pure c function with extra one parameter   //for accessing to instance variables  static NSString privatePlainCeeMethod(SomeClass *my, int fortyTwo) {    NSString *str;    //access to a instance variable as for a usual    //cee structure field: my->fieldName    ...    return [NSString stringWithFormat:@"someVariable:%d, fortyTwo:%d",      my->m_someVariable, fortyTwo];  };    - (NSString *) someMethod  {     ...    return privatePlainCeeMethod(self,42);  };  ...  @end  

I use such trick to divide a big objc method on observable private simple functions. These functions (a) do not pollute class interface and (b) are invoked faster than objc method.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »