1

我有一个不寻常的问题,我不知道为什么:

我有以下方法:

 - (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;
    }

像这样调用:

    NSString *profileSection = [NSString string];
[self copyAndReplaceOccurencesForProfileFromString:htmlBody_profile toString:profileSection];
DLog(@"PROFILE MODIFIED HTML STRING: %@", profileSection);

但“profileSection”字符串返回空。

我必须指出,在调用 return 之前,“aString”中有值。

知道为什么会这样吗?谢谢 !

4

2 回答 2

2

编辑:看看

   NSString *profileSection = [NSString string]; //reference created

再次在 profileSection 作为 aString 传入方法时:

   aString = [NSString stringWithString:initialString]; //new reference created

现在 profileSection 永远不能保存因引用不同而被操纵的字符串。


让你的函数返回一个新的 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;
}

现在像这样使用:

NSString *profileSection = [self copyAndReplaceOccurencesForProfileFromString:htmlBody_profile];
DLog(@"PROFILE MODIFIED HTML STRING: %@", profileSection);
于 2012-09-28T08:26:48.713 回答
2

我想你误解了它return的工作原理。它不操纵从调用者传递的参数的值,而是确定方法调用完成后计算的值。所以说你有一个方法-(id)someMethod,然后像这样调用它:

id myValue = [self someMethod];

在内部someMethod,有一个 return 语句 - 右边的任何东西都是将在调用方法或函数return中分配的东西。myValue

要将其应用于您的情况,请更改您的方法,例如:

 - (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;
    }

然后将您的调用代码更改为:

NSString *profileSection = [self replaceOccurencesOfProfileInString: htmlBody_profile];

现在,您已经创建了一个新NSString实例并将其返回给调用者,并在调用者处将其分配给profileSection变量。

希望这可以帮助!

于 2012-09-28T08:31:12.413 回答