问题
I have an unusual issue and I can't figure out why:
I have the following method:
- (NSString*) copyAndReplaceOccurencesForProfileFromString: (NSString*)initialString toString:(NSString*)aString
{
aString = [NSString stringWithString:initialString];
DLog(@"PROFILE INITIAL HTML STRING: %@", initialString);
aString = [aString stringByReplacingOccurrencesOfString:@"//PROFILE_IMAGE//" withString:profileImageForHTML];
aString = [aString stringByReplacingOccurrencesOfString:@"//PROFILE_NAME//" withString:profileBirthNameString];
aString = [aString stringByReplacingOccurrencesOfString:@"//PROFILE_BIRTHDATE//" withString:profileDateOfBirthString];
return aString;
}
which is called like this:
NSString *profileSection = [NSString string];
[self copyAndReplaceOccurencesForProfileFromString:htmlBody_profile toString:profileSection];
DLog(@"PROFILE MODIFIED HTML STRING: %@", profileSection);
but the "profileSection" string returns empty.
I must point out that "aString" has values in it before calling return on it.
Any idea why is this happening ? Thanks !
回答1:
EDIT : Take a look
NSString *profileSection = [NSString string]; //reference created
Again in when profileSection passed in method as aString:
aString = [NSString stringWithString:initialString]; //new reference created
Now profileSection can never hold string manipulated as reference differs.
Make your function return a new NSString.
- (NSString*) copyAndReplaceOccurencesForProfileFromString: (NSString*)initialString
{
NSString *aString = [NSString stringWithString:initialString];
DLog(@"PROFILE INITIAL HTML STRING: %@", initialString);
aString = [aString stringByReplacingOccurrencesOfString:@"//PROFILE_IMAGE//" withString:profileImageForHTML];
aString = [aString stringByReplacingOccurrencesOfString:@"//PROFILE_NAME//" withString:profileBirthNameString];
aString = [aString stringByReplacingOccurrencesOfString:@"//PROFILE_BIRTHDATE//" withString:profileDateOfBirthString];
return aString;
}
Now use like:
NSString *profileSection = [self copyAndReplaceOccurencesForProfileFromString:htmlBody_profile];
DLog(@"PROFILE MODIFIED HTML STRING: %@", profileSection);
回答2:
I think you misunderstand how return works. It does not manipulate the values of arguments passed from the caller, rather it determines the value that the method call evaluates to after it finishes. So say you had a method -(id)someMethod, and then called it like this:
id myValue = [self someMethod];
Inside someMethod, there is a return statement - whatever is to the right of the return is what will be assigned to myValue in the calling method or function.
To apply this to your situation, change your method like:
- (NSString*) replaceOccurencesOfProfileInString:(NSString *)initialString
{
NSString *aString = initialString;
DLog(@"PROFILE INITIAL HTML STRING: %@", initialString);
NSData *profileImageData = UIImagePNGRepresentation(profileImage);
NSString *profileImageDataString = [profileImageData base64EncodedStringSingleLine];
NSString *profileImageForHTML = [NSString stringWithFormat:@"<p><b><img src='data:image/png;base64,%@' width='%f' height='%f'></b></p>", profileImageDataString, profileImage.size.width, profileImage.size.height];
aString = [aString stringByReplacingOccurrencesOfString:@"//PROFILE_IMAGE//" withString:profileImageForHTML];
aString = [aString stringByReplacingOccurrencesOfString:@"//PROFILE_NAME//" withString:profileBirthNameString];
aString = [aString stringByReplacingOccurrencesOfString:@"//PROFILE_BIRTHDATE//" withString:profileDateOfBirthString];
return aString;
}
Then change your calling code to:
NSString *profileSection = [self replaceOccurencesOfProfileInString: htmlBody_profile];
Now, you have created a new NSString instance and are returning it to the caller, where it is assigned to the profileSection variable.
Hope this helps!
来源:https://stackoverflow.com/questions/12636127/nsstring-returns-empty-from-a-return-method