在目标c中创建实例 [英] creating instances in objective c

查看:73
本文介绍了在目标c中创建实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

//ECHOAppDelegate.m
@implementation ECHOAppDelegate
 ...
 @end

//PtyView.m
 @interface PtyView (PtyPrivate)
 -(void)startTask;
 -(void) didRead: (NSNotification *)fileNoty;
 @end

 @implementation PtyView
 ...
 -(void)startTask {
 //starts task
 }
 @end

现在,如何从ECHOAppDelegate.m触发"startTask"?我需要创建一个实例吗?我是一个初学者:D

Now, how do I trigger "startTask" from ECHOAppDelegate.m? I need to create an instance? I'm a total beginner :D

任何示例代码都很棒!

谢谢, 以利亚

推荐答案

-(void)startTask;似乎是私有实现,理论上不应从外部类调用.

-(void)startTask; appears to be private implementation and in theory should not be called from external classes.

要回答您的问题,您可以这样称呼它:

To answer your question, you can call it something like this:

PtyView *v = [[PtyView alloc] init];
[v startTask];
[v release];

尽管您会收到一条警告,说PtyView可能不响应startTask.由于它不在类的公共接口中.

Though you will get a warning saying, PtyView might not respond to startTask. Since it is not in public interface of class.

更新:上面的代码假定startTask返回时,您已完成此对象.但是有些事情告诉我,您可能正在使用异步回调.如果是这种情况,那么startTask可能会立即返回,并且您将不会在那里释放它.通常,在这种情况下,PtyView将通知您任务的完成.因此,您可以在任务完成后将其释放.

Update: Above code assumes that when startTask returns, you are done with this object. But something tells me that you might be using async callbacks. If that is the case then startTask might return immediately and you won't release it then and there. Normally in this case, you will be notified by PtyView about the completion of task. So you release it when the task is complete.

Update2 : 公开方法很容易.您只需在公共接口(类的头文件)中声明它即可:

Update2: Making a method public is easy. You just declare it in the public interface (the header file of class):

//in PtyView.h
@interface PtyView
-(void)startTask;
@end

//in PtyView.m
@implementation PtyView
...
-(void)startTask {
//starts task
}
@end

请注意,接口声明中未定义类别.

Notice that there is no category defined in the interface declaration.

这篇关于在目标c中创建实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆