Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
822 views
in Technique[技术] by (71.8m points)

creating instances in objective c

Here is my code:

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

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

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

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

Any example code would be awesome!

Thanks, Elijah

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

-(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];

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

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: 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...