Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If I have understood correctly what you are trying to achieve is the following:</p> <ul> <li>Create a <code>NSURLConnection</code> and configure it to fetch a JSON file.</li> <li>When the connection completes, check to see if the received file is satisfactory.</li> <li>If the file is not satisfactory then alert the user.</li> </ul> <p>If my understanding is correct then you do not need to extend the <code>NSURLConnectionDelegate</code> protocol. All you do need to do is implement the NSURLConnectionDelegate. The delegate pattern allows the behaviour of a class to be changed. (In other languages/frameworks the behaviour you explained would be implemented by subclassing. The URL Connection class would be subclassed and the methods and overridden to change the behaviour.) It may be worth your while reading <a href="http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CocoaFundamentals/CocoaDesignPatterns/CocoaDesignPatterns.html" rel="nofollow">Cocoa Design Patterns</a>.</p> <p>It's common for the class that created an object to be its delegate. The following code shows a the creation of a connection and accompanying delegate method implementations.</p> <pre><code>@interface SOViewController : UIViewController &lt;NSURLConnectionDelegate&gt; //this simply tells the compiler that SOViewController implements the NSURLConnectionDelegate protocol. If you excluded you will get a compiler warning but the code will behave correctly. You should include it. //... @property(readwrite, nonatomic, retain) NSURLConnection *connection; @property(readwrite, nonatomic, retain) NSMutableData *data; @end @implementation SOViewController //... -(void)setupJSONFetch:(NSURL *)url { NSURLRequest *request = [NSURLRequest requestWithURL: url]; //create a request NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self]; //create a connection and set self as the delegate self.connection = connection; //keep a reference to the connection self.data = [NSMutableData data]; //create an object to store the downloaded data [connection start]; //go! } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { //store the downloaded data [self.data appendData: data]; } - (void)connectionDidFinishDownloading:(NSURLConnection *)connection destinationURL:(NSURL *)destinationURL { //Check self.data is as expected } //... @end </code></pre> <p>It's also worth noting that the <code>NSURLConnection</code> delegate methods got reorganised in iOS 5.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload