1

Okay I am new to objective C and am trying hard to learn it on my own with out bother the stacked overflow community to much but it is really quite different then what I'm used to (C++).

But I have come across a issue that I for the life of me can't figure out and I'm sure it's going be something stupid. But I am pulling questions and answers from a website that then will display on my iOS application by using this code.

NSString * GetUrl = [NSString stringWithFormat:@"http://www.mywebpage.com/page.php"];
NSString * GetAllHtml = [NSString stringWithContentsOfURL:[NSURL URLWithString:GetUrl] encoding:1 error:nil];

NSString *PullWholeQuestion = [[GetAllHtml componentsSeparatedByString:@"<tr>"] objectAtIndex:1];
NSString *FinishWholeQuestion = [[PullWholeQuestion componentsSeparatedByString:@"</tr>"] objectAtIndex:0];

After I get all of the webpage information I strip down each question and want to make it where it will do a loop process to pull the questions so basically I need to count how many array options there are for the FinishedWholeQuestion variable

I found this snippet online that seemed to work with there example but I cant repeat it

NSArray *stringArray = [NSArray arrayWithObjects:@"1", @"2", nil];
NSLog(@"count = %d", [stringArray count]);
1
  • Welcome to SO, you should take the tour if you haven't already. Commented Jun 16, 2013 at 4:06

1 Answer 1

3

"componentsSeparatedByString" returns an NSArray object, not a single NSString.

An array object can contain zero, one or more NSString objects, depending on the input.

If you change "FinishWholeQuestion" into a NSArray object, you'll likely get a few components (separate by a string).

And now that I'm looking at your code a little more closely, I see you're making an assumption that your array is always valid (and has more than 2 entries, as evidenced by the "objectAtIndex: 1" bit).

You should also change the first character of all your Objective-C variables. Best practices in Objective-C are that the first character of variables should always be lower case.

Like this:

NSString * getUrl = [NSString stringWithFormat:@"http://www.mywebpage.com/page.php"];
NSString * getAllHtml = [NSString stringWithContentsOfURL:[NSURL URLWithString:getUrl] encoding: NSUTF8StringEncoding error:nil];

NSArray * allQuestions = [getAllHtml componentsSeparatedByString:@"<tr>"];
if([allQuestions count] > 1)
{
    // assuming there is at least two entries in this array
    NSString * pullWholeQuestion = [allQuestions objectAtIndex: 1];
    if(pullWholeQuestion)
    {
        NSString *finishWholeQuestion = [[pullWholeQuestion componentsSeparatedByString:@"</tr>"] objectAtIndex:0];
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much for that snippet let me crawl over it several times and see what I can do with it :)
Hmmm still having a little trouble cause the reason I was attempting to do this was to determine how many questions were stored on the site (Likely to increase over time) that way the while statement would only pull the correct number because the total amount would be a variable. Thus the use of [stringArray count]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.