0

So i work with UICollectionView with my JSON. I write this code for parse, but i am not understand for why it's not work. My JSON have ([jsontest2] after key "imageMain").

So i paste my code, please help me:

   @property (nonatomic,strong) NSMutableArray *patternImagesArray;

@end

@implementation ViewController

@synthesize patternImagesArray = _patternImagesArray;

NSURLConnection *connection;
NSMutableData *webdata;

Try to get data from JSON:

-(void) viewDidLoad{

    _patternImagesArray = [[NSMutableArray alloc] init];

    NSURL *url = [NSURL URLWithString:@"site"];


    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    connection = [NSURLConnection connectionWithRequest:request delegate:self];
    if (connection) {
        webdata = [[NSMutableData alloc]init];

    }

}


-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [webdata setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [webdata appendData:data];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{


}

JSON parse:

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webdata options:0 error:nil];
    NSArray *tmp =[allDataDictionary objectForKey:@"jsontest2"];
    if (tmp.count>0){

        for (NSDictionary *diction in tmp) {
            [self.patternImagesArray addObject:diction];
        }


        NSLog(@"%@", self.patternImagesArray);


    }
    [self.collectionView reloadData];
}

Some customize things:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    PatternView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PatternCell" forIndexPath:indexPath];

    NSString *myPatternString = [[self.patternImagesArray objectAtIndex:indexPath.row] valueForKey:@"thumb"];
    NSLog(@"myPatternString %@",myPatternString);
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:myPatternString]];
    cell.patternImageView.image = [UIImage imageWithData:data];


    return cell;


}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [self.patternImagesArray count];
}



-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    return CGSizeMake(100.0, 100.0);
}
Genevios
  • 1,115
  • 1
  • 16
  • 29
  • The outermost layer is a JSON "object"/NSDictionary. It contains a single element named "jsontest2". That element is a JSON "array"/NSArray containing two elements. Each element is a JSON "object"/NSDictionary containing two elements -- "imageThumb" and "imageMain". The elements are strings. – Hot Licks Mar 14 '14 at 22:52
  • Go to json.org and study the syntax -- it takes 5-10 minutes to learn. – Hot Licks Mar 14 '14 at 22:52
  • Yes i know, so i need to add into my UIImageView in my UIViewCollection... – Genevios Mar 14 '14 at 22:54
  • This is a mistake: `for (array in allDataDictionary)`. You already have `array` extracted, and you should be iterating through it's elements, not through the dictionary. – Hot Licks Mar 14 '14 at 22:55
  • I know i already deleted this and try to paste this NSURL *collectionURL = [NSURL URLWithString:[allDataDictionary objectForKey:@"imageMain"]]; // NSData *colectionImageData = [NSData dataWithContentsOfURL:collectionURL]; // UIImage *collectionImage = [UIImage imageWithData:colectionImageData]; [array addObject:collectionImage]; – Genevios Mar 14 '14 at 22:57
  • But also i have error...reason: '-[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object' – Genevios Mar 14 '14 at 22:58
  • I tried also this but also nothing NSString *path=[array valueForKey:@"imageMain"]; NSData* imageData = [NSData dataWithContentsOfURL: [NSURL URLWithString: path]]; UIImage *collectionImage = [UIImage imageWithData:imageData]; [array addObject:collectionImage]; NSLog(@"%@",array); – Genevios Mar 14 '14 at 23:05
  • Quit trying to just modify an example you found somewhere and THINK about what you're doing. Copying examples sometimes works for other things, but rarely for JSON. – Hot Licks Mar 15 '14 at 00:48
  • It's not copy examples. All this code perfectly working with cell.textLabel or detail also with image. But with collection view some problems. – Genevios Mar 15 '14 at 07:02
  • So i already changed some strings for JSON, and when i started my app i can see NSLog with my data - so it's work perfectly but how to insert to UIImageView in CollectionView. – Genevios Mar 15 '14 at 07:05
  • See Gnasher's comments. You're code pretty much sucks, and you need to think it through from the start and write it right, not just modify stuff randomly. And use properly named LOCAL variables to disassemble the JSON. – Hot Licks Mar 15 '14 at 12:42
  • Thanks for help i found my mistake so my code - -(void)connectionDidFinishLoading:(NSURLConnection *)connection { NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webdata options:0 error:nil]; NSArray *tmp =[allDataDictionary objectForKey:@"jsontest2"]; if (tmp.count>0){ for (NSDictionary *diction in tmp) { [self.patternImagesArray addObject:diction]; } NSLog(@"%@", self.patternImagesArray); } [self.collectionView reloadData]; } – Genevios Mar 15 '14 at 18:31

1 Answers1

1

You are abusing the variable "array" for two totally different purposes.

The underlying reason is that you haven't bothered to find reasonable names for variables. "array" is actually a global variable. It's not only accessible from any code within your file, but from any code anywhere in your program. Look at this bit of code:

NSDictionary *allDataDictionary =[NSJSONSerialization JSONObjectWithData:_webdata 
                                          options:kNilOptions  error:nil];

array =[allDataDictionary objectForKey:@"jsontest2"];
for (array in allDataDictionary) {
}

You first parse the JSON data to allDataDictionary (you don't bother checking for errors). You access one key "jsontest2" from the dictionary and store it into the global variable "array". Whatever was in "array" before is now gone.

Then you have a for-loop, using the same global variable as the loop variable! That's total nonsense. But you also need to realise that this will iterate over the keys in your dictionary, so array is now an NSString and not an array anymore.

After that your code gets worse...

Go through your code line by line. Every line, think about what it is doing. There are many severe problems there.

gnasher729
  • 51,477
  • 5
  • 75
  • 98