Joey Kleingers
Joey Kleingers

Reputation: 327

Objective-C: stringByReplacingOccurrencesOfStringwithString and stringByAppendingString not working

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.

Upvotes: 0

Views: 220

Answers (3)

Mario
Mario

Reputation: 4520

First, your property is a weak property, which most likely is wrong. Make it copy instead of weak. Then, instead of writing artistName to access the iVar use self.artistName to access the getter (this is best practice).

Secondly - and most importantly - assign the returned value of stringByReplacing... to your iVar through the setter (ie self.artistName again). Right now you just call the method which does nothing, since it doesnt change the string per se, it gives a new instance of a string with the changes made. in your case, the return value is never used.

So, use

self.artistName = [self.artistName stringByReplacing.....];

and

fullURL = [fullURL stringByAppending....];

But since artistName is a weak property, the string will be gone and so not be appended to your fileURL.

So make your property copy

@property (nonatomic, copy) NSString *artistName;

(I wonder why your code works in the first place because with the newest Xcode and compiler versions your property should be automatically synthesised to have an underscore-prefixed iVar (ie _artistName in your case). So writing just artistName shouldn't work, unless you synthesized it that way.)

Upvotes: 2

Kris Gellci
Kris Gellci

Reputation: 9687

I have edited your code to make it work

- (void)viewDidLoad
{
[super viewDidLoad];
NSString *fullURL = @"en.wikipedia.org/wiki/";

artistName = [artistName stringByReplacingOccurrencesOfString:@" " withString:@"_"];
artistName = [artistName stringByReplacingOccurrencesOfString:@"'" withString:@"%27"];
fullURL = [fullURL stringByAppendingString:artistName];

NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}

Remember that stringByReplacingOccurrencesOfString returns a string which you have to save somewhere, replacing the old artistName string with the new one makes sense in your case.

Upvotes: 1

mdubez
mdubez

Reputation: 3144

[artistName stringByReplacingOccurrencesOfString:@" " withString:@"_"];

actually returns a string that you need to capture, so do.

NSString *myNewString = [artistName stringByReplacingOccurrencesOfString:@"    " withString:@"_"];

Strings are immutable in many languages so methods do not actually modify the underlying string.

Upvotes: 5

Related Questions