如何用另一个字符串变量更新一个字符串变量

发布于 2024-12-23 12:08:09 字数 4502 浏览 1 评论 0原文

我有一个字符串声明,这样

NSString *str = [[NSString alloc] initWithFormat:@"I require an average GPA of at least %.2f to achieve my Goal of %@ this semester - NTU GPA Calculator", pgagoal,(NSString *)[myPickerDelegate.myGoal objectAtIndex: [myPicker selectedRowInComponent:0]]];

我声明了一个全局变量

NSStrinng *tweetString

,并希望将 str 中的字符串复制到 tweetString。我应该如何复制它?由于两者都是指针,我尝试了:

tweetString = str;

或者

tweetString = [NSString stringWithFormat:@"%@", str];

但它不起作用。


编辑: 我的代码:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex1{
NSLog(@"buttonindex 1 clicked");

NSString *str2;
NSLog(@"tweetString before if: %@", tweetString);
if (pgagoal < 0) {
    NSString *str2 = [[NSString alloc] initWithFormat:@"Confirm, Guarantee, Chop and Stamp! I can achieve my Goal of %@ this semester - NTU GPA Calculator", (NSString *)[myPickerDelegate.myGoal objectAtIndex: [myPicker selectedRowInComponent:0]]];
    NSLog(@"tweetString: < 0 %@", str2);
}
else if (pgagoal > 5){
    NSString *str2 = [[NSString alloc] initWithFormat:@"Its impossible!, i need an average GPA of at least %.2f to achieve %@ this semester - NTU GPA Calculator", pgagoal,(NSString *)[myPickerDelegate.myGoal objectAtIndex: [myPicker selectedRowInComponent:0]]]; 
    NSLog(@"tweetString: >5 %@", str2);
}

else{
    NSString *str2 = [[NSString alloc] initWithFormat:@"I require an average GPA of at least %.2f to achieve my Goal of %@ this semester - NTU GPA Calculator", pgagoal,(NSString *)[myPickerDelegate.myGoal objectAtIndex: [myPicker selectedRowInComponent:0]]];
    NSLog(@"tweetString with else: %@", str2);
}

//did i update tweetString correctly?
tweetString = [NSString stringWithString:str2]; <-- stop working from this point EXC_BAD_ACCESS

NSLog(@"tweetString after if else: %@", tweetString);
[self sendEasyTweet:tweetString];
NSLog(@"tweetString: %@", tweetString);
[str2 release];
}

- (void)sendEasyTweet {    
// Set up the built-in twitter composition view controller.
TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];


// Set the initial tweet text. See the framework for additional properties that can be set.
[tweetViewController setInitialText:tweetString];

// Create the completion handler block.
[tweetViewController setCompletionHandler:^(TWTweetComposeViewControllerResult result) {        
    switch (result) {
        case TWTweetComposeViewControllerResultCancelled:
            // The cancel button was tapped.
            NSLog(@"Tweet cancelled");
            break;
        case TWTweetComposeViewControllerResultDone:
            // The tweet was sent.
            NSLog(@"Tweet done");
            break;
        default:
            break;
    }

    // Dismiss the tweet composition view controller.
    [self dismissModalViewControllerAnimated:YES];
}];

// Present the tweet composition view controller modally.
[self presentModalViewController:tweetViewController animated:YES];
}

编辑2: 调试器输出:

2011-12-29 09:54:22.963 GPA[487:707] buttonindex 1 clicked
2011-12-29 09:54:22.966 GPA[487:707] tweetString before if: NTU GPA Calculator <-- i init the string at viewDidLoad
2011-12-29 09:54:22.968 GPA[487:707] tweetString with else: I require an average GPA of at least 1.56 to achieve my Goal of Third Class Honors this semester - NTU GPA Calculator
(gdb)

EDIT3: 我的 tweetString 在 vi​​ew controller.h 中声明为

    @interface GPAMainViewController : UIViewController <GPAFlipsideViewControllerDelegate>{
UIPickerView * myPicker;
GPAAppDelegate * myPickerDelegate;
IBOutlet UITextField *txtGPA;
IBOutlet UITextField *txtTotalAU;
IBOutlet UITextField *txtRemainingAU;
double pgagoal;
NSString *tweetString;
}

@property (nonatomic, retain) IBOutlet UIPickerView * myPicker;
@property (nonatomic, retain) IBOutlet GPAAppDelegate *myPickerDelegate;
@property (nonatomic, retain) UITextField *txtGPA;
@property (nonatomic, retain) UITextField *txtTotalAU;
@property (nonatomic, retain) UITextField *txtRemainingAU;
@property (nonatomic, retain) NSString *tweetString;

-(IBAction)finishEditing:(id)sender;
-(IBAction)calculateGoal: (id) sender;
-(IBAction)showInfo:(id)sender;
-(IBAction)nextField:(id)sender;
-(IBAction)resetField:(id)sender;
-(void)sendEasyTweet:(id)sender;

i have a string declare as such

NSString *str = [[NSString alloc] initWithFormat:@"I require an average GPA of at least %.2f to achieve my Goal of %@ this semester - NTU GPA Calculator", pgagoal,(NSString *)[myPickerDelegate.myGoal objectAtIndex: [myPicker selectedRowInComponent:0]]];

i declared a global variable

NSStrinng *tweetString

and wants to copy the the string in str to tweetString. how should i copy it? since both are pointers, i tried:

tweetString = str;

or

tweetString = [NSString stringWithFormat:@"%@", str];

but it doest work.


EDIT:
my code:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex1{
NSLog(@"buttonindex 1 clicked");

NSString *str2;
NSLog(@"tweetString before if: %@", tweetString);
if (pgagoal < 0) {
    NSString *str2 = [[NSString alloc] initWithFormat:@"Confirm, Guarantee, Chop and Stamp! I can achieve my Goal of %@ this semester - NTU GPA Calculator", (NSString *)[myPickerDelegate.myGoal objectAtIndex: [myPicker selectedRowInComponent:0]]];
    NSLog(@"tweetString: < 0 %@", str2);
}
else if (pgagoal > 5){
    NSString *str2 = [[NSString alloc] initWithFormat:@"Its impossible!, i need an average GPA of at least %.2f to achieve %@ this semester - NTU GPA Calculator", pgagoal,(NSString *)[myPickerDelegate.myGoal objectAtIndex: [myPicker selectedRowInComponent:0]]]; 
    NSLog(@"tweetString: >5 %@", str2);
}

else{
    NSString *str2 = [[NSString alloc] initWithFormat:@"I require an average GPA of at least %.2f to achieve my Goal of %@ this semester - NTU GPA Calculator", pgagoal,(NSString *)[myPickerDelegate.myGoal objectAtIndex: [myPicker selectedRowInComponent:0]]];
    NSLog(@"tweetString with else: %@", str2);
}

//did i update tweetString correctly?
tweetString = [NSString stringWithString:str2]; <-- stop working from this point EXC_BAD_ACCESS

NSLog(@"tweetString after if else: %@", tweetString);
[self sendEasyTweet:tweetString];
NSLog(@"tweetString: %@", tweetString);
[str2 release];
}

- (void)sendEasyTweet {    
// Set up the built-in twitter composition view controller.
TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];


// Set the initial tweet text. See the framework for additional properties that can be set.
[tweetViewController setInitialText:tweetString];

// Create the completion handler block.
[tweetViewController setCompletionHandler:^(TWTweetComposeViewControllerResult result) {        
    switch (result) {
        case TWTweetComposeViewControllerResultCancelled:
            // The cancel button was tapped.
            NSLog(@"Tweet cancelled");
            break;
        case TWTweetComposeViewControllerResultDone:
            // The tweet was sent.
            NSLog(@"Tweet done");
            break;
        default:
            break;
    }

    // Dismiss the tweet composition view controller.
    [self dismissModalViewControllerAnimated:YES];
}];

// Present the tweet composition view controller modally.
[self presentModalViewController:tweetViewController animated:YES];
}

EDIT2:
Debbuger output:

2011-12-29 09:54:22.963 GPA[487:707] buttonindex 1 clicked
2011-12-29 09:54:22.966 GPA[487:707] tweetString before if: NTU GPA Calculator <-- i init the string at viewDidLoad
2011-12-29 09:54:22.968 GPA[487:707] tweetString with else: I require an average GPA of at least 1.56 to achieve my Goal of Third Class Honors this semester - NTU GPA Calculator
(gdb)

EDIT3:
my tweetString is declared in view controller.h as

    @interface GPAMainViewController : UIViewController <GPAFlipsideViewControllerDelegate>{
UIPickerView * myPicker;
GPAAppDelegate * myPickerDelegate;
IBOutlet UITextField *txtGPA;
IBOutlet UITextField *txtTotalAU;
IBOutlet UITextField *txtRemainingAU;
double pgagoal;
NSString *tweetString;
}

@property (nonatomic, retain) IBOutlet UIPickerView * myPicker;
@property (nonatomic, retain) IBOutlet GPAAppDelegate *myPickerDelegate;
@property (nonatomic, retain) UITextField *txtGPA;
@property (nonatomic, retain) UITextField *txtTotalAU;
@property (nonatomic, retain) UITextField *txtRemainingAU;
@property (nonatomic, retain) NSString *tweetString;

-(IBAction)finishEditing:(id)sender;
-(IBAction)calculateGoal: (id) sender;
-(IBAction)showInfo:(id)sender;
-(IBAction)nextField:(id)sender;
-(IBAction)resetField:(id)sender;
-(void)sendEasyTweet:(id)sender;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半透明的墙 2024-12-30 12:08:09

它不起作用的原因(它可能因 EXC_BAD_ACCESS 崩溃)是因为变量 str 的范围仅在声明它的块内,即 if/else 语句的 else 部分的块内。尝试这样的事情:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex1 {
    NSString* str; //declare string here so it is in scope the entire method
    .
    . //your code
    .
    .

    if(yourConditionHere) {
        //make sure you initialize str here as well so if the else part of the statement
        // isn't executed, you aren't trying to access an uninitialized variable
    } else {
        str = [[NSString alloc] initWithFormat:@"I require an average GPA of at
            least %.2f to achieve my Goal of %@ this semester - NTU GPA Calculator", 
            pgagoal,(NSString *)[myPickerDelegate.myGoal objectAtIndex: [myPicker 
            selectedRowInComponent:0]]]; //give str a value

        NSLog(@"tweetString with else: %@", str);
    } //Variable str is going out of scope here the way you have your code set up now

    tweetString = [str copy];

    NSLog(@"tweetString after if else: %@", tweetString);
    [self sendEasyTweet:tweetString];
    NSLog(@"tweetString: %@", tweetString);
    [str release];
}

The reason that it doesn't work (it is probably crashing with an EXC_BAD_ACCESS) is because the scope of the variable str is only within the block in which it is declared, the block of the else part of your if/else statement. Try something like this:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex1 {
    NSString* str; //declare string here so it is in scope the entire method
    .
    . //your code
    .
    .

    if(yourConditionHere) {
        //make sure you initialize str here as well so if the else part of the statement
        // isn't executed, you aren't trying to access an uninitialized variable
    } else {
        str = [[NSString alloc] initWithFormat:@"I require an average GPA of at
            least %.2f to achieve my Goal of %@ this semester - NTU GPA Calculator", 
            pgagoal,(NSString *)[myPickerDelegate.myGoal objectAtIndex: [myPicker 
            selectedRowInComponent:0]]]; //give str a value

        NSLog(@"tweetString with else: %@", str);
    } //Variable str is going out of scope here the way you have your code set up now

    tweetString = [str copy];

    NSLog(@"tweetString after if else: %@", tweetString);
    [self sendEasyTweet:tweetString];
    NSLog(@"tweetString: %@", tweetString);
    [str release];
}
记忆之渊 2024-12-30 12:08:09

如果要复制该字符串,或者在分配该字符串后使用该字符串,则需要复制它或保留它。

NSString *someString = @"This is a string";
NSString *copiedString = [NSString stringWithFormat:@"%@", someString"];

几秒钟之内,两个字符串都将为零或其他一些非值。您必须做的是:

NSString *someString = @"This is a string";
NSString *copiedString = [NSString stringWithFormat:@"%@", someString"] retain];

通过这样做,只要两个变量可行,您就将它们保留在内存中。但在我看来,更好的方法,特别是在处理字符串时,是使用复制,如下所示:

NSString *someString = @"This is a string";
NSString *copiedString = [NSString stringWithFormat:@"%@", someString"] copy];

这将使 someString 在几秒钟或时钟滴答声中消失,但 CopyedString 将继续存在,直到函数完成或类发布。

我怀疑您没有在 tweetString 中获取字符串值,因为当您想使用它时,这两个变量都已从内存中消失。

如果您需要保留某个变量,则必须复制或保留它。

If you want to copy the string, or use the string after you assign it, you either need to copy it or retain it.

NSString *someString = @"This is a string";
NSString *copiedString = [NSString stringWithFormat:@"%@", someString"];

Within a few seconds, both strings will be nil or some other non value. What you must do is :

NSString *someString = @"This is a string";
NSString *copiedString = [NSString stringWithFormat:@"%@", someString"] retain];

By doing this, you will keep both variables in memory as long as they are viable. But in my opinion a better way, especially when dealing with strings is to use copy, like this :

NSString *someString = @"This is a string";
NSString *copiedString = [NSString stringWithFormat:@"%@", someString"] copy];

This will make someString just go away in a few seconds or clock ticks, but copiedString will live on until the function is finished or the class released.

I suspect that you are not getting the string value inside tweetString because both variables have gone from memory when you want to use it.

If you need a variable to stay around, you must copy or retain it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文