AdvanceDeveloperFeaturediOS DeveloperNewbieObjective CProgramming Language

เขียนแอพ iPhone ภาษา Objective-C อ่านไฟล์ Plist

บทเรียนก่อนหน้านี้ที่เกี่ยวข้องคือ เขียนแอพ iPhone ภาษา Objective-C เขียนไฟล์ Plist  ครับ บทเรียนที่แล้วเราจะมีไฟล์ Plist ที่ถูกเขียนขึ้นมาเก็บไว้ใน Simulator Folder เสร็จสรรพ บทเรียนนี้คือการอ่านค่าเจ้าไฟล์ Plist ที่เขียนไว้เมื่อครู่ครับ

สร้างมาใหม่เลย
สร้างมาใหม่เลย

สร้าง Single View Application ขึ้นมาใหม่เลย สำหรับอ่านไฟล์ Plist นี้ แก้ไข ViewController.m ตามนี้ครับ

เจอแล้ว!!
เจอแล้ว!!

เริ่มต้น เราจะทำการตรวจสอบว่า เราพบไฟล์ดังกล่าวนี้อยู่หรือเปล่าเราจะใช้คำสั่งในการตรวจสอบต่อไปนี้

NSFileManager *fileManager = [NSFileManager defaultManager];
    if([fileManager fileExistsAtPath:filePath]){
        NSLog(@"Exist");
    }else{
        NSLog(@"Not Exist");
    }

ถ้าพบไฟล์ ก็ให้บอกเราว่า “Exist” ถ้าไม่พบก็จะเตือนบอกเราว่า “Not Exist” ครับ

ไฟล์ ViewController.m จะมี code ทั้งหมดเป็นแบบนี้

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    NSString *filePath = [documentsDirectory stringByAppendingString:@".plist"];
    NSLog(@"Directory: %@", filePath);
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if([fileManager fileExistsAtPath:filePath]){
        NSLog(@"Exist");
    }else{
        NSLog(@"Not Exist");
    }
}

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

@end

ทำการ Run ตัวแอพพลิเคชันของเราเพื่อตรวจสอบหน่อยว่าเจอหรือไม่?

เจอแล้ว!!
เจอแล้ว!!

ต่อมาให้เราทำการอ่านค่าจากไฟล์ขึ้นมาครับ โดยคำสั่งนี้

NSArray *array=[[NSArray alloc] initWithContentsOfFile:filePath];
        for (int i=0; i<2; i++){
            NSLog(@"%@",array[i]);
        }

นำไปวางที่ตำแหน่งดังตัวอย่างครับ

if([fileManager fileExistsAtPath:filePath]){
        NSLog(@"Exist");
        
        NSArray *array=[[NSArray alloc] initWithContentsOfFile:filePath];
        for (int i=0; i<2; i++){
            NSLog(@"%@",array[i]);
        }
        
    }else{
        NSLog(@"Not Exist");
    }

ลอง Run ตัวแอพพลิเคชันของเราใหม่อีกครั้งแล้วดูที่ console ใหม่ครับว่า ค่ามาหรือเปล่า

อ่าน Plist ไฟล์
อ่าน Plist ไฟล์

หวังว่าคงไม่อยากเกินไป Source code ก็ไม่ต้องมีหรอกครับ ง่ายแค่นี้เอง ถ้า Copy Code ไปวางแล้วยัง Error ก็พิจารณาตัวเองใหม่นะครับ 😀

Asst. Prof. Banyapon Poolsawas

อาจารย์ประจำสาขาวิชาการออกแบบเชิงโต้ตอบ และการพัฒนาเกม วิทยาลัยครีเอทีฟดีไซน์ & เอ็นเตอร์เทนเมนต์เทคโนโลยี มหาวิทยาลัยธุรกิจบัณฑิตย์ ผู้ก่อตั้ง บริษัท Daydev Co., Ltd, (เดย์เดฟ จำกัด)

Related Articles

Back to top button
Game & Mobile Development AR VR XR
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Adblock Detected

เราตรวจพบว่าคุณใช้ Adblock บนบราวเซอร์ของคุณ,กรุณาปิดระบบ Adblock ก่อนเข้าอ่าน Content ของเรานะครับ, ถือว่าช่วยเหลือกัน