iOS - setting text of UITextView throws EXC_BAD_AC

2019-07-24 18:48发布

问题:

I have a UITextView which I have created programmatically in the following code:

NSLog(@"creating the first blurb");
blurb = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
[blurb setText:total];
blurb.backgroundColor = [UIColor redColor];

where blurb is defined as a UITextView in the @interface.

Here's where I'm having the problem:

total is an NSString that is defined to get the text from a .txt file from a specified domain. It is mutated as:

 NSString *url = @"https://sites.google.com/site/paloaltoapps/tbnappsource-password-blackr3d/Updates.txt";
 NSURL *urlRequest = [NSURL URLWithString:url];
 total = [NSString stringWithContentsOfURL:urlRequest encoding:NSUTF8StringEncoding error:nil];

However, when I run the code, I get an instance of EXC_BAD_ACCESS thrown at the setText: line of the top code block.

I have tried printing total out using an NSLog statement, and that seems to work just fine. Can anyone see what I'm doing wrong? Thanks.

回答1:

try this... May be it lose reference......

total = [[NSString stringWithContentsOfURL:urlRequest encoding:NSUTF8StringEncoding error:nil]copy];


回答2:

Why not trying to create an NSString and assign the text to that. And then you can try to print the NSString and see if that works. Just a suggestion.