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 5964635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:28:06+00:00 2026-05-22T19:28:06+00:00

I am stuck and need some help understanding why this is not working. I

  • 0

I am stuck and need some help understanding why this is not working.

I want to be able to download the HTML of a page and then format it to show correctly, the code inside the second class (spriing) will download and display the HTML in a UITextView if it is placed inside the ViewController, however this is breaking the MVC right?

So could anyone tell me why I am getting the out of scope error on the mStringData variable?

My classes are below:

I have one class which is a view controller;

//Class for the download and processing of data from website

#import "FirstViewController.h"

@implementation FirstViewController

// The designated initializer. Override to perform setup that is required before the view is loaded.
//- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
//    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
//        // Custom initialization
//} 
//    return self;
//}


/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    spriing = [Spriing new];

    [spriing downloadData:@"http://www.spriing.co.uk/services/"];
    SpriingTxt.text = spriing.mStringData;
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
    [mRecData release];
    [mStringData release];
}

And a separate class;

#import "Spriing.h"

@implementation Spriing

@synthesize mStringData;
@synthesize mRecData;

- (void)downloadData: (NSString*) URL{

    mBaseURL = URL;
    // Create the request.
    NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:mBaseURL]
                                        cachePolicy:NSURLRequestUseProtocolCachePolicy
                                        timeoutInterval:60.0];

    // create the connection with the request
    // and start loading the data
    mCon=[[NSURLConnection alloc] initWithRequest:request delegate:self];

    if (mCon) 
    {
        // create var to store data
        mRecData = [[NSMutableData data] retain];
    } 
    else 
    {
        // Inform the user that the connection failed.
    }
}

//If the connection is reset
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    //reset the data length
    [mRecData setLength:0];
}

//Obtaining new data
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    //Add any newly recieved data to the currently stored data
    [mRecData appendData:data];
}

//If something went wrong
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    //Release the connection
    [mCon release];
    //Release the data
    [mRecData release];

    //Alert the user
    UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Error!" 
                                                     message:@"No internet connection!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
    [alert show];
    [alert release];
}

//When its done
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //NSLog(@"finished");

    // Once this method is invoked, "responseData" contains the complete result
    self.mStringData = [[[NSString alloc] initWithData:mRecData encoding:NSUTF8StringEncoding] retain];

    //NSLog(@"%@", mStringData);

    self.mStringData = [self processData:mStringData];

    //NSLog(@"%@", mStringData);

    //SpriingTxt.text = mStringData;

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    //mStringData = nil;
}

- (NSURLRequest *)connection:(NSURLConnection *)connection
             willSendRequest:(NSURLRequest *)request
            redirectResponse:(NSURLResponse *)redirectResponse
{
    [mBaseURL autorelease];
    mBaseURL = [[request URL] retain];
    return request;
}

-(NSString*) processData: (NSString*) string
{
    NSMutableString *html = [NSMutableString stringWithCapacity:[string length]];

    NSScanner *scanner = [NSScanner scannerWithString:string];

    NSString *tempText = nil;

    while (![scanner isAtEnd])
    {
        [scanner scanUpToString:@"<" intoString:&tempText];

        if (tempText != nil)
            [html appendString:tempText];

        [scanner scanUpToString:@">" intoString:NULL];

        if (![scanner isAtEnd])
            [scanner setScanLocation:[scanner scanLocation] + 1];
         tempText = nil;
    }

    return html;
}

- (void) dealloc
{
    [super dealloc];
    //[mStringData release];
}

@end
  • 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-05-22T19:28:07+00:00Added an answer on May 22, 2026 at 7:28 pm

    You are starting an asynchronous request for a URL which will take some time. Although it returns immediately, it doesn’t imply that the data has been download. NSURLRequest‘s delegate will be notified when the data has finished downloading. It is not until then that there is data in mStringData which is probably nil prior to being assigned the downloaded data. So when you do SpriingTxt.text = spriing.mStringData; immediately after an asynchronous request without the data being downloaded, SpriingTxt.text is assigned nil.

    To resolve this, you can either make a synchronous request which will block until the data has been downloaded which is generally a bad idea or you can message via delegates or notifications to your view controller when the data of your asynchronous request has been downloaded.

    To implement the delegate

    Delegates are implemented using protocols. You will create a delegate property in the delegating object which would be Spriing as it will let the delegate know when the string has been downloaded and the view controller will be its delegate as it wants to know when the data is available so that it can update its view. Delegates are usually not retained as most times it is the object that creates them that becomes its delegate. So retaining the delegate would create a retain cycle in such instances. There are lots of tutorials about creating the delegates. A rough implementation would be,

    in Spriing.h

    @protocol SpriinDelegate;
    
    @interface Spriing:... {
        id<SpriingDelegate> delegate;
        ...
    }
    
    @property (nonatomic, assign) id<SpriingDelegate> delegate;
    ...
    
    @end
    
    @protocol SpriingDelegate
        - (void)spriing:(Spriing*)aSpriing didFinishDownloadingString:(NSString*)aString;
    @end
    

    in Spriing.m

    @implementation Spriing
    
    @synthesize delegate;
    
    ...
    
    //When its done
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        ...
    
        self.mStringData = [self processData:mStringData];
    
        if ( self.delegate && [self.delegate respondsToSelector:@selector(spriing:didFinishDownloadingString:)]) {
            [self.delegate spriing:self didFinishDownloadingString:self.mStringData];
        }
    
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    }
    
    ...
    @end
    

    in the view controller,

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        spriing = [Spriing new];
        spriing.delegate = self;
    
        [spriing downloadData:@"http://www.spriing.co.uk/services/"];
    }
    
    - (void)spriing:(Spriing*)aSpriing didFinishDownloadingString:(NSString*)aString {
        SpriingText.text = aString;
    }
    
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok well I need some help because I am plain stuck. I am trying
I'm currently working on my own PHP Framework, and I need some help figuring
Guys i need some help here , im getting this error here at the
I need some help with setting up a cURL request to a page. In
i need some help. i have this code: <%= f.datetime_select :date, :start_year => 2011
I'm working with jQuery for the first time and need some help. I have
I am stuck here and need some serious help. I had to do some
I need some help with a MySQL query I'm working on. I have data
guys i really REALLY need some help on this one. 2 days i've been
I need some help with my web service and json call.. stuck trying to

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.