투케이2K

61. (Objective-C/objc) NSBundle mainBundle 사용해 파일 경로 (path) 확인 및 내용 (contents) 출력 실시 본문

Objective-C

61. (Objective-C/objc) NSBundle mainBundle 사용해 파일 경로 (path) 확인 및 내용 (contents) 출력 실시

투케이2K 2022. 9. 7. 08:00
반응형

[개발 환경 설정]

개발 툴 : XCODE

개발 언어 : OBJECTIVE-C

 

[저장된 파일]

 

[소스 코드]

// MARK: - [헤더 파일에 정의 없이 : void 메소드 구현]
- (void)testMain {
    printf("\n");
    printf("==================================== \n");
    printf("[ViewController >> testMain() :: 테스트 메소드 수행] \n");
    printf("==================================== \n");
    printf("\n");
    
    
    // [파일 저장 경로 확인]
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"test" ofType:@"txt"];
    
    
    // [파일 내용 읽기 수행]
    NSString *fileRead = [NSString stringWithContentsOfFile:filePath
                                                   encoding:NSUTF8StringEncoding
                                                      error:NULL];
    
    // [로그 출력 실시]
    printf("\n");
    printf("==================================== \n");
    printf("[ViewController >> testMain() :: 로그 결과 출력 실시] \n");
    printf("[filePath :: %s] \n", filePath.description.UTF8String);
    printf("[fileRead :: %s] \n", fileRead.description.UTF8String);
    printf("==================================== \n");
    printf("\n");
}
 

[결과 출력]

 

반응형
Comments