以多种方法访问变量

时间:2013-04-03 07:56:22

标签: ios6 collectionview

还有点新意,我有一些问题我希望有人可以提供帮助。我正在尝试将来自我的服务器的JSON字符串加载到iOS6中的集合视图中 我可以使用从viewDidLoad方法调用的fetchedData方法提取数据,该部分工作正常。在fetchedData方法中,我分解JSON数据并将其放在NSDictionaries和NSArrays中,并可以将正确的数据转储到日志中以查看它。

问题在于我尝试使用代码中其他地方的任何信息,例如获取任何hte数组中的元素数量,以用作计数器来填充集合视图。

可能是我累了,但我似乎无法理解这一部分。许多主要变量的声明都在fetchedData方法中,我认为因为在那里声明它可能是我无法在其他地方看到它们的原因所以我将变量的声明移动到接口部分并且希望这会使变量GLOBAL和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

2 个答案:

答案 0 :(得分:0)

尝试分解JSON数据并在appDelegate中对其进行排序。如果您在那里声明公共变量@property (nonatomic, strong) NSDictionary *myDict等,那么您可以通过导入appDelegate并使用以下代码来访问这些变量:

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

否则,您可以将信息存储在singleton或根视图控制器中。关键是将变量存储在一个不能被解除分配的类中。大多数情况下,为此目的使用viewController是一个坏主意 - 它们倾向于被导航离开,从而释放内存并摆脱变量。谷歌"模型 - 视图 - 控制器"了解更多信息。

答案 1 :(得分:0)

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

感谢您对此的帮助