开发者

Passing an instance of an object to a method

I have this method:

- (开发者_开发百科void)postDictionary:(NSDictionary *)dictionary toURL:(NSURL *)url forDelegate:(id)delegate withIdentifier:(NSString *)identifier;

I want to pass an instance of an object to this method and later call something like:

if ([delegate respondsToSelector:@selector(didFinishDownload:)]) {
    [delegate performSelector:@selector(didFinishDownload:) withObject:@"test"];
}

I guess I have to pass an pointer of the object to the method?

I guess I'll have to use *, ** and &. But I don't know where I have to use which of these.

Can you please help me?

SideSwipe


You'll find that all Objects in Objective C are referenced by pointers. So when you declare an NSString you do something like this:

NSString *myString = @"testString";

In the same way doing alloc init to create a new object returns a pointer. You can just pass the pointer directly to the method.

if ([delegate respondsToSelector:@selector(didFinishDownload:)]) {
[delegate performSelector:@selector(didFinishDownload:) withObject:myString];

}


I guess - (void)postDictionary:(NSDictionary *)dictionary toURL:(NSURL *)url forDelegate:(id)delegate withIdentifier:(NSString *)identifier; is the method declared by you.

U can have

- (void)postDictionary:(NSDictionary *)dictionary toURL:(NSURL *)url forDelegate:(id)delegate withIdentifier:(NSString *)identifier withObject:(id)obj;


Why can't you easily do for example

if ([delegate respondsToSelector:@selector(didFinishDownload:)]) {
    [delegate performSelector:@selector(didFinishDownload:) withObject:identifier];
}

Why you say you need to do a kind of pointer operation?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜