0

I am getting a weird error on XCODE: expected '[' error and my build fails. This is the code

[descWeb loadHTMLString:[NSString stringWithFormat:@"<html><body p style='color:black' text="#000000" face="HelveticaNeue" size="5">%</body></html>",strtemp] baseURL: nil];

This is the whole code block

[descWeb removeFromSuperview];
descWeb = [[UIWebView alloc]init];
descWeb.delegate = self;
descWeb.tag = 1;
[descWeb setOpaque:NO];
[descWeb setBackgroundColor:[UIColor clearColor]];
descWeb.frame = CGRectMake(3, 7, 314, 350);
[descWeb loadHTMLString:[NSString stringWithFormat:@"<html><body p style='color:black' text="#000000" face="HelveticaNeue" size="5">%</body></html>",strtemp] baseURL: nil];
[scrollView addSubview:descWeb];

Thank you for looking and your help

2
  • Your format string doesn't have an %@ placeholder for the strtemp you're trying to put in it. I can't remember off hand if this will cause the error you're seeing or not. Commented Feb 6, 2014 at 1:35
  • @nhgrif You would get a completely different message if the format specifiers didn't match the arguments. This is a simple quoting issue. But you are correct that the @ is missing after the %. Commented Feb 6, 2014 at 1:51

3 Answers 3

1

You need to add slashes to the quotation marks.

NSString *string = @"A quotation mark: \"";
NSLog(@"%@", string );

Output:

A quotation mark: "

Sign up to request clarification or add additional context in comments.

Comments

1

You're mixing single and double quotes, so the stringWithFormat argument currently ends with text=", and Xcode can't balance the ['s and ]'s after this line:

[descWeb loadHTMLString:[NSString stringWithFormat:@"<html><body p style='color:black' text="#000000" face="HelveticaNeue" size="5">%</body></html>",strtemp] baseURL: nil];

Try using single quotes ' instead of double " inside your HTML.

2 Comments

It's perfectly fine to use double quotes. They just need to be escaped properly.
That's true. I offered a simple fix and an explanation for the error observed. Using double quotes that are escaped is indeed perfectly fine. Life is full of choices.
0

What the others have said, about escaping the double quotes (and missing an @) is correct.

Another option though, that may keep your code cleaner and is less tedious than escaping the double quotes, is to keep all of your HTML in .html files and load the contents of those files instead.

For example, create a file in your project called desc.html with the HTML you need in there. Then change your code to:

__autoreleasing NSError* error = nil;
NSStringEncoding encoding = 0;
NSString* htmlString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"desc" ofType:@"html"] usedEncoding:&encoding error:&error];

if (!htmlString)
{
    NSLog(@"Error: %@", error);
    return;
}

[descWeb loadHTMLString:[NSString stringWithFormat:htmlString, strtemp] baseURL:nil];

Besides keeping your project cleaner, this also makes it easier for you or someone else (designer etc) to update the HTML without changing your code.

Comments

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.