Objective-C (iOS): テキストファイルを読み込む

ReadTextFileViewController

Objective-C: ReadTextFileViewController.h

#import <UIKit/UIKit.h>

@interface ReadTextFileViewController : UIViewController

@end

Objective-C: ReadTextFileViewController.m

#import "ReadTextFileViewController.h"

@interface ReadTextFileViewController ()

@end

@implementation ReadTextFileViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    NSError *error = nil;
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"txt"];
    NSString *string = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
    
    UILabel *label = [[UILabel alloc] init];
    label.text = string;
    [label sizeToFit];
    [self.view addSubview:label];
}

@end