2011-12-29 216 views
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]; 
} 

EDIT2: Debbuger輸出:

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聲明鑑於或者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; 
+1

這兩個任務應該工作。它以什麼方式不起作用?編譯錯誤? – 2011-12-29 01:13:33

+0

嗨mattias,我有一段我的代碼加上我收到的錯誤的帖子.. – at0m87 2011-12-29 01:23:38

+0

這是作業,是嗎? – 2011-12-29 01:32:06

回答

0

它不起作用的原因(它可能與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]; 
} 
+0

謝謝你的幫助。其他的東西仍然不正確。我在所有if和else語句之外都有tweetShring = [str copy],但代碼仍然以相同的錯誤結束。 – at0m87 2011-12-29 02:01:17

+0

這不是你唯一需要改變的事情。你需要在if/else之外聲明變量str,就像我在我的例子中所說的那樣。閱讀我的代碼中的所有評論,你就會明白我在說什麼。 – 2011-12-29 02:05:13

+0

RIckay我設法通過了那個錯誤!我刪除了所有行中的「NSString」NSString * str = ... 它似乎是額外的聲明是一個NSString導致問題=)))現在我的日誌可以打印出tweetString =)字符串) ) – at0m87 2011-12-29 02:15:16

0

如果你想複製字符串,或者在分配字符串後使用字符串,你需要複製它或者保留它。

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走開在幾秒鐘或時鐘滴答,但copiedString將生活,直到功能完成或班級發佈。

我懷疑你沒有得到tweetString中的字符串值,因爲當你想使用它時,這兩個變量都從內存中消失了。

如果你需要一個變量留下來,你必須複製或保留它。

+0

我明白你的觀點。試圖在其後添加副本。仍然失敗。我認爲這個問題可能在別的地方。檢查我的編輯上面的調試器打印出來。 – at0m87 2011-12-29 01:55:55

相關問題