Seems xcode is not accepting and code from ch 23


#1

i tried the nsdata object to file and it still doesnt work nothing in chapter 23 is working for

my code isnt workign and i am copying it exactly from the texts

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
       
        NSURL *url =[NSURL URLWithString:@"http://www.google.com/image/logos/ps_logo2.png"];
        NSError *error = nil;
        NSData *data = [NSURLConnection sendSynchronousRequest:request   returningResponse:NULL error:&error];
        
        if (!data)
         {
             NSLog(@"fetch failed :%@", [error localizedDescription]);
             return 1;
         
         }
        
        NSLog(@" the file is %lu bytes", [data length]);
        BOOL written = [data writeToFile:@"/tmp/google.png" options:0 error:&error];
        
        if (!written) {
            NSLog(@"write failed : %@", [error localizedDescription]);
            return 1;
        
        }
        
        
    }
    return 0;
}

#2

Forgive me for being blunt, but your description is pretty vague.

What is the problem you are having?

  • Compilation error;
  • Runtime error;
  • No output; or;
  • Xcode is setting off a nuclear reaction.

Did your code compile? The [color=#FF0000]request[/color] variable is not defined! Check your code again.


#3

This should work:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    @autoreleasepool {
        NSURL *url =[NSURL URLWithString:@"http://www.google.com/image/logos/ps_logo2.png"];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        NSError *error = nil;
        NSData *data = [NSURLConnection sendSynchronousRequest:request   returningResponse:NULL error:&error];
        
        if (!data)
        {
            NSLog(@"fetch failed :%@", [error localizedDescription]);
            return 1;
            
        }
        
        NSLog(@" the file is %lu bytes", [data length]);
        BOOL written = [data writeToFile:@"/tmp/google.png" options:0 error:&error];
        
        if (!written) {
            NSLog(@"write failed : %@", [error localizedDescription]);
            return 1;
            
        }
        
        
    }
    return 0;
}

#4

You missed the part of the code:

NSURLRequest *request = [NSURLRequest requestWithURL:url];