[英]Calling Obj-C Methods
I want to call a method B using a different method A. The problem is that the aksed parameters of method B is not present in Method A. here's what I've tried .. 我想使用其他方法A调用方法B。问题是方法A中没有方法B的参数。这就是我尝试过的方法。
-(void) methodA {
// some code
CGSize *size = [CGSizeMake(self.frame.size.width, self.frame.size.height)];
[self methodB:size];
}
-(void) methodB:(CGSize) size {
//some code
}
There is certainly a better way... 当然有更好的方法...
Thanks 谢谢
I don't think your code will be compiled. 我认为您的代码不会被编译。
CGSize is not an object. CGSize不是对象。 Refactor to:
重构为:
CGSize size = CGSizeMake(self.frame.size.width, self.frame.size.height);
[self methodB:size];
You should only wrap []
around methods. 您只应在方法周围包裹
[]
。 CGSizeMake(self.frame.size.width, self.frame.size.height)
is a function, not a method, so get rid of the square braces. CGSizeMake(self.frame.size.width, self.frame.size.height)
是一个函数,而不是一个方法,因此请除去方括号。 You can tell it's a function because functions look like functionName(argument1, argument2, ...)
whereas methods look like [object methodName:argument1 methondNameContinued:argument2]
. 您可以说这是一个函数,因为函数看起来像
functionName(argument1, argument2, ...)
而方法看起来像[object methodName:argument1 methondNameContinued:argument2]
。
Further, CGSizeMake
returns a CGSize
structure, not a pointer to a CGSize
structure, so ditch the *
in *size
. 此外,
CGSizeMake
返回CGSize
结构,而不是一个指向CGSize
结构,因此沟*
在*size
。
Then you'll be left with this: 然后,您将得到以下结果:
CGSize size = CGSizeMake(self.frame.size.width, self.frame.size.height);
[self methodB:size];
Which is correct. 哪个是对的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.