Search code examples
objective-cnsstringnslog

Got an error when try to print stringByReplacingOccurrencesOfString method


When i try to print this string i got bad access error:

NSString *myPath = [myPath stringByReplacingOccurrencesOfString:@"/Users/Me/Library/iPhone/4.2/MyApp/Documents/Photos/pic1.png" 
                                                         withString:@"/Users/John/Library/iPhone/5/MyApp/Documents/Photos/picture.png"];

    NSLog(@"%@", myPath);

Why? Thanks.


Solution

  • The error is because you are calling the method stringByReplacingOccurrencesOfString:withString on a variable (myPath) that has not been instantiated. You need to call that method on an instance of the NSString class that already contains the string you are replacing text in.