从另一个类访问NSMutableArray

时间:2014-05-23 13:28:26

标签: ios objective-c nsmutablearray

我正在尝试从其他类访问NSMutableArray,但在使用以下代码时,调用代码时没有值。

TableViewController.h

#import <UIKit/UIKit.h>

@interface TableViewController : UITableViewController

@property (nonatomic, strong) NSMutableArray *imageURLs;

@end

TableViewController.m

#import "TableViewController.h"
#import "ImageCell.h"
#import "ParseJSON.h"
#import "AsyncImageView.h"

@implementation TableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    ParseJSON *json = [[ParseJSON alloc] init];
    [json Parse];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    ImageCell *cell = [[ImageCell alloc] init];

    cell.firstImage.imageURL = self.imageURLs[0];
    cell.secondImage.image = self.imageURLs[1];
    cell.thirdImage.image = self.imageURLs[2];

    return cell;
}
@end

ParseJSON.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "TableViewController.h"

@interface ParseJSON : NSObject

- (void)Parse;

@end

ParseJSON.m

#import "ParseJSON.h"

@implementation ParseJSON

- (void)Parse
{
    NSData *imageData = [[NSData alloc] initWithContentsOfURL:
                              [NSURL URLWithString:@"http://www.delta.rhystowey.com/json/main.json"]];
    NSError *error;
    NSDictionary *imageDictionary = [NSJSONSerialization
                                JSONObjectWithData:imageData
                                options:kNilOptions
                                error:&error];
    NSMutableArray *URLs = [NSMutableArray array];

    if( error )
    {
        NSLog(@"%@", [error localizedDescription]);
    }
    else {
        NSArray *images = imageDictionary[@"Images"];
        for (NSDictionary *image in images)
        {
            [URLs addObject:image[@"url"]];
        }
    }
    TableViewController *tvc = [[TableViewController alloc] init];
    tvc.imageURLs = URLs;    
}
@end

1 个答案:

答案 0 :(得分:2)

您正在创建视图控制器并设置其数组。之后,它会被释放。基本上这两行都是错误的:

TableViewController *tvc = [[TableViewController alloc] init];
tvc.imageURLs = URLs; 

更改您的parse方法以返回数组。

- (NSMutableArray *)parse
{
    NSData *imageData = [[NSData alloc] initWithContentsOfURL:
                              [NSURL URLWithString:@"http://www.delta.rhystowey.com/json/main.json"]];
    NSError *error;
    NSDictionary *imageDictionary = [NSJSONSerialization
                                JSONObjectWithData:imageData
                                options:kNilOptions
                                error:&error];
    NSMutableArray *URLs = [NSMutableArray array];

    if( error )
    {
        NSLog(@"%@", [error localizedDescription]);
    }
    else {
        NSArray *images = imageDictionary[@"Images"];
        for (NSDictionary *image in images)
        {
            [URLs addObject:image[@"url"]];
        }
    }

    return URLs;    
}

@end

然后在viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];
    ParseJSON *json = [[ParseJSON alloc] init];
    _imageURLs = [json Parse];
}