string = textView.text,但string是(nul)

时间:2015-12-08 07:45:40

标签: ios objective-c nsstring uitextview writetofile

我真的尝试了一切。我确定我的错误会像错过的 self .string一样愚蠢而小,但我似乎无法让它发挥作用。我的迷你程序所做的是检查名为Code.txt的文件是否已存在。如果确实存在,那么它将在UITextView中加载文件(并且它做得很好)。如果不存在,则会创建文件名Code.txt。所有这些都很好。

但是,当我尝试将UITextView中的当前文本保存到文件中时,我无法先将其保存到字符串中。这就是我想要实现的目标:

退出应用后:textView.text --> codeString --> Code.txt [saved]

除此之外,我无法将textView.text保存到字符串(codeString)。 NSLog将其作为坚果返回(请参阅saveFile方法中的NSLog(@"String: %@", self.codeString);)。

ViewController.h

#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "BuildViewController.h"

@interface ViewController : UIViewController <UITextViewDelegate>

@property (nonatomic, strong) NSString *codeString;
@property (nonatomic, retain) IBOutlet UITextView *textView;
@property (nonatomic, retain) BuildViewController *buildViewController;

- (void)saveFile;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize textView;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    textView.delegate = self;

    // Check if Code.txt exists
    NSString* documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *codeFile = [NSString stringWithFormat:@"%@/Code.txt", documentsDir];
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:codeFile];

    NSLog(@"viewWillAppear");

    if (fileExists == true) {
        // Code.txt does exist, continue to viewDidLoad
        NSLog(@"Code.txt does exist");
    }

    else if (fileExists == false) {
        // Code.txt does not exist, create the file
        NSLog(@"Code.txt does not exist");
        ViewController *viewController = [ViewController alloc];
        [viewController createFile];
    }

    self.textView.delegate = self;
}

- (void)viewWillAppear:(BOOL)animated {
    // Load Code.txt
    NSString* documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString* codeFile = [documentsDir stringByAppendingPathComponent:@"Code.txt"];
    NSString *codeString = [NSString stringWithContentsOfFile:codeFile encoding:NSUTF8StringEncoding error:NULL];
    textView.text = codeString;
    NSLog(@"Loaded Code.txt");
}

- (void)saveFile {
    // Save text to Code.txt
    self.codeString = textView.text;
    NSString *documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *codeFile = [NSString stringWithFormat:@"%@/Code.txt", documentsDir];
    [self.codeString writeToFile:codeFile
                 atomically:NO
                   encoding:NSStringEncodingConversionAllowLossy
                      error:nil];
    NSLog(@"String: %@", self.codeString);
    NSLog(@"Saved text to Code.txt");
}

- (void)createFile {
    NSString *documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *codeFile = [NSString stringWithFormat:@"%@/Code.txt", documentsDir];
    NSString *codeString = @"print(\"Hello, world\")";
    [codeString writeToFile:codeFile
              atomically:NO
                encoding:NSStringEncodingConversionAllowLossy
                   error:nil];
    NSLog(@"Created Code.txt");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

1 个答案:

答案 0 :(得分:0)

您的错误是您创建了{@ 1}}的新实例,如@Avi所说。

您应该在内存中获取现有实例

如果您想在后台保存文件,可以注册通知

ViewController中,添加此行

viewDidLoad

然后将此功能添加到 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];

ViewController

然后在dealloc

-(void)enterBackground:(NSNotification *)notification{
    [self saveFile];
}