以多种方式访问​​变量

问题描述:

还是有点新,我遇到了一些问题,希望有人能帮忙。我试图从我的服务器加载一个JSON字符串到iOS6的collectionview中 我可以使用从viewDidLoad方法调用的fetchedData方法来引入数据,该部分工作正常。在fetchedData方法中,我分解出JSON数据并将它放在NSDictionaries和NSArrays中,并将正确的数据转储到日志中以查看它。以多种方式访问​​变量

问题是,当我尝试使用我的代码中的任何其他信息的任何信息,如获取任何hte数组中的元素数量作为计数器来填充collectionview。

这可能是我累了,但我似乎无法绕过这部分。许多主要变量的声明在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 *[email protected]"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"); 



[email protected]"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 (nonatomic, strong) NSDictionary *myDict等,那么你可以通过输入你的appDelegate并使用下面的代码访问这些变量:

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

否则,您可以将信息存储在一个singleton,或根视图控制器。关键是将你的变量存储在一个不会被释放的类中。大多数情况下,为此目的使用viewController是一个糟糕的主意 - 它们有一种被远离的趋势,这会释放内存并摆脱变量。谷歌“模型视图控制器”的更多信息。

+0

我说的是在一个单一的.m文件,我将这些代码添加到我的问题,它包含了所有的方法中的数据你的帮助我提到 –

+0

如果这一切都在宣告。 m文件,那么它将不可访问,因为这些属性不是公共的。 – AMayes

+0

这是否包括在顶部的相同.m文件中使用它们,我可以向他们展示它们,但是在底部它们已经不存在了,我该如何更改它? –

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

感谢这个