访问委托方法的返回值 [英] Get access to a return value of a delegate method

查看:121
本文介绍了访问委托方法的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是写了一个委托方法,它应该返回一个数组,我已经创建了这个方法。下面是代码:

   - (NSArray *)receivedString:(NSString *)rx {
NSString * portsetting = [[NSString alloc] init];
NSMutableArray * portArray = [[NSMutableArray alloc] initWithCapacity:[portsetting
length]];
portsetting = rx;

for(int i = 0; i <[portsetting length]; i ++)
{
NSString * ichar = [NSString stringWithFormat:@%c,[portsetting
characterAtIndex:i]];
[portArray addObject:ichar];
}

return portArray;
}

此委托方法是我导入的库的一部分,由我自己调用,但由程序在我的代码中搜索该方法。到目前为止这么好....一切都很好。



但这里是事情。我想在我的代码访问这个返回的数组,但我不想调用该方法本身,因为我没有任何输入值。这一切都由程序来处理...难以解释自己。



所以在方法被调用后,我想访问这个数组,也许通过一种方法,我会写我自己。但是我到目前为止没有一个线索,怎么办......有人可以帮助我吗?



是否有必要初始化数组外部这个函数获取访问呢?我将如何实现呢?在init部分的代码?



谢谢
Sebastian

div 在类(符合协议并接收回调的类)中,声明一个NSMutableArray的实例变量和属性



当调用委托方法时,将您的portArray指定给您的类属性之前你回来。然后你的类实例有一个对portArray的引用,可以通过你的类中的任何方法访问。你不需要显式地为它分配一个数组,因为你的回调方法结束时你已经有了数组在内存中 - 将数组分配给你的类属性保留它,因此断言所有权超出了范围



您可以在代理回调(receivedString)中调用自己的方法,并且它可以访问您的数组。



在标题(myClass.h)中:

  @interface myClass< ConformsToReceivedStringProtocol> 
{
@private
NSMutableArray * portArray_;
}

@property(nonatomic,retain)NSMutableArray * portArray;

- (void)myMethodThatUsesPortArray;

在您的实现(myClass.m)

  @implementation myClass 

@synthesize portArray = portArray_;

- (NSArray *)receivedString:(NSString *)string
{

//做你已经做的一切

NSMutableArray * myPortArray;

[self setPortArray:myPortArray];

[self myMethodThatUsesPortArray];

return myPortArray;

}

- (void)myMethodThatUsesPortArray
{

//做任何你想要的操作

NSMutableArray * myArray = [self portArray];
}

//不要忘记释放属性
- (void)dealloc
{

[self setPortArray:nil ];
[portArray_ release];

[super dealloc];
}

最后一个选项只是将数组传递给自己的方法你返回,避免需要定义ivars和属性,但需要你自己管理数组的内存:

   - void)myMethodThatUsesArray:(NSMutableArray *)portArray 
{
[portArray retain];
//使用portArray进行操作,然后释放所有权
[portArray release];
}

- (NSArray *)receivedText:(NSString *)string
{
//假设我们有一个myPortArray [self myMethodThatUsesArray:myPortArray]
return myPortArray;
}


I just wrote a delegate method, that shall return an array, I've created inside this method. Here is the code for that:

- (NSArray *) receivedString:(NSString *)rx{
NSString *portsetting = [[NSString alloc] init];
NSMutableArray *portArray = [[NSMutableArray alloc] initWithCapacity:[portsetting 
length]];
portsetting = rx;

for (int i = 0; i < [portsetting length]; i++)
{
    NSString *ichar = [NSString stringWithFormat:@"%c",[portsetting 
characterAtIndex:i]];
    [portArray addObject:ichar];
}

return portArray;
}

This delegate method is part of a library I've imported, an is not called by myself, but by the program searching for that method in my code. So far so good.... everything is working fine.

but here is the thing. I would like to get access to this returned array in my code, but I don't want to call the method itself, because I don't have any input values for that. This is all handled by the "program"... difficult to explain myself.

So after the method has been called, I would like to get access to this array, maybe by a method I would have written myself. But I don't have a clue so far, how to do that...... Can someone give me a helping hand?

Is it necessary to init the array outside this function to get access to it? And how would I implement that? In the init-part of the code? is there a way to make a copy of this array in another method?

Thank you Sebastian

解决方案

In your class (the class that conforms to protocol and receives the callback), declare an instance variable and property for an NSMutableArray (or you could cast it as an array if it won't change again after you first receive it).

When your delegate method is called, assign your portArray to your class property before you return. Then your class instance has a reference to the portArray that can accessed by any method in your class. You don't need to explicitly allocate/init an array for this, because you already have the array in memory by the time your callback method is finishing - assigning the array to your class property retains the it, thus asserting ownership beyond the scope of the delegate callback.

You could call your own method from within the delegate callback (receivedString), and it would have access to your array.

In your header (myClass.h):

@interface myClass <ConformsToReceivedStringProtocol>
{
@private
    NSMutableArray *portArray_;
}

@property (nonatomic, retain) NSMutableArray *portArray;

-(void) myMethodThatUsesPortArray;

In your implementation (myClass.m)

@implementation myClass

@synthesize portArray = portArray_;

-(NSArray *) receivedString:(NSString *)string
{

//do everything you're already doing

NSMutableArray *myPortArray;

[self setPortArray:myPortArray];

[self myMethodThatUsesPortArray];

return myPortArray;

}

-(void) myMethodThatUsesPortArray 
{

//do whatever you want with

NSMutableArray *myArray = [self portArray];
}

//don't forget to release the property
-(void) dealloc 
{

[self setPortArray:nil];
[portArray_ release];

[super dealloc];
}

A final option would be just to pass the array to your own method directly before you return, avoiding the need to define ivars and properties, but requiring you to manage the memory of the array yourself:

- (void) myMethodThatUsesArray:(NSMutableArray *)portArray
{
[portArray retain];
//do something with portArray and then release ownership
[portArray release];
}

-(NSArray *) receivedText:(NSString *)string
{
//assume we have a myPortArray (balanced in memory)
[self myMethodThatUsesArray:myPortArray]
return myPortArray;
}

这篇关于访问委托方法的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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