I have an NSString that I am trying to use to load a webpage on a Web View in XCode. However, I need to replace spaces in the NSString with underscores, and I also need to replace all apostrophes with “%27”. Then, I need to append onto the end of the fullURL NSString. Whenever it executes the part of code that makes the NSString replacements, nothing gets replaced. It stays the same as the original NSString. When I try to append, it does not append.
The .h file:
@interface WikipediaViewController : UIViewController
@property (weak, nonatomic) NSString* artistName;
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@end
The method with the NSString replacements and append in it:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *fullURL = @"en.wikipedia.org/wiki/";
[artistName stringByReplacingOccurrencesOfString:@" " withString:@"_"];
[artistName stringByReplacingOccurrencesOfString:@"'" withString:@"%27"];
[fullURL stringByAppendingString:artistName];
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
Oh, also, the artistName variable is being initialized from a different class, so just assume that it already contains information.
Can anyone tell me why this is not working? Thanks.
actually returns a string that you need to capture, so do.
Strings are immutable in many languages so methods do not actually modify the underlying string.