Ios6 在多个方法中访问变量

Ios6 在多个方法中访问变量,ios6,collectionview,Ios6,Collectionview,还是有点新,我有一些问题,我希望有人能帮助我。我正在尝试将来自服务器的JSON字符串加载到iOS6中的collectionview中 我可以使用从viewDidLoad方法调用的fetchedData方法拉入数据,这部分工作正常。在fetchedData方法中,我分解JSON数据并将其放在NSDictionaries和NSArray中,并且可以将正确的数据转储到日志以查看它 问题是当我尝试在代码的其他地方使用任何信息时,例如获取任何数组中的元素数量,以用作填充collectionview的计数器

还是有点新,我有一些问题,我希望有人能帮助我。我正在尝试将来自服务器的JSON字符串加载到iOS6中的collectionview中 我可以使用从viewDidLoad方法调用的fetchedData方法拉入数据,这部分工作正常。在fetchedData方法中,我分解JSON数据并将其放在NSDictionaries和NSArray中,并且可以将正确的数据转储到日志以查看它

问题是当我尝试在代码的其他地方使用任何信息时,例如获取任何数组中的元素数量,以用作填充collectionview的计数器

可能是我累了,但我似乎无法在这一部分动脑筋。许多主要变量的声明都在fetchedData方法中,我认为既然在那里声明了,这可能是我在其他地方看不到它们的原因,所以我将变量声明移到了接口部分,希望这能使变量成为全局变量,fetchedData方法继续正常工作,但没有别的地方

当我在单元格定义区域中插入分隔符时,我可以在调试器窗口中看到变量显示为空

我不确定您可能希望看到代码的哪些部分,所以请告诉我,我可以发布它们,但也许有人可以给出一个示例,说明如何通过多种方法访问数组和字典项

为了避免混淆,并在这一点上暴露我的代码大杂烩,这里是.m文件,或者至少是它的大部分,请不要对我一直在尝试的任何我能想到的编码风格进行猛烈的攻击,我自己也很努力地撕毁了它,但已经很晚了

#import "ICBCollectionViewController.h"
#import "ICBCollectionViewCell.h"
#import "ICBDetailViewController.h"

@interface ICBCollectionViewController () {

NSDictionary* json;
NSDictionary* title;
NSDictionary* shortDescrip;
NSDictionary* longDescrip;
NSDictionary* price;
NSDictionary* path;
NSDictionary* sKU;
NSDictionary* audiotrack;
NSDictionary* audiotracksize;



NSArray* titles;
NSArray* shortDescription;
NSArray* longDescription;
NSArray* prices;
//    NSArray* paths;
NSArray* SKUs;
NSArray* audiotracks;
NSArray* audiotracksizes;
}
@end
/*
@interface NSDictionary(JSONCategories)
+(NSDictionary*)dictionaryWithContentsOfJSONURLString:(NSString*)urlAddress;
-(NSData*)toJSON;
@end

@implementation NSDictionary(JSONCategories)
+(NSDictionary*)dictionaryWithContentsOfJSONURLString:(NSString*)urlAddress
{
NSData* data = [NSData dataWithContentsOfURL: [NSURL URLWithString: urlAddress] ];
__autoreleasing NSError* error = nil;
id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if (error != nil) return nil;
return result;
}

-(NSData*)toJSON
{
NSError* error = nil;
id result = [NSJSONSerialization dataWithJSONObject:self options:kNilOptions error:&error];
if (error != nil) return nil;
return result;
}
@end
*/
@implementation ICBCollectionViewController
@synthesize paths;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
dispatch_async(kBgQueue, ^{
        NSData* data = [NSData dataWithContentsOfURL: imobURL];
        [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});

// Do any additional setup after loading the view.
}

- (void)fetchedData:(NSData *)responseData {
NSError* error;
//parse out the json data
json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

titles = [json objectForKey:@"title"]; //2
shortDescription = [json objectForKey:@"shortD"];
longDescription =  [json objectForKey:@"longD"];
prices = [json objectForKey:@"price"];
self.paths = [json objectForKey:@"path"];
SKUs = [json objectForKey:@"SKU"];
audiotracks = [json objectForKey:@"audiotrack"];
audiotracksizes = [json objectForKey:@"audiotracksize"];

 NSLog(@"paths: %@", paths); //3
//  NSLog(@"shortDescrip: %@", shortDescription);

NSInteger t=7;
    // 1) Get the latest loan
    title = [titles objectAtIndex:t];
    shortDescrip = [shortDescription objectAtIndex:t];
    longDescrip = [longDescription objectAtIndex:t];
    price = [prices objectAtIndex:t];
    path = [paths objectAtIndex:t];
    sKU = [SKUs objectAtIndex:t];
    audiotrack = [audiotracks objectAtIndex:t];
    audiotracksize = [audiotracksizes objectAtIndex:t];

    //NSLog(title.count text);
    //NSLog(title.allValues);

    // 2) Get the data
    NSString* Title = [title objectForKey:@"title"];
    NSString* ShortDescrip = [shortDescrip objectForKey:@"shortD"];
    NSString* LongDescrip = [longDescrip objectForKey:@"longD"];
    NSNumber* Price = [price objectForKey:@"price"];
    NSString* Path = [path objectForKey:@"path"];
    NSString* SKU = [sKU objectForKey:@"SKU"];
    NSString* AudioTrack = [audiotrack objectForKey:@"audiotrack"];
    NSNumber* AudioTrackSize = [audiotracksize objectForKey:@"audiotracksize"];





    /*************************HERE THE DATA EXISTS*******************************/
    /******** Path = "XYXYXYXYXYXY" for example  ********************************/

    // 3) Set the label appropriately
    NSLog([NSString stringWithFormat:@"Here is some data: Title: %@ Path %@ SKU: %@ Price: %@ Track %@ Size %@",Title, Path, SKU, Price, LongDescrip, AudioTrackSize]);

}







- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
//DetailSegue

if ([segue.identifier isEqualToString:@"DetailSegue"]) {
    ICBCollectionViewCell *cell = (ICBCollectionViewCell *)sender;
    NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
    ICBDetailViewController *dvc = (ICBDetailViewController *)[segue destinationViewController];
    dvc.img = [UIImage imageNamed:@"MusicPlayerGraphic.png"];

}
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
NSLog(@"paths qty = %d",[paths count]);
return 20;
}

// The cell that is returned must be retrieved from a call to    -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier=@"Cell";
ICBCollectionViewCell *cell = (ICBCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

//    paths = [json objectForKey:@"path"];
NSDictionary* path = [paths objectAtIndex:indexPath.row];
NSString* Path = [path objectForKey:@"path"];
//    NSString* Path = [paths objectAtIndex:indexPath.row];
NSLog(@"%d",indexPath.row);



    /***********************HERE IT DOES NOT**************************/
    /******** Path = "" **********************************************/

NSLog(@"xxx");
NSLog(path);
NSLog(paths);
NSLog(Path);
NSLog(@"ZZZ");



Path=@"deepsleep";
NSLog(@"xxx");
NSLog(Path);
NSLog(@"ZZZ");





//    paths = [json objectForKey:@"path"];

//    NSString* Path = [path objectForKey:@"path"];

NSString *imagefile = [NSString stringWithFormat:@"https://imobilize.s3.amazonaws.com/glennharrold/data/%@/mid.png", Path];
NSLog(imagefile);
NSURL *url1=[NSURL URLWithString:imagefile];
dispatch_async(kBgQueue, ^{
    NSData *data1 = [NSData dataWithContentsOfURL:url1];
    cell.imageView.image =[[UIImage alloc]initWithData:data1];
});
return cell;
}


@end

尝试分离JSON数据并在appDelegate中对其进行排序。如果在此处声明公共变量
@property(非原子,强)NSDictionary*myDict
等,则可以通过导入appDelegate并使用以下代码来访问这些变量:

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSDictionary *newDict = appDelegate.myDict;

否则,可以将信息存储在或根视图控制器中。关键是将变量存储在一个不会被释放的类中。大多数情况下,使用viewController来实现这一目的是一个坏主意——它们有被导航的倾向,这会释放内存并去除变量。谷歌“模型视图控制器”获取更多信息。

我发现了主要问题是ViewDidLoad方法。我使用后台活动从服务器获取JSON数据,当该进程运行时,前台也在处理中,因为剩余的代码基于后台进程完成时返回的值,所以数据实际上已被删除null,因此基于该单件的所有数据也为null,看起来好像不可用。一旦我让进程在前台运行,所有变量都开始有值


感谢您的帮助

我所说的数据都在一个.m文件中,我将把代码添加到我的问题中,它包含我提到的所有方法如果都在.m文件中声明,然后它将无法访问,因为属性不是公共的。这是否包括在顶部的同一个.m文件中使用它们?我可以显示它们,但在底部它们已消失。我如何更改此设置?我从未尝试将单个.m文件用于多个实现。我建议您使用
@property
在单个.h文件中声明变量,看看这是否有帮助。