views:

340

answers:

5

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 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?

+1  A: 

It looks like your call is incorrect. Perhaps try:

NSString *myString = [self myfunc:x];
Abizern
[self myfunc:x] works! Thanks!
Alan
A: 

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

Georg
Well, that's true :) --- I am trying to learn though.
Alan
+1  A: 

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];
mouviciel
A: 

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.

lazyden
+1  A: 

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!

mouviciel