如何在目标c中的方法之外保留数组
在 ASIFormDataRequest 之后,我从 JSON 创建一个临时 NSMutableArray *resultArray 然后将其添加到定义的 NSMutablearray *myData
-(void)viewDidLoad{
myData = [[NSMutableArray alloc] init];
//request that calls gotInfo method
}
-(void)gotInfo:(ASIFormDataRequest *)request{
NSString *responseString = [request responseString];
NSMutableArray *resultArray = [responseString yajl_JSON];
[myData addObject:resultArray];
}
-(IBAction)doSomethingWithData:(id)sender{
//something with myData
}
但是当我尝试从 gotInfo: 方法外部调用 myData 时,我收到错误的访问错误,并且当我检查 myData 之外时该方法,它显示 kern_protection_failure。所以我猜测在方法之外,resultArray 显然已被释放,但它也从 myData 中释放,因为 myData 内的对象共享相同的内存位置? 我也尝试过
-(void)gotInfo:(ASIFormDataRequest *)request{
NSString *responseString = [request responseString];
[myData addObject:[responseString yajl_JSON]];
}
如何保留我的数据?
在我的头文件中:
#import <UIKit/UIKit.h>
@class ASIFormDataRequest;
@interface EventsTableController : UITableViewController <UITableViewDataSource>{
NSMutableArray *myData;
}
-(void)gotInfo:(ASIFormDataRequest *)request;
更新:
所以在gbd中,myData被分配为0x5e96560,所以我这样做了
po 0x5e96560
,然后我得到了EXC_BAD_ACCESS,原因是地址为KERN_PROTECTION_FAILURE:0x00000009,
但如果我
po [[0x5e96560 objectAtIndex:0] objectForKey:@"key"]
这样做了,我就会得到价值!为什么yyyyy?
After a ASIFormDataRequest , i create a temporary NSMutableArray *resultArray from the JSON then add it to a defined NSMutablearray *myData
-(void)viewDidLoad{
myData = [[NSMutableArray alloc] init];
//request that calls gotInfo method
}
-(void)gotInfo:(ASIFormDataRequest *)request{
NSString *responseString = [request responseString];
NSMutableArray *resultArray = [responseString yajl_JSON];
[myData addObject:resultArray];
}
-(IBAction)doSomethingWithData:(id)sender{
//something with myData
}
but when i try to call myData from outside of the gotInfo: method, i get bad access errors and when i inspect myData outside of the method, it shows a kern_protection_failure. So i'm guessing that outside of the method, the resultArray is obviously released, but it's also released from myData since the object inside myData is sharing the same memory location?
I also tried
-(void)gotInfo:(ASIFormDataRequest *)request{
NSString *responseString = [request responseString];
[myData addObject:[responseString yajl_JSON]];
}
How do I preserve myData??
in my header file:
#import <UIKit/UIKit.h>
@class ASIFormDataRequest;
@interface EventsTableController : UITableViewController <UITableViewDataSource>{
NSMutableArray *myData;
}
-(void)gotInfo:(ASIFormDataRequest *)request;
UPDATE:
so in the gbd, the myData is allocated as 0x5e96560 so i did
po 0x5e96560
and then i get the EXC_BAD_ACCESS with the reason being KERN_PROTECTION_FAILURE at address: 0x00000009
but if i do
po [[0x5e96560 objectAtIndex:0] objectForKey:@"key"]
then i get the value! whyyyyyy?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
并创建对象
和
注意:您有责任在不使用该变量后释放 myData。
and create the object
and
NOTE : You are responsible to release myData after no use of that variable.
当您将 myData 声明为 EventsTableController 内部的成员时,您实际上没有任何方法可以正确访问它,但是您没有为其设置 @property,也没有合成它。通过在您的 EventsTableController.m 文件中综合它,您告诉 xcode 生成正确触摸 myData 所需的 getter/setter,这就是您的程序似乎失败的地方。如果您这样做,这应该可以解决您的问题。
-卡罗利
You dont really have any way to correctly access myData as you declare it as a member inside of EventsTableController, but you dont set the @property for it, and do not synthesize it either. By synthesizing it in your EventsTableController.m file you are telling xcode to generate the getter/setters you need to correctly touch myData, which is where your program seems to be failing. If you do this, this should solve your problem.
-Karoly
除了 ivar 的名称不同(
mienVar
与myVar
)之外,我没有发现任何问题。其他一些代码必须释放您的 ivar,或者您在 viewDidLoad 有机会实际创建数组之前访问它(我打赌是后者)。我认为你应该将代码放在初始化方法中的
viewDidLoad
中。不要忘记在dealloc中释放数组。当然,您也可以编写自己的
myData
getter 方法,进行延迟初始化,而不是在 init 方法中创建它:注意,现在,您应该访问
self.myData
如果你想使用它。Except for the different name of your ivar (
mienVar
vs.myVar
), I don't see a problem. Some other code must be releasing your ivar, or you are accessing it beforeviewDidLoad
has the opportunity to actually create the array (I bet it is the latter).I think you should put the code in
viewDidLoad
in your initialization method instead. Don't forget to release the array indealloc
.You could, of course, also write your own
myData
getter method, doing lazy initialization, instead of creating it in the init method:Note that now, you should access
self.myData
if you want to use it.我认为 NSString yajl_JSON 类别可以返回数组或字典 - 您可能需要检查下面一行的结果数组的类型,因为它可能是 NSDictionary:
NSMutableArray *resultArray = [responseString yajl_JSON];
如果您将其视为数组,而它是可能导致问题的字典。
(下面来自NSObject+YAJL类别的相关代码)
(以及YAJLDocument对象中)
I think the NSString yajl_JSON category can return an array or a dictionary - you might need to inspect the type of the result array on the line below as it may be an NSDictionary:
NSMutableArray *resultArray = [responseString yajl_JSON];
IF you are treating it as an array when its a dictionary that might be causing your problems.
(relevant code from the NSObject+YAJL category below)
(and in YAJLDocument object)