我想從x代碼中存儲和加載全局變量的一些文本。我在主函數之外的main.m中聲明它。然後當我想訪問它時,我使用了extern。第二次點擊saveButton並顯示文本後,應用程序崩潰。看起來像重寫全局labelString字符串會有一些錯誤。你能解決這個難題嗎?xcode - 與字符串執行錯誤
編輯:由於BKC,我在代碼中做了一些小的修改,但是我仍然得到相同的錯誤。代碼已更新。
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (retain, nonatomic) IBOutlet UITextField *field;
- (IBAction)saveButton:(id)sender;
FOUNDATION_EXPORT NSString *labelString;
FOUNDATION_EXPORT NSString *separator;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController()
@end
@implementation ViewController
@synthesize field;
NSString *labelString = @"";
NSString *separator = @"|<->|";
- (IBAction)saveButton:(id)sender {
if([field.text length] != 0) // if field isn't blank
{
if([labelString length] == 0) // nothing stored in labelString
{
labelString = field.text;
}
else // if something is already stored in labelString
{
NSString *str = [NSString stringWithFormat:@"%@%@%@", labelString, separator, field.text];
labelString = str;
}
field.text = @"";
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
[field release];
[super dealloc];
}
@end
謝謝。
請添加一個崩潰日誌。 – paulrehkugler
在日誌中,唯一得到的是「(lldb)」但是我確實有一個當應用程序崩潰時立即出現的屏幕截圖。我不太清楚它是什麼意思,但:http://i.imgur.com/0jrqB5n.png – Sam
@ user2874028在Xcode中添加一個異常斷點,這將使您的調試變得更加輕鬆:https://developer.apple.com/ library/ios/recipes/xcode_help-breakpoint_navigator/articles/adding_an_exception_breakpoint.html –