如何在目标c中的方法之外保留数组

发布于 2024-11-28 09:04:35 字数 1469 浏览 0 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

故笙诉离歌 2024-12-05 09:04:35
@property(nonatomic,retain) NSMutableArray *myData 

并创建对象

self.myData = [[NSMutableArray alloc] init];

// and i assume your resultArray is a mature NSMutableArray object
[self.myData addObject:resultArray];

我能想到的使用复制的最佳方法是始终将 NSString 属性设置为“复制”而不是保留。这样,如果您搞砸了并忘记释放物体所握住的绳子,您可以从 Leaks 仪器中获得更准确的读数。文案的其他用途需要更仔细地考虑。

注意:您有责任在不使用该变量后释放 myData。

@property(nonatomic,retain) NSMutableArray *myData 

and create the object

self.myData = [[NSMutableArray alloc] init];

and

// and i assume your resultArray is a mature NSMutableArray object
[self.myData addObject:resultArray];

The best way of using copy I can think of, is to always set NSString properties to "copy" instead of retain. That way you get more accurate readings from the Leaks instrument if you mess up and forget to release a string an object is holding onto. Other uses of copy need to be more carefully thought out.

NOTE : You are responsible to release myData after no use of that variable.

醉生梦死 2024-12-05 09:04:35

当您将 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

从此见与不见 2024-12-05 09:04:35

除了 ivar 的名称不同(mienVarmyVar)之外,我没有发现任何问题。其他一些代码必须释放您的 ivar,或者您在 viewDidLoad 有机会实际创建数组之前访问它(我打赌是后者)。

我认为你应该将代码放在初始化方法中的 viewDidLoad 中。不要忘记在dealloc中释放数组。

当然,您也可以编写自己的 myData getter 方法,进行延迟初始化,而不是在 init 方法中创建它:

- (NSMutableArray *) myData
{
    if (!myData)
        myData = [[NSMutableArray alloc] init];
    return myData;
}

注意,现在,您应该访问 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 before viewDidLoad 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 in dealloc.

You could, of course, also write your own myData getter method, doing lazy initialization, instead of creating it in the init method:

- (NSMutableArray *) myData
{
    if (!myData)
        myData = [[NSMutableArray alloc] init];
    return myData;
}

Note that now, you should access self.myData if you want to use it.

太傻旳人生 2024-12-05 09:04:35

我认为 NSString yajl_JSON 类别可以返回数组或字典 - 您可能需要检查下面一行的结果数组的类型,因为它可能是 NSDictionary:

NSMutableArray *resultArray = [responseString yajl_JSON];

如果您将其视为数组,而它是可能导致问题的字典。

(下面来自NSObject+YAJL类别的相关代码)

YAJLDocument *document = [[YAJLDocument alloc] initWithData:data parserOptions:options error:error];
id root = [document.root retain];
[document release];
return [root autorelease];

(以及YAJLDocument对象中)

@interface YAJLDocument : NSObject <YAJLParserDelegate> {
(id root_; // NSArray or NSDictionary

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)

YAJLDocument *document = [[YAJLDocument alloc] initWithData:data parserOptions:options error:error];
id root = [document.root retain];
[document release];
return [root autorelease];

(and in YAJLDocument object)

@interface YAJLDocument : NSObject <YAJLParserDelegate> {
(id root_; // NSArray or NSDictionary
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文