-2

I am trying to replace instances of 's with s or alternatively instances of s with 's. However, the result of the code below is an empty string. What could I be doing wrong?

NSString *myStr = @"Eat at Joe's";
 NSString *newStr = [myStr stringByReplacingOccurrencesOfString:@"\'s" withString:@"s"];
//edited as per Vadian
NSLog(@"newStr:%@",newStr); //logs as newStr:
user1904273
  • 4,562
  • 11
  • 45
  • 96

1 Answers1

3

You forgot the placeholder

NSLog(@"newStr: %@", newStr); 

Don't you get the warning

Data argument not used by format string

vadian
  • 274,689
  • 30
  • 353
  • 361
  • 2
    Curious why this was accepted as the correct answer since the question (after fixing the typo) includes this same line of code already. So this doesn't actually fix anything. – rmaddy Nov 12 '18 at 23:28
  • The answer is correct..literally. I think the real reason my code did not work was Xcode got corrupted as after xcode crashed and I restarted mac it worked properly. You are not supposed to delete questions with answers otherwise I would have deleted this question. – user1904273 Nov 13 '18 at 19:50
  • It is also possible the error in my app had to do with apostrophes in IOS 11 and onward. For my question, I wrote the string as a ' using the keyboard but in the app it comes from user input where apple substitutes smart apostrophes. Here is a question you answered on this very topic.https://stackoverflow.com/questions/35304347/how-to-find-and-replace-a-particular-unicode-character-with-a-non-unicode-charac – user1904273 Nov 14 '18 at 00:53