简体   繁体   English

Obj-C方法:使用参数进行调用

[英]Obj-C methods: calling with parameters

I've been using C-style functions, but I just learned they can't see instance variables. 我一直在使用C风格的函数,但我刚刚了解到它们看不到实例变量。 So I was advised to convert them to Objective-C methods. 因此建议我将它们转换为Objective-C方法。

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? 我误会了什么?

It looks like your call is incorrect. 您的通话似乎不正确。 Perhaps try: 也许尝试:

NSString *myString = [self myfunc:x];

As far as I understand, you send the -myfunc: message to a NSString object. 据我了解,您将-myfunc:消息发送到NSString对象。 So the -myfunc: method should be declared in NSString class (or a category of NSString ). 因此, -myfunc:方法应在NSString类(或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];

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: 开始时 ,有一个C函数返回一个指向NSString对象的指针:

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: 然后 ,您想要在该函数中添加一些有关UIImage对象的代码:

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). 如果要访问.image实例变量,则此方法必须属于image1对象的类(假设这是AlanImage类)。 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: 我的调查倾向于使您的image对象成为良好的候选人:

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! 无论如何,似乎您正在学习Objective-C和面向对象的概念:祝贺并保持动力!

You haven't worked out what Object Oriented Programming is. 您还没有弄清楚什么是面向对象的编程 With [theObject method] you can only call methods belonging to the specific instance. 使用[theObject method]您只能调用属于特定实例的方法。

I am not sure that following trick correctly work for a "general" objective-c, but in apple implementation you can do such: 我不确定以下技巧对“通用” objective-c是否正确起作用,但是在苹果实现中,您可以这样做:

@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. 我使用这种技巧在可观察的私有简单函数上划分了一个大的objc方法。 These functions (a) do not pollute class interface and (b) are invoked faster than objc method. 这些函数(a)不会污染类接口,并且(b)调用速度比objc方法快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM