Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8697733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:35:36+00:00 2026-06-13T01:35:36+00:00

I saw some code like this : NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];

  • 0

I saw some code like this :

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];

if(connection) {
    receivedData = [NSMutableData data];
} else {

}

How do I make it to go into the else? I tried disabling the connection and still never go into the else it goes through the didFailWithError … so wondering if it is even necessary??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:35:37+00:00Added an answer on June 13, 2026 at 1:35 am

    The apple documentation on the return value of this method says:

    Return Value

    The URL connection for the URL request. Returns nil if a
    connection can’t be initialized.

    This could be misleading for those not familiar with Cocoa (and Objective-C) best practices.

    Generally an init method should return an object; unless there is some fundamental reason returning an object would be dangerous. Most other languages handle cases like this by throwing exceptions, requiring you to add countless try/catch brackets.

    The initWithRequest:delegate: method will return nil if (for example) you pass a nil as the request parameter; and for good reason. Why create a connection object to have no useful purpose?

    However lets consider a situation like airplane mode or an invalid url. If initWithRequest:delegate: just returned nil then how would you know what went wrong? Allowing the connection to call connection:didFailWithError: gives you an opportunity to gain insight into the particular failing, through the error parameter.

    To address your question as to whether handling the event of the connection being nil is ever necessary: It depends. If the connection fails to initialize it will never call the connection delegate methods, so if your code relies on these callbacks to issue subsequent requests then yes it’s necessary.

    A side note: Creating your NSMutableData here is a little premature since you don’t yet know that there is a valid connection. An answer I gave here gave some very basic sample code regarding where to do this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I saw some code like this: try { db.store(mydata); } finally { db.cleanup(); }
I saw some sample code like this in Javascript. Is this AJAX? (function() {
I saw some code written by another developer that looks something like this: var
Just now,I saw some code like this: if(condition){ var xx='sss'; } //do something if(condition){
I saw some C code like this: // A: typedef uint32_t in_addr_t; struct in_addr
I saw some code in TCL like this: namespace eval ::info { set count
I saw some code, html form code which looks like this: <form name=form1 method=post
I saw some code like this: while(ajaxCallInProgress) { } ajaxCallInProgress = true; What is
Recently I saw some code like this: <tr> <th> Some label: </th> <td> <input
I saw some code on the apple website that goes like this id <NSFetchedResultsSectionInfo>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.