Making GET and POST Requests from an iPhone Application - Clarification Needed

后端 未结 3 1392
半阙折子戏
半阙折子戏 2020-12-29 16:13

I\'m following along with this useful looking answer to my question.

It seems to be working, but here are two questions I have:

  1. How do I detect an HTTP
3条回答
  •  一整个雨季
    2020-12-29 17:09

    Yes, you need to release that object. See the Memory Management Programming Guide for Cocoa. Basically, if you ever create an object with a method name that begins with alloc or new or contains copy, you become an owner of the object and are responsible for freeing it later. Only in the case where you know you're going to need the object up until program termination is it ok not to free it, in which case the operating system reclaims the memory when your app terminates.

    If you only need the object within a small scope, you can send it the autorelease message. This will add it to the autorelease pool. The autorelease pool periodically sends a release message to each object in it. It's kind of complicated; see the section on autorelease pools. For example:

    In this case, though, because NSURLConnections are asynchronous, autoreleasing won't work. You don't know exactly when it's going to call back into your object with data, so you want to make sure the object hasn't been released yet. The only way to be sure is to know exactly when you're done with the object, and then send it a release message yourself.

    All of the various init* functions return a pointer to the given object, so you can just do:

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    ...
    // when done with connection:
    [connection release];
    

提交回复
热议问题