错误读取在iPhone SDK上复制NSMutableArray [英] Error Reading Copied NSMutableArray on iPhone SDK

查看:187
本文介绍了错误读取在iPhone SDK上复制NSMutableArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个方法中,我提取并解析了一个 JSON ,并将它放在一个名为jsonArray的 NSArray - (void)method1 。然后我将那个jsonArray的内容复制到一个名为被复制的JSON数组的 NSMutableArray ,以用于其他方法。问题是,copiedJsonArray每当我在其他方法 - (void)method2 在控制台中记录其内容时崩溃,但它在 - (void) method1

In one of my methods, I fetched and parsed a JSON and placed it inside an NSArray called jsonArray in -(void)method1. I then copied the contents of that jsonArray to an NSMutableArray called copiedJsonArray to be used on other methods. Problem is, copiedJsonArray crashes whenever I log its contents in the console from the other methods -(void)method2 but it logs fine in -(void)method1. Can someone help me on how to fix this?

在我的头文件中:

@interface MainViewController : UIViewController

@property (nonatomic, retain) NSMutableArray *copiedJsonArray;

在我的实现文件中:

@synthesize copiedJsonArray;

- (void)viewDidLoad
{
    [self method1];
}

- (void)method1
{
    NSString *urlString = [NSString stringWithFormat:THE_URL]; 
    NSURL *url = [NSURL URLWithString:urlString];
    NSData *data = [NSData dataWithContentsOfURL:url];
    NSString *jsonString = [[[NSString alloc] initWithData:data
                                              encoding:NSUTF8StringEncoding] autorelease];
    NSDictionary *jsonDictonary = [jsonString JSONValue];
    NSArray *jsonArray = [jsonDictonary valueForKeyPath:@"QUERY.DATA"];

    self.copiedJsonArray = [[NSMutableArray alloc] initWithArray:jsonArray copyItems:YES];

    NSLog(@"Copied JSON Array in Method 1: %@", self.copiedJsonArray);

    [self method2];
}

- (void)method2
{
    NSLog(@"Copied JSON Array in Method 2: %@", self.copiedJsonArray);
}

我也试过这样做,但它也做同样的错误:

I also tried doing this too but it does the same error:

copiedJsonArray = [jsonArray mutableCopy];

我也试过执行 NSCopy

@interface MainViewController : UIViewController <NSCopying>
{
    NSMutableArray *copiedJsonArray;
}

我这样做,我可以在我的copiedJsonArray当用户点击我的 UISegmentedControl 时,一次又一次地从JSON中获取其内容。任何帮助将不胜感激。提前感谢。

I'm doing this so that I can do a loop in my copiedJsonArray without fetching its contents from JSON again and again when the user taps on my UISegmentedControl. Any help would be appreciated. Thanks in advance.

推荐答案

如果你在method1之前调用method2,它会崩溃,因为copiedJasonArray尚未创建。你不应该在方法中创建实例变量(因为你不知道它们是否被调用)。您应该在创建viewController时执行此操作,例如在viewDidLoad中。

If you call method2 before method1 it will crash as copiedJasonArray has not been created. You should not create instance variables inside methods (as you cannot know if they have been called). You should do it when you create your viewController, in viewDidLoad for example.

使用属性

@interface
@property (retain) NSMutableArray* copiedJsonArray;
@end

@synthesize copiedJsonArray = _copiedJsonArray

(编译器会自动将其放入4.5)

or leave that line it out (the compiler will put it in automatically in 4.5)

访问 self.copiedJsonArray _copiedJSONArray

在getters,setters,inits和deallocs之外,使用 self。表单,它更安全。

access as self.copiedJsonArray or _copiedJSONArray.
Outside of getters,setters,inits and deallocs, use the self. form, it's safer.

您也可以在设置中懒洋洋地创建 _copiedJsonArray

You could also create _copiedJsonArray lazily in the setter:

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

这篇关于错误读取在iPhone SDK上复制NSMutableArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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