将对象的实例传递给方法

时间:2011-07-20 09:55:13

标签: iphone objective-c object methods arguments

我有这个方法:

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

我想将一个对象的实例传递给这个方法,然后调用类似的东西:

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

我想我必须将对象的指针传递给方法?

我想我必须使用 * ,* * 和&amp ;.但我不知道我在哪里使用这些。

你能帮帮我吗?

侧击

3 个答案:

答案 0 :(得分:0)

我猜- (void)postDictionary:(NSDictionary *)dictionary toURL:(NSURL *)url forDelegate:(id)delegate withIdentifier:(NSString *)identifier;是您声明的方法。

你可以拥有

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

答案 1 :(得分:0)

为什么你不能轻易做到例如

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

为什么你说你需要做一种指针操作?

答案 2 :(得分:0)

你会发现Objective C中的所有对象都被指针引用。因此,当您声明NSString时,您可以执行以下操作:

NSString *myString = @"testString";

以同样的方式执行alloc init来创建一个新对象会返回一个指针。您可以直接将指针传递给方法。

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

}

相关问题